Closed Zonciu closed 3 years ago
Hi, thanks for the Report. I can reproduce the behavior with 'Modbus slave'. For larger data types like INT32 the slave lets you choose between different byte layouts. But short/INT16 data are always sent with the big-endian layout.
Currently, FluentModbus does not support different byte layouts (only little endian). Unfortunately the Modbus specs are not very specific about byte layouts for data types larger than 2 bytes. Thats why I did not yet implement anything in that direction.
I already thought about how to solve this on client side. I think I will provide an overload method for each client Modbus function which then allows to specify the byte layout in the server which you need to get from the server's documentation. I will provide Little-Endian, Big-Endian, Little-Endian byte swap and Big-Endian byte swap. I will also add a default option so that the layout does not have to be specified in every single call. The only question left is what should be the default value.
On the master side of FluentModbus there could be similar method overloads, so the user can decide how to handle the data in the registers. Here also the question is what is a meaningful default? The best performance would bei simple Little-Endian layout but then you run into the issues you posted here. So I probably will use one of the Big-Endian layouts to better confirm with the spec. This means a breaking change and a major version bump. I hope to finish this by end of this week.
How about using the same byte order as the PLC as the default? Such as Siemens PLC.
I'm also experiencing the same problem with a Phoenix Contact PLC over here.
Thanks for your feedback. Unfortunately I don't have much time right now, but I`ll try to fix it next week.
Sorry for my late reply. I have released a new version that allows to specify the data layout (big-endian vs little-endian).
Example:
var client = new ModbusTcpClient(..., ModbusEndianness.BigEndian);
When you specify this layout, all data will be reversed automatically.
I´ll close this issue for now as it should be resolved. If you have further issues, feel free to reopen it.
I use
Slave Log
Modbus Slave
to simulate slave, but I get reverse data.Master code
Master output
I tested it in NModbus, it works correct. NModbus code
NModbus output