Python的串口通信(pyserial)
2019-04-15 本文已影响0人
我爱学python
串口通信是指外设和计算机间,通过数据信号线 、地线、控制线等,按位进行传输数据的一种通讯方式。这种通信方式使用的数据线少,在远距离通信中可以节约通信成本,但其传输速度比并行传输低。串口是计算机上一种非常通用的设备通信协议。pyserial模块封装了python对串口的访问,为多平台的使用提供了统一的接口。
安装:
pip3 install pyserial
测试:
两个CH340 (TTL转串口模块)接入到PC串口上,通过Python进行数据交互:

简单串口程序实现:

获取可用串口列表:

十六进制处理:

其他细节补充:

部分封装:
其中读数据的封装方法并不是很好用,使用的话又得循环接收,这样反而更加复杂了
