ArduinoModbusSlave: Invalid code ("Illegal function")
Looking at the code: https://github.com/yaacov/ArduinoModbusSlave/blob/master/src/ModbusSlave.h#L45
enum {
CB_MIN = 0,
CB_READ_COILS = CB_MIN,
CB_READ_DISCRETE_INPUTS,
CB_READ_HOLDING_REGISTERS,
CB_READ_INPUT_REGISTERS,
CB_WRITE_COILS,
CB_WRITE_HOLDING_REGISTERS,
CB_READ_EXCEPTION_STATUS,
CB_MAX
};
The CB_READ_COILS = CB_MIN
assignment looks very wrong to me. as they are compared in the poll
function with the FC_xxx
values.
With the code currently in master, the arduino code only return errors for any request I made from the master (I use QModMaster Windows app) whose packets are correct.
Is master tested or am I missing something? The (very) old library worked nicely but I needed to remove the CTRL pin because of a new RS485 adapter.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 15 (15 by maintainers)
I suspect the problem is due to timing problems. In my loop function, I poll the modbus, read some sensors and write on the SPI screen. Both the sensors and the SPI screen are slow. By adding other two
slave.poll()
between the sensor read and the SPI writing, the problems go away. I suspect the logic around the ‘time of the last communication’ is wrong. It shouldn’t really relevant whether the last poll was done a long time ago or not.The reason why I started poking at the library is because it doesn’t work anymore (arduino pro mini at 9600 baud):
The log is done from QModMaster who has the “monitor” function, and verified by another reader using a serial monitor who print hex codes.
As you can see there are many “8x” codes for all the responses which means error. I am still investigating what the reason could be …