Just spent a lot of time debugging a strange problem, I am using a heavily modified version of this library to read about 300 registers from a PLC. Leaving this here in case it saves somebody the time.
BUG:
When the uC is busy and does not read the Serial buffer, a new Query may be sent before the buffer is emptied, this causes an offset, after which, the data is stored in the incorrect au16reg, because it's the response to a previous query. I modified the Modbus::query function as follows:
int8_t Modbus::query( modbus_t telegram ) {
port->flush(); //Waits for the transmission of outgoing serial data to complete.
while ( port->available() ) port->read(); //Clears RX buffer before sending a new query
Great library! Many thanks to the author.
Just spent a lot of time debugging a strange problem, I am using a heavily modified version of this library to read about 300 registers from a PLC. Leaving this here in case it saves somebody the time.
BUG: When the uC is busy and does not read the Serial buffer, a new Query may be sent before the buffer is emptied, this causes an offset, after which, the data is stored in the incorrect au16reg, because it's the response to a previous query. I modified the Modbus::query function as follows:
int8_t Modbus::query( modbus_t telegram ) { port->flush(); //Waits for the transmission of outgoing serial data to complete. while ( port->available() ) port->read(); //Clears RX buffer before sending a new query