I am trying to get the data from a blood pressure monitor using the python in raspberry pi3. I googled and found a few examples to get the data using python.
My code:
#!/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()
When I ran above code using "sudo python pyusb.py" command, it is returning the following error:
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
If un-commented the line "neo.open()" it is throwing another error:
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.
I have looked at the similar issue here. But there is a issue with overriding of "serial.Serial()" method. I am unable to identify exactly what is the wrong with code above. Can anyone help me with, what I am doing wrong there?
If you have a look at the docs for pyserial, http://pyserial.readthedocs.io/en/latest/shortintro.html#readline it says you must specify a timeout when using readline()
. This is because readline()
waits for a EOL character at the end of each transmission. Try increasing the timeout to 1 using something like this:
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