How to read and write data to a Modbus server with different data formatting (different Byte Order and Word Order)

9 visualizaciones (últimos 30 días)

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

MathWorks Support Team
MathWorks Support Team el 17 de Dic. de 2024
Unlike Modbus Poll, which allows flexible formatting of byte and word orders, MATLAB relies on the server's endianness for data formatting, therefore as of MATLAB R2024b, the endianness needs to match to be able to read and write. The requirement that the endianness should match is detailed in the Byte Order Troubleshooting Tip for Modbus documentation page.
Currently, to format the data with different endianness of byte and word order then in MATLAB the "swapbytes" function can be used to swap the bytes after receiving the data from the Modbus server. If you want to receive the data in little-endian byte order and big-endian word order you can use the "swapbytes" function after receiving the data, but the "swapbytes" function swaps both the word order and the byte order, therefore to counter this you need to create the Modbus client with a little-endian word order, so that after the swapping the desired endianness is achieved.
An example of this workflow is provided below:
  1. Create a Modbus client:
    m = modbus("tcpip", ipaddress, port, "ByteOrder","big-endian","WordOrder","little-endian");
  2. Read the value from the register:
    val = read(m, "holdingregs", 3, 1, serverId, 'single');
  3. 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);
  4. Since the received data has its bytes swapped, it is necessary to swap the bytes again to interpret it correctly.
    myData = swapbytes(valSingle);
The "myData" variable now holds the expected value. If you want to use "write" instead, then a similar workflow detailed above is to be applied.
It is important to change the datatype of the received value (in this case, to "single") otherwise the bytes will be swapped incorrectly, leading to a wrong interpretation of the data.
An enhancement request has been submitted to enable this formatting of data in the desired byte order and word order in the Modbus Explorer app, and the development team will consider this for a future release.

Más respuestas (0)

Categorías

Más información sobre Modbus Communication en Help Center y File Exchange.

Productos


Versión

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by