arduino-libraries / ArduinoModbus

258 stars 123 forks source link

ModbusClient.requestFrom() crashes Finder Opta if id > 247 #151

Open gareddustephane opened 6 months ago

gareddustephane commented 6 months ago

When implementing Modbus RTU on an Opta Finder, with the classical Arduino IDE, in order to read coils on a remote slave (server), we must use the following method :

https://github.com/arduino-libraries/ArduinoModbus/blob/cdae56d97dfc0b5aac709f5349dcdd5fbe7aed8a/src/ModbusClient.h#L172

However, the device crashes if the id is greater than 247 as in the following piece of code :

ModbusRTUClient.requestFrom(248, COILS, 0x00, 10)

the RESET LED blinks red and no code is executed. Moreover, we can't upload a sketch anymore onto the device. The RESET button has to be pressed twice in order to make the Opta Finder be recognized again.

The only place in the requestFrom() method where the id is used here :

https://github.com/arduino-libraries/ArduinoModbus/blob/cdae56d97dfc0b5aac709f5349dcdd5fbe7aed8a/src/ModbusClient.cpp#L335

Calling this function:

https://github.com/arduino-libraries/ArduinoModbus/blob/cdae56d97dfc0b5aac709f5349dcdd5fbe7aed8a/src/libmodbus/modbus-rtu.cpp#L131-L144

Hence, here, a test is performed in order to verify the id of the targeted slave. But as the line modbus_set_slave(_mb, id); in the requestFrom() method doesn't, this leads to a crash. In other words, the error raised by modbus-rtu.cpp is not propagated to the requestFrom() method.

This could be easily fixed by making a simple check.