pyhys / minimalmodbus

Easy-to-use Modbus RTU and Modbus ASCII implementation for Python.
Apache License 2.0
308 stars 145 forks source link

"ValueError: Wrong functioncode: 4 instead of 3. The response is: '\x01\x04\x04\x00\x00\x00\x00û\x84'" #37

Closed laneschmidt closed 5 years ago

laneschmidt commented 6 years ago

System Information

Problem

This error was thrown from the following code:

var_1 = i.read_registers(registeraddress=2012, numberOfRegisters=2, functioncode=3)

The device was setup like this, if it's relevant:

i = minimalmodbus.Instrument(port='/dev/ttyUSB0', slaveaddress=1, mode='rtu')
i.serial.baudrate = 19200
i.serial.bytesize = 8
i.serial.parity = serial.PARITY_EVEN
i.serial.stopbits = 1
i.serial.timeout = 1

The only thing I can think of is Python is returning the wrong line when providing the ValueError.

I wanted to post this to make this Git aware of the problem, I know it's not the most active repo. I think this is just a one-off exception but this code has been working for over a year without fail.

Thanks

pyhys commented 5 years ago

Yes, sometimes there might be a glitch in the serial communication. It is a good thing to have error handling in your script to handle this.