Issue with python serial: ValueError: Attempting to use a port that is not open
Issue with python serial: ValueError: Attempting to use a port that is not open
我正在尝试使用树莓派 pi3 中的 python 从血压计获取数据。我用谷歌搜索并找到了一些使用 python.
获取数据的示例
我的代码:
#!/usr/bin/python
import serial
neo = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0
)
print("connected to: " + neo.portstr)
#neo.open() #opens port
print "Is port open ? ",neo.isOpen() #returns true?
#ser.write("help\n");
while True:
dataline = neo.readline();
if dataline:
print(dataline), neo.close()
当我 运行 上面的代码使用“sudo python pyusb.py”命令时,它返回以下错误:
connected to: /dev/ttyUSB0
Is port open ? True
None
Traceback (most recent call last):
File "pyusb.py", line 18, in <module>
dataline = neo.readline();
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 443, in read
if not self._isOpen: raise portNotOpenError
ValueError: Attempting to use a port that is not open
如果取消注释行“neo.open()”,则会抛出另一个错误:
connected to: /dev/ttyUSB0
Traceback (most recent call last):
File "pyusb.py", line 13, in <module>
neo.open() #opens port
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 271, in open
raise SerialException("Port is already open.")
serial.serialutil.SerialException: Port is already open.
我看过类似的问题。但是覆盖“serial.Serial()”方法存在问题。我无法确定上面的代码到底出了什么问题。任何人都可以帮助我,我在那里做错了什么?
如果您查看 pyserial 的文档,http://pyserial.readthedocs.io/en/latest/shortintro.html#readline 它说您必须在使用 readline()
时指定超时。这是因为 readline()
在每次传输结束时等待 EOL 字符。尝试使用如下方法将超时增加到 1:
import serial
neo = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
print("connected to: " + neo.portstr)
while True:
dataline = neo.readline();
print dataline
我正在尝试使用树莓派 pi3 中的 python 从血压计获取数据。我用谷歌搜索并找到了一些使用 python.
获取数据的示例我的代码:
#!/usr/bin/python
import serial
neo = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=0
)
print("connected to: " + neo.portstr)
#neo.open() #opens port
print "Is port open ? ",neo.isOpen() #returns true?
#ser.write("help\n");
while True:
dataline = neo.readline();
if dataline:
print(dataline), neo.close()
当我 运行 上面的代码使用“sudo python pyusb.py”命令时,它返回以下错误:
connected to: /dev/ttyUSB0
Is port open ? True
None
Traceback (most recent call last):
File "pyusb.py", line 18, in <module>
dataline = neo.readline();
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 443, in read
if not self._isOpen: raise portNotOpenError
ValueError: Attempting to use a port that is not open
如果取消注释行“neo.open()”,则会抛出另一个错误:
connected to: /dev/ttyUSB0
Traceback (most recent call last):
File "pyusb.py", line 13, in <module>
neo.open() #opens port
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 271, in open
raise SerialException("Port is already open.")
serial.serialutil.SerialException: Port is already open.
我看过类似的问题
如果您查看 pyserial 的文档,http://pyserial.readthedocs.io/en/latest/shortintro.html#readline 它说您必须在使用 readline()
时指定超时。这是因为 readline()
在每次传输结束时等待 EOL 字符。尝试使用如下方法将超时增加到 1:
import serial
neo = serial.Serial(
port='/dev/ttyUSB0',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)
print("connected to: " + neo.portstr)
while True:
dataline = neo.readline();
print dataline