ljean / modbus-tk

Create Modbus app easily with Python
Other
557 stars 211 forks source link

Result is wrongly decoded instead of unpacked after PR #148 #178

Closed Jordi-aguilar closed 1 year ago

Jordi-aguilar commented 1 year ago

Hi!

After updating modbus_tk to the latest version I noticed that my script stopped working due to the following issue:

After pull request #148 the response (variable data) of my instrument is evaluated like: data.decode() producing errors or wrong outputs. instead of: struct.unpack(data_format, data)

due to the following condition in this file:

if (re.match("[>]?[sp]?",data_format)):
    result = data.decode()
else:
    result = struct.unpack(data_format, data)

In my case, I do not pass any argument to data_format, but some of the examples of pairs of data and data_format before reaching this if condition are:

data = bytearray(b'F\n\xe0\x00')
data_format = >HH

or

data = bytearray(b'\x00\x00') 
data_format = >H

I believe that the problem here is that the evaluation re.match("[>]?[sp]?",data_format) is not accurate? Or should I use something else in the parameter data_format?

I have no idea about modbus, regex... Hopefully someone can shed some light. Personally I have made a work-around locally modifying this condition.

Thanks a lot!

Jordi-aguilar commented 1 year ago

Duplicate of #172