@@ -189,20 +189,20 @@ def make_packet(self, function, register, args):
189
189
190
190
def decode_packet (self , packet , spacket ):
191
191
'''Decode the modbus request packet.'''
192
- fcode = struct .unpack (">B" , packet [1 ])[0 ]
193
- addr = struct .unpack (">B" , packet [0 ])[0 ]
192
+ fcode = struct .unpack (">B" , bytes ([ packet [1 ]]) )[0 ]
193
+ addr = struct .unpack (">B" , bytes ([ packet [0 ]]) )[0 ]
194
194
if self .address != addr :
195
195
shex = ":" .join ("{:02x}" .format (ord (c ) for c in spacket ))
196
196
rhex = ":" .join ("{:02x}" .format (ord (c ) for c in packet ))
197
197
raise ModbusError ("Address error; Sent=%s, Recieved=%s" % (shex , rhex ))
198
198
if fcode > 127 :
199
- ecode = struct .unpack (">B" , packet [2 ])[0 ]
199
+ ecode = struct .unpack (">B" , bytes ([ packet [2 ]]) )[0 ]
200
200
ttp = (ecode , self .errorMessages .get (ecode , 'Unknown error code' ))
201
201
raise ModbusError ('Modbus Error: Exception code = %d(%s)' % ttp )
202
202
203
203
if fcode == 3 : #Read holding register(s)
204
- cnt = struct .unpack (">B" , packet [2 ]) [0 ]/ 2
205
- return struct .unpack (">%dH" % cnt , packet [3 :])
204
+ cnt = struct .unpack (">B" , bytes ([ packet [2 ]])) [0 ] / 2
205
+ return struct .unpack (">%dH" % cnt , bytes ( packet [3 :]) )
206
206
elif fcode == 6 :
207
207
pass #nothing is required
208
208
elif fcode == 16 :
0 commit comments