Ubuntu18.04下使⽤Python实现串⼝通信
Ubuntu18.04下使⽤Python实现串⼝通信
前⾔
Ubuntu18.04使⽤Python实现串⼝通信⽅案和之前那篇⽂章 基本相同,毕竟Jeston nano安装的系统便是arm版本的Ubuntu18.04.基本信息
库:Python的serial
硬件:电脑Ubuntu18.04系统、USB转ttl、Jeston nano(做信息接受发送)
配置过程
串⼝调试软件
和Windos⼀样,ubuntu下也有很多串⼝调试软件,这⾥我使⽤的是 CuteCom ;
您可以通过以下命令安装它:
sudo apt-get install cutecom
CuteCom 界⾯:
安装serial库
1
你可以直接在终端下通过pip安装下载
pip install pyserial
或者conda命令 conda install pyserial:
ubuntu怎么安装python
Note:如果您使⽤了anaconda,这⾥要注意终端下的环境是哪⼀个,记得在您计划使⽤的Python环境下使⽤pip命令;2
你也可以直接在Pycharm编辑器下下载,pyserial即是。
3
测试是否安装成功:import serial 不报错就可。通信调试过程
硬件连接:
测试结果:
电脑端:
Jeston nano端:
测试成功。
电脑Ubuntu18.04端代码:
import time
import serial
print("UART Demonstration Program")
print("NVIDIA Jetson Nano Developer Kit")
serial_port = serial.Serial(
port='/dev/ttyUSB0',
baudrate=115200,
bytesize=serial.EIGHTBITS,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
)
# Wait a second to let the port initialize
time.sleep(1)
try:
# Send a simple header
serial_port.write("UART Demonstration Program\r\n".encode())
serial_port.write("NVIDIA Jetson Nano Developer Kit\r\n".encode()) while True:
if serial_port.inWaiting()>0:
data = ad()
print(data)
serial_port.write(data)
# if we get a carriage return, add a line feed too
# \r is a carriage return; \n is a line feed
# This is to help the tty program on the other end
# Windows is \r\n for carriage return, line feed
# Macintosh and Linux use \n
if data =="\r".encode():
# For Windows boxen on the other end
serial_port.write("\n".encode())
except KeyboardInterrupt:
print("Exiting Program")
except Exception as exception_error:
print("Error occurred. Exiting Program")
print("Error: "+str(exception_error))
finally:
serial_port.close()
pass
其他问题
这⾥有⼀个⼩问题
问题描述:
插上usb转ttl,电脑不识别,但是有 /dev/ttyUSB0 这个⽂件

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。