要使用 python 中的串口, 可以下载 pywin32-224-cp36-cp36m-win_amd64.whl 去安装或者 pip install 去安装.
调试下来, 有一点很不爽, 读取 read() 数据的 timeout 时间最小单位是秒, 这对应很频繁的读取使用, 很浪费时间. 如果不设置这个时间我在有些串口设备上调试发现 read 一定读满给定的字节数才能返回. 比如: Uart.read(512), 一定要读到 512 字节才能返回, 如果这个时间单位能改成毫秒那就最好了.
贴一段简单的写读操作, 作为一个记录.
- import serial
- uart = serial.Serial('COM31',115200,serial.EIGHTBITS,serial.PARITY_NONE,serial.STOPBITS_ONE,timeout=1,write_timeout=1)
- uart.open()
- uart.rts = 1
- uart.dtr = 1
- cmd = b'ATI'+b'\x0D'+b'\x0a'
- write_len = uart.write(cmd)
- rsp_str = uart.read(512)
- print("rsp:%s"%(rsp_str.decode('utf-8')))
- uart.close()
来源: http://www.bubuko.com/infodetail-2930646.html