Python3双向串行通信:读取数据

时间:2017-07-31 09:20:07

标签: python-3.x serial-port pyserial

我正在尝试通过Python3建立双向通信。有一个激光测距仪插入我的一个USB端口,我想发送/接收命令。我有一张可以发送的命令以及它们将返回的内容,因此这部分已经存在。

我需要的是一种实时操作的便捷方式。到目前为止,我有以下代码:

import serial, time

SERIALPORT = "/dev/ttyUSB0"
BAUDRATE = 115200

ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
ser.timeout = None          #block read
ser.xonxoff = False     #disable software flow control
ser.rtscts = False     #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False       #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 0     #timeout for write

print ("Starting Up Serial Monitor")

try:
    ser.open()
except Exception as e:
    print ("Exception: Opening serial port: " + str(e))

if ser.isOpen():
    try:
        ser.flushInput()
        ser.flushOutput()
        ser.write("1\r\n".encode('ascii'))
        print("write data: 1")
        time.sleep(0.5)
        numberOfLine = 0
        while True:
            response = ser.readline().decode('ascii')
            print("read data: " + response)
            numberOfLine = numberOfLine + 1
            if (numberOfLine >= 5):
                break
        ser.close()
    except Exception as e:
        print ("Error communicating...: " + str(e))
else:
    print ("Cannot open serial port.")

所以在上面的代码中我发送“1”,它应该触发激光探测器的“getDistance()”功能,并以mm为单位返回距离。我在Putty上尝试了这个并且它可以工作,返回最多4位数的距离。但是,当我启动上面的Python脚本时,我的输出只有以下内容:

Starting Up Serial Monitor
Exception: Opening serial port: Port is already open.
write data: 1
read data: 

它永远存在。没有读取数据或任何内容。

我错在哪里?

3 个答案:

答案 0 :(得分:3)

显然更简单的代码版本解决了这个问题。

import serial
import time

ser = serial.Serial('/dev/ttyUSB0', 115200, timeout = 1) # ttyACM1 for Arduino board

readOut = 0   #chars waiting from laser range finder

print ("Starting up")
connected = False
commandToSend = 1 # get the distance in mm

while True:
    print ("Writing: ",  commandToSend)
    ser.write(str(commandToSend).encode())
    time.sleep(1)
    while True:
        try:
            print ("Attempt to Read")
            readOut = ser.readline().decode('ascii')
            time.sleep(1)
            print ("Reading: ", readOut) 
            break
        except:
            pass
    print ("Restart")
    ser.flush() #flush the buffer

根据需要输出:

Writing:  1
Attempt to Read
Reading: 20
Restart
Writing:  1
Attempt to Read
Reading:  22
Restart
Writing:  1
Attempt to Read
Reading:  24
Restart
Writing:  1
Attempt to Read
Reading:  22
Restart
Writing:  1
Attempt to Read
Reading:  26
Restart
Writing:  1
Attempt to Read
Reading:  35
Restart
Writing:  1
Attempt to Read
Reading:  36

答案 1 :(得分:0)

在我看来,ser.timeout = None可能会导致问题。 while循环的第一个循环看起来很顺利,但是当程序第二次尝试ser.readline()时,程序会挂起。

有几种方法可以解决这个问题。我首选的方法是指定非None超时,可能是一秒钟。即使设备未发送结束字符,这也允许ser.readline()返回值。

另一种方法是将ser.readline()更改为ser.read(ser.in_waiting)ser.read(ser.inWaiting()),以便返回缓冲区中的所有可用字符。

答案 2 :(得分:0)

尝试此代码

try:
  ser = serial.Serial("/dev/ttyS0", 9600) #for COM3
  ser_bytes = ser.readline()
  time.sleep(1)
  inp = ser_bytes.decode('utf-8')
  print (inp)
except:
  pass