我使用python2.7,我有這個代碼。數據值範圍從0到65792.Python的struct.pack()數據範圍錯誤
data_length=30
code=202
data=[51400,31400,100,51400,31400,100,51400,31400,100]
checksum = 0
total_data = ['$', 'M', '<', data_length, code] + data
for i in struct.pack('<2B%dh' % len(data), *total_data[3:len(total_data)]):
checksum = checksum^ord(i)
total_data.append(checksum)
try:
b = None
b = self.ser.write(struct.pack('<3c2B%dhB' % len(data), *total_data))
except Exception, error:
print "\n\nError in sendCMD."
print "("+str(error)+")\n\n"
pass
struct.pack('<2B%dh' % len(data), *total_data[3:len(total_data)]):
,並給了我這個錯誤:
for i in struct.pack('<2B%dh' % len(data), *total_data[3:len(total_data)]):
struct.error: short format requires SHRT_MIN <= number <= SHRT_MAX
'h'是一個有符號短路,其最大正值爲32768.'H'是一個無符號短路,其最大值爲65535。 – tdelaney