modbus icon indicating copy to clipboard operation
modbus copied to clipboard

Add a data type UINT16_BS : byte swapped in unsigned 16-bit integers

Open lvhuihui1988 opened this issue 7 months ago • 3 comments

The PLC we used stores a word as an unsigned 16-bit integer. But bytes with this 16-bit register are swapped. This data type is not supported by Modbus. Therefore, I modified the source code to support 16-bit unsigned intergers with swapped bytes within a word. I add a new data type called UINT16_BS.

lvhuihui1988 avatar Jun 20 '25 09:06 lvhuihui1988

What type of computer are you running the Modbus driver on (for example Intel x64 with Linux, etc.)?

I want to make sure the problem is not with the endianness of the Modbus client. The Modbus specification is very clear that 16-bit values should be transmitted in network byte order, i.e. big-endian. The Modbus driver converts to the endianness of the host.

MarkRivers avatar Jun 20 '25 12:06 MarkRivers

We use Linux x86-64. In Modbus, data types such as INT32_LE_BS and UINT32_LE_BS support byte swapping. However, for 16-bit unsigned integers, only UINT16 is available by default. To address this, I have added UINT16_BS, which enables byte swapping support for 16-bit unsigned integers.

lvhuihui1988 avatar Jun 23 '25 02:06 lvhuihui1988

I think for completeness we should also add INT16_BS.

MarkRivers avatar Aug 09 '25 13:08 MarkRivers