I am currently working on a project of data acquisition with a RaspberryPi. When i launch my request script, sometimes (rare but too often) my slave (computer) detects a CRC error or invalid length. I'm guessing the error could come from the fact that, as my script is fast and asks for hundreds of register in a few seconds, sometimes the message is incomplete and my slaves detects it as a wrong message. I wanted to know if it was possible that minimalmodbus isn't timed right and sometimes will send a wrong or part of a request (incomplete).
Error returned on the slave:
invalid request: Invalid CRC in request
This is a typical error i get on my master when the slave doesn't know what to answer:
error = SLAVE_ERRORS[str(e)]
KeyError: "Checksum error in rtu mode: '\\x8aÿ' instead of '\\x8fF' . The response is: '4ÿ\\x07$Ê\\x8aÿ' (plain response: '4ÿ\\x07$Ê\\x8aÿ')"**
I use modbus_tk on the slave to emulate a modbus slave. And next is typically a part of my code that requests values from the slave that depends on a type of value.
try:
try:
var_register = file_var[i]['varRegister']
var_type = file_var[i]['varType']
var_use = file_var[i]["varUse"]
var_name = file_var[i]["varName"].strip()
if '#' in var_register:
continue
elif var_type=='U16' or var_type=='I16' or var_type=='S16':
value = inst.read_register(
int(var_register),
0,
3,
not bool(file_var[i]['varSigned'])
)
elif var_type=='U32' or var_type=='I32' or var_type=='S32':
value = inst.read_long(
int(var_register),
3,
not bool(file_var[i]['varSigned'])
)
As my first guess was a problem of timing i randomly inserted "time.sleep" to time my requests but the errors are still coming up. And it's completely random, sometimes it'll work for 5 mins and sometimes just a few seconds before my first CRC error. Do you have any idea on where I should look into? Thanks in advance for your help!
EDIT: My PC acts as a slave with a modbus_tk script emulating multiple slaves. The RPI is the master asking for registers and their values. the slaves are all configured to have values in those specific register in order to avoid an IllegalAddress error. The physical connection is a USB to RS485 converter, the RPI is equipped with a HAT handling RS485 input/output. So it is in fact a ModBus RTU communication. The slave loops and sends an answer when a request comes in.
EDIT2: So I have investigated a bit further and found something interesting. I received an error (this time on the master) the slave apparently sent the wrong Checksum... while looking into it, i found that the checksum generated by modbus_tk was fine, but the answer received by the master wasn't the same. It looks like some bytes are changed on the road which is weird. Where could that come from? Hardware issue? Only does this error when polling multiple registers though (more than 2 at once). all other requests are fine if the number of registers asked for, is 1 or 2.