我的串行端口出现了一个奇怪的问题。Ubuntu 更新和重启后,似乎发生了一些变化。
przem@przem:~/Pulpit/bat/scripts$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial("/dev/ttyUSB0", 57600)
>>> ser.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected or multiple access on port?)
>>>
我知道我的设备应该返回“ ”(空符号)的结果,但是我得到的却不是这个结果,而是异常:
“设备报告读取准备就绪但未返回任何数据(设备断开连接或端口上有多个访问?)”
请帮我。
答案1
我遇到了同样的问题。它似乎不仅限于串行端口硬件。您可以使用 socat 创建两个伪终端:
$ socat -d -d pty,raw,echo=0 pty,raw,echo=0
2015/09/30 09:46:18 socat[6296] N PTY is /dev/pts/17
2015/09/30 09:46:18 socat[6296] N PTY is /dev/pts/18
2015/09/30 09:46:18 socat[6296] N starting data transfer loop with FDs [3,3] and [5,5]
您可以同时连接两个设备,例如使用 cu
cu -l /dev/pts/17 -s 115200
和
cu -l /dev/pts/18 -s 115200
并双向发送数据,没有问题。
但连接 Python 2.7 失败,并出现您提到的错误消息
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import serial
>>> ser = serial.Serial("/dev/pts/17", 230400, timeout=0.2)
>>> ser.read()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 460, in read
raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)
此代码在昨天安装 Ubuntu 14.04 更新之前有效。
有任何想法吗?
答案2
我不赞同这样做,但在做出这个改变之后,我不再看到这些错误:
--- serialposix.py.stock 2015-10-03 06:53:45.241261071 -0700
+++ serialposix.py 2015-10-03 06:55:07.481262475 -0700
@@ -457,7 +457,11 @@
# Disconnected devices, at least on Linux, show the
# behavior that they are always ready to read immediately
# but reading returns nothing.
- raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
+ # retrying the read seems to get me past this error:
+ # [ERROR] Can't read from printer (disconnected?) (SerialException): device reports readiness to read but returned no data (device disconnected?)
+
+ #raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
+ pass
read.extend(buf)
return bytes(read)
答案3
这可能是内核错误。请参见此处:https://bugs.launchpad.net/ubuntu/+source/python2.7/+bug/1501240
解决方法似乎是使用内核3.13.0-63-generic
。
您可以在启动时按住 shift 键来更改一次性启动的内核。要使更改持久化,您需要编辑/etc/default/grub
:https://askubuntu.com/questions/262965/grub-timeout-set-to-0-cant-access-grub-menu-anymore?rq=1