smarmengol / Modbus-Master-Slave-for-Arduino

Modbus Master-Slave library for Arduino
GNU Lesser General Public License v2.1
473 stars 328 forks source link

"Timeout" on Arduino Uno R4 Wifi due to Serial1.write(buf, len) and CRC check #75

Open SeanMWX opened 6 months ago

SeanMWX commented 6 months ago

Overview: Users of the Arduino Uno R4 WiFi experiencing "Timeout" errors when using Serial1 might find this workaround helpful. The issue appears to be related to how Serial1.write(buf, len) transmits data, specifically affecting the transmission length, leading to the failure in CRC check, finally resulting in "Timeout" errors.

Issue description: When attempting to send data using Serial1.write(buf, len), the number of elements transmitted is consistently one less than the specified len. For example:

  1. If len = 8, only the first 7 elements of the buffer are sent.

Screenshot from 2024-02-18 21-37-04

  1. To transmit all 8 elements, len must be set to 9.

Screenshot from 2024-02-18 21-40-45

Solution: To address this issue, you can increment the buffer size before the operation in Modbus::sendTxBuffer() as follows:

u8BufferSize++; // Increment buffer size to accommodate the missing byte
port->write(au8Buffer, u8BufferSize);

This adjustment ensures that the intended number of elements is transmitted, potentially resolving the timeout issue by correcting the data length for CRC checks.

While this sollution has been effective for my setup using the Arduino Uno R4 WiFi, it's not clear if it applies universally to other Arduino models or scenarios.

Additionally: Arduino IDE version: 2.3.1