licheepi 4a串口通信(usb 串口)
博主使用usb串口原因是博主的是内测版本,虽然购买了1.8转3v3的usb转ttl模块,但是板载的UART1还是1.8V电平。 所以本博客记录博主使用usb转串口进行串口通信。
博主环境:debian20231023镜像 lichee pi 4a 8+8G
(1)安装pyserial库并查看usb串口模块端口
- 安装pyserial库
pip3 install pyserial
pip list查看
博主终端输出信息:可以看到pyserial已经安装上去了
(ort) root@lpi4a:/home/sipeed/Desktop/udisk/test_py# pip list
Package Version
---------------------- --------------------------------
certifi 2023.7.22
charset-normalizer 3.3.1
coloredlogs 15.0.1
cycler 0.12.1
filelock 3.12.4
flatbuffers 23.5.26
fonttools 4.43.1
hhb-onnxruntime-th1520 2.6.0
humanfriendly 10.0
idna 3.4
Jinja2 3.1.2
kiwisolver 1.4.4
loguru 0.7.0
MarkupSafe 2.1.3
matplotlib 3.7.2.dev0+gb3bd929cf0.d20230630
mpmath 1.3.0
networkx 3.2
numpy 1.25.0
opencv-python 4.5.4+4cd224d
packaging 23.2
Pillow 9.5.0
pip 23.0.1
protobuf 4.24.4
psutil 5.9.5
pycocotools 2.0.6
pyparsing 3.1.1
pyserial 3.5
python-dateutil 2.8.2
requests 2.31.0
setuptools 66.1.1
setuptools-scm 8.0.4
six 1.16.0
sympy 1.12
tabulate 0.9.0
torch 2.0.0a0+gitc263bd4
torchvision 0.15.1a0
tqdm 4.65.0
typing_extensions 4.8.0
urllib3 2.0.7
- 查看usb串口模块端口
(博主的串口模块那么就是/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0)
root@lpi4a:~# ls /dev/serial/by-id/
usb-1a86_USB_Serial-if00-port0
(2)程序与实测
# -*- coding: utf-8 -*-
import serial
import time
import threading
# 打开串口
serialPort = "/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0" # 串口
baudRate = 115200 # 波特率
ser = serial.Serial(serialPort, baudRate, timeout=0.5)
# ser = serial.Serial('/dev/serial/by-id/usb-1a86_USB_Serial-if00-port0', 115200)
def receive_message_wxw():
global ser
while True:
data_count = ser.inWaiting()
if data_count != 0:
recv = ser.read(ser.in_waiting).decode("utf-8")
print(recv)
tmp1 = threading.Thread(target=receive_message_wxw)
tmp1.start()
# 收发数据
while True:
# str = input("请输入要发送的数据(非中文)并同时接收数据: ")
str="test message\n"
ser.write((str).encode('utf-8'))
time.sleep(1);