Python的串口

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

猜你喜欢

转载自www.cnblogs.com/Wokky/p/10306651.html