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)

Most upvoted comments

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):

[RTU]>Tx > 14:22:18:203 - 06  01  00  02  00  01  5D  BD  
[RTU]>Rx > 14:22:21:942 - 06  81  01  30  51  
Sys > 14:22:21:942 - Error : Illegal function
[RTU]>Tx > 14:33:29:785 - 06  01  00  02  00  01  5D  BD  
[RTU]>Rx > 14:33:33:267 - 06  81  01  30  51  
Sys > 14:33:33:267 - Error : Illegal function
[RTU]>Tx > 14:34:08:339 - 06  01  00  02  00  01  5D  BD  
[RTU]>Rx > 14:34:12:132 - 06  81  01  30  51  
Sys > 14:34:12:132 - Error : Illegal function
[RTU]>Tx > 14:35:04:658 - 06  01  00  00  00  01  FC  7D  
[RTU]>Rx > 14:35:08:526 - 06  81  01  30  51  
Sys > 14:35:08:526 - Error : Illegal function
[RTU]>Tx > 14:35:15:936 - 06  01  00  01  00  01  AD  BD  
[RTU]>Rx > 14:35:19:435 - 06  81  01  30  51  
Sys > 14:35:19:435 - Error : Illegal function
[RTU]>Tx > 14:50:15:597 - 06  02  00  01  00  01  E9  BD  
[RTU]>Rx > 14:50:17:888 - 06  82  02  70  A0  
Sys > 14:50:17:888 - Error : Illegal data address
[RTU]>Tx > 14:50:52:471 - 06  03  00  01  00  01  D4  7D  
[RTU]>Rx > 14:50:55:171 - 06  83  03  B0  F0  
Sys > 14:50:55:171 - Error : Illegal data value

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 …