端口

时间:2015-05-20 08:57:44

标签: python python-2.7 pyserial

我有一个来电显示设备,可以处理多达4行,并使用USB串口与计算机通信。使用pySerial我写了下面的代码:

import serial
from time import sleep

s = serial.Serial('/dev/cu.usbserial', 115200)

def event_stream():

    # Modified code from thread reading the serial port
    while True:
        data = s.read()  # Wait forever for anything
        # print tdata
        sleep(1)  # Sleep (or inWaiting() doesn't give the correct value)
        data_left = s.inWaiting()  # Get the number of characters ready to be read
        data += s.read(data_left)  # Do the read and combine it with the first character
        if data:
            if 'CallerID' in data:
                start = data.find(':CallerID:') + 10
                end = data.find(' ', start)
                caller_id = data[start:end]

                start = data.rfind('L', 0, start - 10) + 1
                end = data.find(':', start)
                line = int(data[start:end])

                yield 'event: discovery\ndata: %s\n\n' % json.dumps(dict(line=line, caller_id=caller_id))
            elif 'RingsCount' in data:
                start = data.find(':RingsCount:') + 12
                end = data.find(' ', start)
                ring_count = int(data[start:end])

                start = data.rfind('L', 0, start - 12) + 1
                end = data.find(':', start)
                line = int(data[start:end])
                yield 'event: ringing\ndata: %s\n\n' % json.dumps(dict(line=line, count=ring_count))

for data in event_stream():
    print data

当某人调用第1行或第2行或...时,此代码捕获调用者ID并且一切正常但是当同时调用第1行和第2行时,pySerial引发此异常并且无法捕获调用者ID。

SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)

有没有办法同时处理所有4行?

0 个答案:

没有答案