大家好,我在 Web 服务器 Tornado 中有这部分 Python 代码:
# Transform an integer into a string with HEX symbols in the format that is
# understandable by Quantaservd
def hextransform(data, length):
data = hex(data)[2:]
assert(len(data) <= length)
# zero-padding
data = ('0' * (length - len(data))) + data
# Swap 'bytes' in the network ID
data = list(data)
for i in range(0, length, 2):
tmp = data[i]
data[i] = data[i + 1]
data[i + 1] = tmp
# Reverse the whole string (TODO: CHECK)
data.reverse()
data = "".join(data)
return data
def hex2char(i):
return ('0'+hex(i)[2:])[-2:]
def cmd_payload(op1_hex, op2_hex, l, pl, ck):
return hex2char(op1_hex) + hex2char(op2_hex) + hex2char(l) + pl + hex2char(ck)
def packet2json(op_group, op_code, payload):
lun = len(payload) / 2
fcs = op_group ^ op_code ^ lun #fcs = XOR of op_group, op_code, lenght and payload
for i in range(len(payload)):
if ((i % 2) == 1):
fcs = fcs ^ int('0x' + payload[(i - 1):(i + 1)], 16)
print int('0x' + payload[(i - 1):(i + 1)], 16)
s=cmd_payload(op_group,op_code, lun, payload, fcs)
#p = r '{"data": "' + s + r '"}'
p=r'{"data": "'+s+r'"}'
return p
def invert2bytes(stringa, ind):
byte_low = stringa[ind:ind + 2]
ind += 2
byte_high = stringa[ind:ind + 2]
return byte_high + byte_low
我有这个错误:
File "./wsn.py", line 883, in get cmdjson = packet2json(op_group_hex,op_code_hex, packet)
File "./wsn.py", line 180, in packet2json fcs = fcs ^ int('0x' + payload[(i - 1):(i + 1)], 16)
ValueError: invalid literal for int() with base 19: '0x*`0'
当程序到达 hextransform 函数时......我不知道这是否是一个基本错误,因为在错误中它说明了关于 base 19 的内容,而 mu 函数在 base 16 中。或者如果问题是 '* ' 特点...
有任何想法吗?非常感谢。
编辑
我在调用 hextransform 函数的处理程序中添加了这部分代码:
#Command OnOffCmd_SetState
if command in ['ON', 'OFF', 'TOGGLE']:
op_group = "70"
op_code = "50"
packet_meta = "*%s;%s;%s;%s;02%s%s600080000%s#"
pkt_len = hextransform(16, 2)
netid = hextransform(int(nid), 16)
sens_id = hextransform(int(sid) >> 16, 4)
sens_id_little = invert2bytes(sens_id,0)
cluster_id = hextransform(int(sid) & 0x00FFFF, 4)
end_point = "08"
if command == 'ON':
cmd_data = "01"
elif command == 'OFF':
cmd_data = "00"
elif command == 'TOGGLE':
cmd_data = "02"
packet = packet_meta % (netid, op_group, op_code, pkt_len, sens_id, end_point, cmd_data)
packet = packet.upper()
op_group_hex=0x70
op_code_hex=0x50
cmdjson = packet2json(op_group_hex,op_code_hex, packet)
#Command ZDP-RestartNwk.Request
elif command == 'RESTARTNWK':
op_group = "A3"
op_code = "E0"
netid = hextransform(int(nid), 16)
packet_meta = "*%s;%s;%s;%s;02600080000#"
pkt_len = hextransform(0, 1)
packet = packet_meta % (netid, op_group, op_code, pkt_len)
packet = packet.upper()
op_group_hex=0xA3
op_code_hex=0xE0
cmdjson = packet2json(op_group_hex,op_code_hex, packet)
#Command ZDP-IEEE_addr.Request
elif command == 'IDENTIFY':
op_group = "A2"
op_code = "05"
packet_meta = "*%s;%s;%s;%s;02%s600080000#"
pkt_len = hextransform(2, 1)
netid = hextransform(int(nid), 16)
sens_id = hextransform(int(sid) >> 16, 4)
sens_id_little = invert2bytes(sens_id,0)
packet = packet_meta % (netid, op_group, op_code, pkt_len, sens_id)
packet = packet.upper()
op_group_hex=0xA2
op_code_hex=0x05
cmdjson = packet2json(op_group_hex,op_code_hex, packet)
编辑 2
如果我写
pkt_len = hextransform(2, 1)
我有错误。如果我有
pkt_len = hextransform(16, 2)