Reputation: 3
I am communicating with a modbus rtu device with 32bit wide registers, custom apparently.
I tried using a modbus library out of the box but no luck as I get the error as follows modbus_tk.exceptions.ModbusInvalidResponseError: Response length is invalid 0
Two things I do not understand...
response.decode('utf-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc1 in position 4: invalid start byte
Ultimately the response message is a pressure reading and should look something like 97.6
. I cannot figure out how to interpret the byte string to be 97.6
.
Any help is greatly appreciated.
I tried decoding to utf-8 and converting the hexadecimal bytes to ascii with no success.
So I attempted to can my request for holding register, I tried ser.write(b'\x01\x03\x07\xd0\x00\x01\xc4\x86')
and I got a response.
response = b'\x01\x03\x04B\xc1ff\x14='
Which I believe is unicode however I am not sure.
Upvotes: 0
Views: 410
Reputation: 539
Yes, I suppose you get an error, because modbus registers are 16 bits in length. The invalid start byte most likely means that you are starting your reading in the wrong register. Modbus registers are sequential, and have a weird offset from the docs. Like, say your reference states address 40088. By being 40xxx we can assume this is a Function 3 register, but is not mandatory. Now, in fact the address can be base 0 or base 1, meaning to substract or add 1 to it, making the real address 40087 in most cases.
So if you go and read 40088, you'll have a starting byte that doesn't match.
About coding, modbus is Big endian for words and bytes, so you'd have to verify the decoder, and finally the RTU frame comes with a lot more information than the requested value. You need to read the modbus spec to know, but generally there's 1 byte for function, 2 bytes for length, 2 bytes for answer, then 1 byte for closing, or something like that. I don't know the details by heart, I use pymodbus! haha Cheers
Upvotes: 0