alejoseb / Modbus-STM32-HAL-FreeRTOS

Modbus TCP and RTU, Master and Slave for STM32 using Cube HAL and FreeRTOS
GNU Lesser General Public License v2.1
579 stars 192 forks source link

Modbus TCP, ERR_BAD_SIZE reading coils. #49

Closed Alberto-SM closed 2 years ago

Alberto-SM commented 2 years ago

Hi,

While using the MCU as a Modbus TCP master, trying to read 8 or less coils from a slave throws ERR_BAD_SIZE. I'm using pyModSlave as a slave.

I checked that request and response ADUs are correct with Wireshark, and also that the buffer in 'TCPgetRxBuffer' (Modbus.c) matches with the response ADU.

Debugging 'TCPgetRxBuffer' function, I noticed that in line 997 there is this condition: if (buflen>11) { // minimum frame size for modbus TCP

In the case I described above, buffer length is exactly 10 bytes: Transaction ID (2 bytes) + Protocol ID (2 bytes) + N.º following bytes (2 bytes) + Slave ID (1 byte) + Function Code (1 byte) + Data length (1 byte) + Data (1 byte in this case)

Is this a case the code currently doesn't take into account or am I missing something else?

alejoseb commented 2 years ago

Hi, You are right for master mode the shortest frame should be 10 bytes, I solved the issue in the latest commit.