技巧一:还原从串口接收的HEX字符串
![](https://img-blog.csdnimg.cn/202007221059491.png)
- 串口读来的bytes类型HEX数据转换成HEX类型字符串转换函数。
#ByteToHex的转换,返回数据16进制字符串
def ByteToHex( bins ):
return ''.join( [ "%02X" % x for x in bins ] ).strip()
# 还原从串口接收的HEX字符串
# ESP、ESP8266、STM32平台通用
import time
from machine import UART, Pin
from ubinascii import *
uart = UART(2, baudrate=115200, rx=16, tx=17, timeout=10)
#ByteToHex的转换,返回数据16进制字符串
def ByteToHex( bins ):
return ''.join( [ "%02X" % x for x in bins ] ).strip()
while True:
#time.sleep(0.01)
if (uart.any()):
temp = uart.read(1)
print(temp)
temp = ByteToHex(temp)
print(temp)
- 串口助手发送16进制010400000008f1cc,函数接收截图。(用处自己体会)
![](https://img-blog.csdnimg.cn/202007221203344.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5MDExMjQ5,size_16,color_FFFFFF,t_70)