serial.Serial.readline() 引发 SerialException,但同一代码在一周前工作

serial.Serial.readline() raises SerialException, but the same code worked a week ago

我有一对应用程序,它们通过串行端口发送文本(仅在一个方向)进行通信。他们已经工作了一段时间了。上周,阅读方停止在我的机器上工作,并在我调用 serial.Serial 对象的 readline() 方法时引发 SerialException相同的代码在另一台机器上工作正常!我唯一能想到的可能导致问题的是我在发生这种情况的前一天晚上安装了一堆系统更新(知道如何在 that? 上查看历史)。我正在使用 Ubuntu 和 Python 2.7.6(见下文),据我所知我在两台机器上安装了相同的 python 包。

我已经编写了两个小示例应用程序来尝试进行故障排除,但在读取方面出现以下错误:

File "./reader.py", line 16, in <module>
  s = port.readline()
File "/usr/local/lib/python2.7/dist-packages/serial/serialposix.py", line 475, in read
  raise SerialException('device reports readiness to read but returned no data (device disconnected or multiple access on port?)')

我使用 "real" 端口还是 "virtual" 端口似乎并不重要,因此可以通过使用以下命令创建两个虚拟端口来重现:

socat -d -d pty,raw,echo=0 pty,raw,echo=0

这是我为故障排除创建的示例 "writer.py":

#!/usr/bin/env python
from __future__ import print_function
import serial

print( 'Opening port' )
port = serial.Serial( port='/dev/pts/5',  # Substitute the correct port here!
                      baudrate=115200,
                      bytesize=serial.EIGHTBITS,
                      parity=serial.PARITY_NONE,
                      stopbits=serial.STOPBITS_ONE,
                      timeout=0.25 )
while True:
    s = raw_input()
    if s:
        port.write( s + '\n' )

效果很好 - 我可以使用 "Serial port terminal" 等应用程序阅读通过端口发送的文本。

这是示例 "reader.py",它在另一台机器上可以找到,但在我的机器上立即失败:

#!/usr/bin/env python
from __future__ import print_function
import serial

print( 'Opening port' )
port = serial.Serial( port='/dev/pts/10',
                      baudrate=115200,
                      bytesize=serial.EIGHTBITS,
                      parity=serial.PARITY_NONE,
                      stopbits=serial.STOPBITS_ONE,
                      timeout=0.25 )
while True:
    s = port.readline()
    if s:
        print( s )

一旦我使用 socat 命令和 运行 "reader.py" 创建虚拟端口,我总是立即得到异常。有什么想法可能会导致此故障发生在我的机器上吗?

系统信息:

~/temp$ uname -a
Linux alonghi-ubu 3.13.0-65-generic #105-Ubuntu SMP Mon Sep 21 18:50:58 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
~/temp$ python --version
Python 2.7.6

Ubuntu 14.04 3.13.0.65 内核中断 python 串行通信。尝试将内核降级到 3.13.0-63 并且串行通信应该像以前一样工作