espressif / esp-modbus

ESP-Modbus - the officially suppported library for Modbus protocol (serial RS485 + TCP over WiFi or Ethernet).
Apache License 2.0
114 stars 54 forks source link

Data endianness problem (IDFGH-13693) #72

Closed ForgetCSX closed 1 month ago

ForgetCSX commented 1 month ago

Checklist

Issue or Suggestion Description

I encountered a format issue with the data endianness when using this repository. image image When I read the holding register at address 0, the data returned to me is 85 1F 3F AB Converting 1.34 through dotted hexadecimal converter the single precision float big endian format I get is 3F AB 85 1E The float little endian format is 1E 85 AB 3F This will cause my modbus master device to not recognize the received data

This is the data online converter I use https://www.asciim.cn/hex/float.html

There is another question. I use the 06 command to write a single register. In the same way, I still operate the register at address 0. image As you can see, the writing should be successful, and the data returned to me is also 100, but when printing on the slave machine, there is still no change. image

alisitsyn commented 1 month ago

@ForgetCSX,

The frame logger shows the raw ADU of Modbus frame. As per Modbus specification: image. This means you should not expect the same order of the data as you described above because the Modbus standard standard does not prescribe the extended data types mapping and it can be performed differently by device vendors and by different software. The esp-modbus Slave by default converts the 32-bit data from big-endian to the 'Little-endian byte swap' format understandable by compiler. However, the esp-modbus supports the data conversion functions and you can convert the data to the format that is needed for your particular Slave application. The extended types support can be enabled by CONFIG_FMB_EXT_TYPE_SUPPORT=y in the kconfig menu. image

See the esp-modbus extended datatypes, Modbus Endianness Conversion API Reference, example of conversion API. If you are using the esp-modbus master with your slaves, you can configure the master to use appropriate format option to work with your slave and the extended data conversion will be performed automatically by master in this case.

There is another question. I use the 06 command to write a single register. In the same way, I still operate the register at address 0.

This is because you try to write one register (2 bytes) to the same mapping area which corresponds to the float data and then printed as a float in your application. The result of this can not be correct. Configuring Slave Data Access

Please read the above and let me know if you have further questions.

ForgetCSX commented 1 month ago

@alisitsyn Thank you for your reply. I understand. Generally speaking, the problem is the data format. I didn’t know where to adjust it at first. I can adjust the byte order and it will be no problem. Now there is no doubt.

alisitsyn commented 1 month ago

The issue is closed as explained. Feel free to reopen.