python serial 模块使用

  • python3 开始, python 加入了 serial 操作串口的模块

  • 模块的引用

    import serial
  • 串口的申请

    serial_com1 = serial.Serial("/dev/ttyO2", 9600, timout=5)
  • 串口数据的写入:

    serial_com1.write("test_chr".encode())
  • 串口数据的读取:

    msg = serial_com1.read(64)
    print(msg.decode())
  • 串口模块内,传输的都是字节数据,所以,在写入数据的时候,不能直接写字符串数据

原文地址:https://www.cnblogs.com/chenfulin5/p/10025895.html