- Create a Modbus client:m = modbus("tcpip", ipaddress, port, "ByteOrder","big-endian","WordOrder","little-endian");
- Read the value from the register:val = read(m, "holdingregs", 3, 1, serverId, 'single');
- Now though MATLAB reads the value from the server in "single" precision, it stores it in the variable as a "double". So change the datatype of the variable to "single".valSingle = single(val);
- Since the received data has its bytes swapped, it is necessary to swap the bytes again to interpret it correctly.myData = swapbytes(valSingle);
How to read and write data to a Modbus server with different data formatting (different Byte Order and Word Order)
I am using the Industrial Communication Toolbox in MATLAB R2024b and I have been trying to read and write functions over TCP/IP using Modbus, however, I operate a server configured with a big-endian byte order and word order, and need to read and write data in a format where the word order is big-endian but the byte order is little-endian.
When I tried setting the MATLAB Modbus client to little-endian byte order it resulted in the following error when using the Modbus Explorer app:
Failure reading from Modbus device. Verify that the register properties and the Modbus connection parameters are accurate.
Whereas when trying this using the MATLAB Command Window with APIs I get the following error when performing "read" or "write":
A communication error occurred while reading from the Modbus server: Timeout occurred waiting for a response.
However, when setting the byte order to big-endian then I receive incorrect values and no error.
When using Modbus Poll it gives the correct result without error.
Why is this error occurring?
Respuesta aceptada
0 comentarios
Más respuestas (0)
Ver también
Categorías
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!