grid-x / modbus

BSD 3-Clause "New" or "Revised" License
78 stars 26 forks source link

feat(cmd/modbus-cli): support writing single coil #42

Closed frzifus closed 1 year ago

frzifus commented 2 years ago

With this pr the modbus-cli tool is extended by the function 0x05. This allows to switch individual outputs on and off.

Usage:

# read
modbus-cli -slaveID=0xFF -address=tcp://127.0.0.1:502 -fn-code=0x01 -type-parse=uint8 -register=54321 -quantity=1 -log-frame

# Turn on
modbus-cli -slaveID=0xFF -address=tcp://127.0.0.1:502 -fn-code=0x05 -type-parse=uint8 -register=54321 -quantity=1 -log-frame -write-value=0xFF00

# Turn off
modbus-cli -slaveID=0xFF -address=tcp://127.0.0.1:502 -fn-code=0x05 -type-parse=uint8 -register=54321 -quantity=1 -log-frame -write-value=0x0000

Ref: https://ozeki.hu/p_5880-mobdbus-function-code-5-write-single-coil.html

Signed-off-by: Benedikt Bongartz benne@klimlive.de

andig commented 2 years ago

IMHO the point here is that the on/off values for the coils are non-obvious. Do they depend on modbus implementation's endianess? If not, the on/off values should be part of the api.

guelfey commented 2 years ago

I agree, I'd rather not have to know this detail that on is 0xFF00 (I had to look this up right now). Maybe just take any nonzero value as on?

frzifus commented 2 years ago

Do they depend on modbus implementation's endianess?

oh good question, will try to get that out.

Maybe just take any nonzero value as on?

Sounds fair to me.

andig commented 1 year ago

What is missing to get coils support merged?

frzifus commented 1 year ago

öhm, seems we're almost celebrating a one year anniversary. :partying_face: :balloon:

Do they depend on modbus implementation's endianess?

This is what modbus.org says about it:

4.2 Data Encoding • MODBUS uses a ‘big-Endian’ representation for addresses and data items. This means that when a numerical quantity larger than a single byte is transmitted, the most significant byte is sent first. So for example Register size value 16 - bits 0x1234 the first byte sent is 0x12 then 0x34

Other than that, i haven't found any further info during the last year.

Maybe just take any nonzero value as on?

Adapted it, now every value that is non-zero represents on.

andig commented 1 year ago

Recently used https://ozeki.hu/p_5880-mobdbus-function-code-5-write-single-coil.html. Seems we can acceppt any non-zero Value as "on", but devices will only ever accept/return 0xFF00 bigendian.