Python串口异步通信(串口接收中断)
串口是计算机上一种非常通用的设备通信协议。pyserial模块封装了python对串口的访问,为多平台的使用提供了统一的接口。
安装库
Python要使用串口功能需要导入这两个库:
- pyserial (基本串口功能)
pip3 install pyserial
- pyserial-asyncio (实现异步功能需要这个库)
pip3 install pyserial-asyncio
获取串口名称
使用serial.tools.list_ports.comports()函数会返回一个ListPortInfo类型的列表
import serial
import serial.tools.list_ports
class serial_class:
def get_port(self):
self.port_list = serial.tools.list_ports.comports()
return self.port_list
serial_port =<