Package: modbus-cli
Author:favalex@gmail.comDate:2023-02-26Copyright:MPL 2.0Version:0.1.9Manual section:1SYNOPSISmodbus [-h] [-r REGISTERS] [-s SLAVE_ID] [-b BAUD] [-p STOP_BITS] [-P {e,o,n}] [-v] device access [access …]DESCRIPTIONRead and write registers of Modbus devices.Access both TCP and RTU (i.e. serial) devices and encode and decode types
larger than 16 bits (e.g. floats) into Modbus 16 bits registers.Optionally access registers by symbolic names, as defined in a registers file.
Symbolic names for enumerations and bitfields are supported too.Designed to work nicely with other standard UNIX tools (watch,socat,
etc.), see the examples.Implemented in python on top of the protocol implementation provided by the
umodbus python library.INSTALLRegular python install, eitherpip install modbus_clito install from pypi
orpython setup.py installto install from source.OPTIONSdevice/dev/ttyXXXfor serial devices, orhostname[:port]for TCP devicesaccessOne or more read or write operations. See ACCESS SYNTAX below.-rFILE,--registers=FILERead registers definitions from FILE. Can be specified multiple times.-v,--verbosePrint on screen the bytes transferred on the wire.-bBAUD,--baud=BAUDSet the baud rate for serial connections.-pBITS,--stop-bits=BITSSet the number of stop bits for serial connections.-PPARITY,--parity=PARITYSet the parity for serial connections: (e)ven, (o)dd or (n)one-BORDER,--byte-order=ORDERSet the byte order to one of ‘le’ (little endian), ‘be’ (big endian) or ‘mixed’-h,--helpShow this help message and exit.ACCESS SYNTAX[MODBUS_TYPE@]ADDRESS[/BINARY_TYPE][:ENUMERATION_NAME][=VALUE]Mnemonic: access the register(s) of MODBUS_TYPE startingatADDRESS,
interpreting them as BINARY_TYPE. The/syntax is inspired by gdb (but the
available types are different.)MODBUS_TYPE = h|H|i|c|C|dThe modbus type, one ofcodenamesizewritableh or Hholding register16 bitsyesiinput register16 bitsnoc or Ccoil1 bityesddiscrete input1 bitnoTypesCandHforce the use of “write multiple registers” even when accessing a single register.  This
is useful for some devices that don’t support singular write functions.The default modbus type is holding register.ADDRESS = <number>0-based register addressBINARY_TYPE = <pack format>Any format description accepted by the python standardpackmodule. Some common formats are:codetypeh16 bits signed integerH16 bits unsigned integeri32 bits signed integerI32 bits unsigned integerf32 bits IEEE 754 floatThe default byte order is big-endian.
To use little endian, use a<prefix, or--byte-order=le.
To use mixed endian (little endian, but individual 16 bit registers are big endian), use--byte-order=mixed.VALUE = <number>The value to be written to the register. If not present, the register will be read instead.If only one register is written to, the modbus function6 (0x6), “write single register” is used.
If multiple registers are written to, the modbus function16 (0x10), “write multiple registers” is used.EXAMPLESRead a holding register$ modbus $IP_OF_MODBUS_DEVICE 100Write a holding register$ modbus $IP_OF_MODBUS_DEVICE 100=42Read multiple registersTo read (or write) multiple registers simply list them on the command line:$ modbus $IP_OF_MODBUS_DEVICE 100 c@2000When performing access to multiple contiguous registers, one single modbus operation is performed.When multiple modbus operations are needed, they are all initiated at once, and
the results are collected as they arrive.More examples of the access syntaxh@39/Iread the 32-bits unsigned integer stored in holding registers at addresses 39 and 4039/Isame as above (h is the default modbus type)39/I=42write the integer 42 to that registerSOME_REGISTER=42same as above, provided the registers file contains the definitionSOME_REGISTER h@39/I39/I=0xcafethe value can be specified in hexadecimalc@5read coil at address 5h@24/f=6.78write a floating point value to holding registers at addresses 24 and 25i@1/6Bread six unsigned bytes stored in input registers at addresses 1, 2 and 3Monitor a registerThe UNIX commandwatchcan be used to read a register at regular intervals:$ watch modbus $IP_OF_MODBUS_DEVICE 100Read a serial device attached to a remote computerThe UNIX commandsocatcan be used to access a remote device through a TCP
tunnel:remote$ socat -d -d tcp-l:54321,reuseaddr file:/dev/ttyUSB0,raw,b19200
local$ socat -d -d tcp:sc:54321 pty,waitslave,link=/tmp/local_device,unlink-close=0
local$ modbus /tmp/local_device 100Read multiple registers based on their namesGiven the following registers definitions:$ cat registers.modbus
di0 d@0
di1 d@1
ai0 i@512
ai1 i@513glob matching (*, ?, etc.) can be used to read all theairegisters at once:$ modbus -r registers.modbus $IP_OF_MODBUS_DEVICE ai\*REGISTERS FILESThe purpose of the registers files is to be able to refer to registers by name.There can be multiple definition files, specified using either the-rcommand line switch or theMODBUS_DEFINITIONSenvironment variable.A#in a definition file starts a comment.Each line contains a symbolic name followed by a register definition. The name
and the definitions are separated by spaces, for example:status i@512:STATUS
leds 513:LEDSThe file can also contain the possible values for an enumeration or a bitmask,
for example:# This is an enumeration named STATUS
:STATUS
  0=OFF
  1=ON
  2=ERROR

# This is a bitmask named LEDS
|LEDS
  0=LED0
  1=LED1
  3=LED3
  4=LED4ENVIRONMENTMODBUS_DEFINITIONSA colon separated list of register definitions files.SEE ALSOmodbusumodbuspack format