PROPHET/OS
NAME
modbus: asks a device over Modbus TCP, on port 502, and says the refusal it gets back
SYNOPSIS
modbus read <address> <unit> <what> <at> [count]|write <address> <unit> <what> <at> <value ...>|selbsttest
DESCRIPTION
Asks a device over Modbus TCP, on port 502, and says the refusal it gets back.
read takes one of the four tables a device holds: coils and inputs are bits, holding and input are registers of sixteen bits. Without a count it reads one. write takes coil and register with exactly one value, and registers with one or more, which is the multiple write. Which of the two write function codes goes out is said on the command line and never guessed from the count of values: a tool that picks the code behind the operator writes with a code the device may not know.
One line per item comes out, the address and the number the wire carried. What that number means stands in the device's data sheet and nowhere else: Modbus has no naming at all, so this tool carries the bytes and never the meaning.
A device that will not do what it was asked answers with the function code plus 0x80 and one byte of reason. That is written out as a sentence with its code, and the tool gives 1. Swallowing it would be how a plant tool lies: a read that was refused and reported as a zero is a reading of a register nobody read.
The <unit> is the address of the device, 0 to 255. On a serial line it is the slave address, 1 to 247, and 0 is the broadcast; behind a gateway it says which device, and 255 is the one that answers for itself. It is not optional, because a plant tool that silently talks to unit 1 is the same kind of lie as one that swallows a refusal.
The address is an address and no name: resolve says what a name is. One exchange and no vigil: whoever wants a register every minute puts the line into cronv.
What is served is read coils (01), read discrete inputs (02), read holding registers (03), read input registers (04), write single coil (05), write single register (06) and write multiple registers (16). Missing: 15 is 05 for a stretch, 23 is 03 and 16 in one frame, and 20 with 21 are the file records of a plant historian. None of them can be reached with the seven above, so each is built when a device needs it and not before.
The format itself lies in lib/os/modbus.c, outside the kernel, and the area modbus drives it against byte sequences of the specification written out by hand. The CRC of the serial frame and the length field of the TCP header have a probe each: the CRC is the one number of this protocol that goes low byte first, and the length counts the unit byte behind it and not the six bytes in front of it.
Both wrappers are built and modbus selbsttest drives both, in ring 3, against a device in memory: this machine has no loopback, so there is no way to give a program of ours the other end of a connection. The tool itself reaches only the TCP wrapper, because ring 3 has no door to a serial line: POS_DEV_SERIAL_READ and POS_DEV_SERIAL_WRITE stand in the call table without a handler, and COM1 belongs to the console. What that leaves open is a real device on a real line, and it is open.
EXAMPLE
$ modbus --help modbus read <address> <unit> <what> <at> [count]|write <address> <unit> <what> <at> <value ...>|selbsttest asks a device over Modbus TCP, port 502, and says the refusal it gets back read takes coils, inputs, holding or input; write takes coil, register or registers the frames of a serial line stand beside those of TCP, and selbsttest drives both