python在调用pyserial通信的时候,接收到串口调试助手或者设备发送过来的HEX值,在特定位置会出现错误编码,接收到的不是十六进制数据,而是一些乱符号,' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/'等等,这个是因为serial中没有对应的解码机制,python中带有的decode()解码对特定的一部分HEX值可以解决,对于其他是不能解决的,如果我们要正确接收HEX值应该如果解决呢?在这里HEX值只需要写两位。
解决这个问题我们首先要是到ASCII与HEX的关系,这里有两个映射表,分别对应发送端的HEX值和接收端的HEX值,在这里涉及到的转换命名如下:发送端以ascii_data命名,接收端以hex_data命名。
ascii_data = [['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0A', '0B', '0C', '0D', '0E', '0F'],
['10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1A', '1B', '1C', '1D', '1E', '1F'],
['20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2A', '2B', '2C', '2D', '2E', '2F'],
['30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3A', '3B', '3C', '3D', '3E', '3F'],
['40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4A', '4B', '4C', '4D', '4E', '4F'],
['50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5A', '5B', '5C', '5D', '5E', '5F'],
['60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6A', '6B', '6C', '6D', '6E', '6F'],
['70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7A', '7B', '7C', '7D', '7E', '7F'],
['80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '8A', '8B', '8C', '8D', '8E', '8F'],
['90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '9A', '9B', '9C', '9D', '9E', '9F'],
['A0', 'A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF'],
['B0', 'B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B9', 'BA', 'BB', 'BC', 'BD', 'BE', 'BF'],
['C0', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'CA', 'CB', 'CC', 'CD', 'CE', 'CF'],
['D0', 'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8', 'D9', 'DA', 'DB', 'DC', 'DD', 'DE', 'DF'],
['E0', 'E1', 'E2', 'E3', 'E4', 'E5', 'E6', 'E7', 'E8', 'E9', 'EA', 'EB', 'EC', 'ED', 'EE', 'EF'],
['F0', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'FA', 'FB', 'FC', 'FD', 'FE', 'FF']]
hex_data = [['\\x00', '\\x01', '\\x02', '\\x03', '\\x04', '\\x05', '\\x06', '\\x07', '\\x08', '\\t', '\\n', '\\x0b', '\\x0c','\\r', '\\x0e', '\\x0f'],
['\\x10', '\\x11', '\\x12', '\\x13', '\\x14', '\\x15', '\\x16', '\\x17', '\\x18', '\\x19', '\\x1a', '\\x1b', '\\x1c', '\\x1d', '\\x1e', '\\x1f'],
[' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/'],
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?'],
['@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'],
['P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\\\', ']', '^', '_'],
['`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'],
['p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~', '\\x7f'],
['\\x80', '\\x81', '\\x82', '\\x83', '\\x84', '\\x85', '\\x86', '\\x87', '\\x88', '\\x89', '\\x8a', '\\x8b', '\\x8c', '\\x8d', '\\x8e', '\\x8f'],
['\\x90', '\\x91', '\\x92', '\\x93', '\\x94', '\\x95', '\\x96', '\\x97', '\\x98', '\\x99', '\\x9a', '\\x9b', '\\x9c', '\\x9d', '\\x9e', '\\x9f'],
['\\xa0', '\\xa1', '\\xa2', '\\xa3', '\\xa4', '\\xa5', '\\xa6', '\\xa7', '\\xa8', '\\xa9', '\\xaa', '\\xab', '\\xac', '\\xad', '\\xae', '\\xaf'],
['\\xb0', '\\xb1', '\\xb2', '\\xb3', '\\xb4', '\\xb5', '\\xb6', '\\xb7', '\\xb8', '\\xb9', '\\xba', '\\xbb', '\\xbc', '\\xbd', '\\xbe', '\\xbf'],
['\\xc0', '\\xc1', '\\xc2', '\\xc3', '\\xc4', '\\xc5', '\\xc6', '\\xc7', '\\xc8', '\\xc9', '\\xca', '\\xcb', '\\xcc', '\\xcd', '\\xce', '\\xcf'],
['\\xd0', '\\xd1', '\\xd2', '\\xd3', '\\xd4', '\\xd5', '\\xd6', '\\xd7', '\\xd8', '\\xd9', '\\xda', '\\xdb', '\\xdc', '\\xdd', '\\xde', '\\xdf'],
['\\xe0', '\\xe1', '\\xe2', '\\xe3', '\\xe4', '\\xe5', '\\xe6', '\\xe7', '\\xe8', '\\xe9', '\\xea', '\\xeb', '\\xec', '\\xed', '\\xee', '\\xef'],
['\\xf0', '\\xf1', '\\xf2', '\\xf3', '\\xf4', '\\xf5', '\\xf6', '\\xf7', '\\xf8', '\\xf9', '\\xfa', '\\xfb', '\\xfc', '\\xfd', '\\xfe', '\\xff']]
有了这两张表,如果我们是接收串口助手或者设备发送过来的数据出现16进制乱码的时候,我们可以通过ascii_to_hex_dict和hex_to_ascii_dict两张字典进行解码。字典打吗如下:
def ascii_to_hex_dict(ascii_data=ascii_data,hex_data=hex_data):#对应的字典编码
ascii_to_hex_dict={}
for i in range(len(ascii_data)):
for j in range(len(ascii_data[0])):
name=ascii_data[i][j]
name_value=hex_data[i][j]
ascii_to_hex_dict[name]=name_value
return ascii_to_hex_dict
def hex_to_ascii_dict(ascii_data=ascii_data,hex_data=hex_data):#对应的字典编码
hex_to_ascii_dict={}
for i in range(len(ascii_data)):
for j in range(len(ascii_data[0])):
name=hex_data[i][j]
name_value=ascii_data[i][j]
hex_to_ascii_dict[name]=name_value
return hex_to_ascii_dict
调用这两个字典后,会有一个返回值,我们只需要向返回值中输入需要解码的单个字符就行。tmp=hex_to_ascii_dict()可以解决串口调试助手发送过来的HEX值出现乱码的问题,在serial采用read接收单个字符,将接收的字符进行data=str(data)[:-1][2:]处理后,打印print(tmp[data])就可以显示正确的HEX值了;另外一个的使用相同。如果数据多,要接收一串数据,自己通过数组将接收到的单个数据append到数组中,并做相应判断来判断当前数据是否是自己想要的数据。
此外,在python中有这么一些数据转换关系函数:ord,hex,int,chr等,根据需要可以自己百度查询一下,或者官网查询使用。下面是16进制,10进制,ASCII,HEX的转换。
def hex_to_dec(string_num):#16进制变成10进制
# print(string_num.upper())#如果输入的16进制中有小写,则通过upper将小写字符换成大写字符
# int('A',16)#将16进制的转化成10进制
result=str(int(string_num.upper(), 16))
return result
def dec_to_hex(string_num):#10进制变成16进制,('10')
base = [str(x) for x in range(10)] + [chr(x) for x in range(ord('A'), ord('A') + 6)]#对应的16进制编码(0-9,A-F)
num = int(string_num)#对输入的数据进行int转化
mid = []
while True:#通过不断的除以16取余数实现10进制转16进制
if num == 0: break#如果商为0则停止循环
num, rem = divmod(num, 16)#对输入的10进制数进行:取整,取余
mid.append(base[rem])#将余数转化成对应的16进制数,并添加到数组中
return ''.join([str(x) for x in mid[::-1]])#字符串拼接并取反,最后返回16进制数
def to_hexstring(data='TEST'):
result=''
for i in data:
tmp=hex(ord(i))[2:].upper()
if len(tmp)==1:
result += str(0)+str(tmp) + ' '
else:
result+=str(tmp)+' '
return result
def to_asciistring(data='54 5174'):
data=data.split(' ')
result=''
datalength=len(data)
for i in range(datalength):
print(data[i])
tmp = chr(int(data[i], 16))
result += tmp
return result