Jetson Nano 发送和接收串口数据

必要的包

pyserial模块封装了python对串口的访问,为多平台的使用提供了统一的接口。

sudo pip3 install pyserial

程序

import serial 
import time
import binascii
num=[None]*5
ser=serial.Serial("/dev/ttyTHS1",115200) #使用THS1连接串行口,THS1,对应nano上面的物理引脚8 10
def recv():
    print("receive test.......")
    while True:
        for i in range (0,5):
            data=str(binascii.b2a_hex(ser.read(1)))[2:-1]
            num[i]=data     
        if((num[0]=='5a') and (num[1]=='a5')): 
            print(num)
def write():
    print("write test.......")
    while True:      
        flag="5AA50D8203105AA5010000020000"
        ser.write(bytes.fromhex(flag))
        time.sleep(1)        
op=input("enter the operation:")
if op =="0":
    recv()
elif op=="1":
    write()

可能遇到的问题

串口权限不足的问题
解决办法:sudo chmod 777 /dev/ttyTHS1 (修改文件权限可读可写可执行)

提示

Serial.write() 发送的字节!!!!!!!
例如:用Serial.write(97) 发送的过程 【int 97 ——Serial.write() 发送97的ascii码(10010111)——串口监视器接收到ascii码,则会显示对应的字符即a,以十六进制显示的话为(0x61)】

参考

https://blog.csdn.net/qq_36895854/article/details/88925939

你可能感兴趣的:(开发板)