A modbus CLI written in C based on libmodbus.
modbus [-p=<profile>] [-a=<addr:port>] [-s=<slave_id>] [-t=<u16|u32>] [-w=<hl|lh>] <register>[=<value>]
-t: register type,u16(default) oru32-w: word order foru32,hl(default, high word first) orlh(low word first)
The modbus CLI makes it easy to handle multiple modbus devices in your home with the help of profiles. It supports a simple CSV based profile file, located in the user's home directory.
If there is only one valid profile listed it will be used when -p is not specified.
If multiple profiles exist, one named default will be used when -p is not specified.
If multiple profiles exist and none is named default, pass -p <name>.
Line format: Name,IP,PORT,SlaveID
Example content of the file under ~/.modbus:
default,192.168.1.200,4196,1
boiler,192.168.2.101,502,0# Read the register 41 from the given address
modbus -a 192.168.1.200:4196 41
# Specify slave id for the above operation
modbus -a 192.168.1.200:4196 -s 1 41
# Read a uint32 from registers 41 and 42 (high word at 41, low word at 42)
modbus -a 192.168.1.200:4196 -t u32 41
# Read a uint32 where word order is low word first
modbus -a 192.168.1.200:4196 -t u32 -w lh 41
# Write uint32 value 123456 to registers 41 and 42
modbus -a 192.168.1.200:4196 -t u32 41=123456In this case you have profile file the usage is just simply:
# Read the register 41
modbus 41
# Write the value 34 to the register number 41
modbus 41=34
# Write the value 34 to the register number 41 for the boiler
modbus -p boiler 41=34You need to have libmodbus installed. On macOS it is:
# Install libmodbus
brew install libmodbus
# Expose brew based headers and shared libraries
export CPATH="$HOMEBREW_PREFIX/include:$CPATH"
export LIBRARY_PATH="$HOMEBREW_PREFIX/lib:$LIBRARY_PATH"Build and run:
# Compile
make
# Run
./modbus -hRun unit tests:
make testThe test binary covers parser helpers, word-order conversion helpers, address parsing, and profile-selection behavior.