ljean / modbus-tk

Create Modbus app easily with Python
Other
570 stars 212 forks source link

Default block unit length is two bytes why? #154

Open hxmwr opened 3 years ago

hxmwr commented 3 years ago

https://github.com/ljean/modbus-tk/blob/14c70f2c3c794b7e2944762f408b98e7bc77a8fe/modbus_tk/modbus.py#L500 Every block unit length must be 2 bytes, is this part of protocol or just convention for this project. If I have a UINT (4 bytes) value, I have to split it to 2 WORD and then store it in two block units (if not, struct.pack would failed when reading), when reading this value I have concat these two WORD and this is not so convient.

blotsome commented 3 years ago

In Modbus, Holding Registers and Input Registers are 16-bit (2 bytes) per register. This is a protocol convention. It is very common for 32-bit data to be stored in 2 registers, for example floating point or as you mentioned UINT32. There is no standard on which register goes first when decoding, which is why software like modscan32 allows you to select between MSR and LSR. It is a limitation of the protocol, but I guess something you get used to when working with modbus.