0

I need some help troubleshooting an Attribute Error in my Python code.

I am working with a serial device connected by a /dev/ttyUSB0 to a Raspberry Pi 2B. My goal for this code is to read live serial data continuously from the serial device and separate out commands delineated by an '@' symbol instead of /n or /r.

This project is still in its early stages but from some quick research I've done the pyserial attribute 'read_until' would work perfectly as its backbone. This would allow me to read in one byte at a time and stop at an '@' symbol, and afterwards process the data before moving on to the next portion of serial data.

For some reason I get an Attribute Error when I try to use 'read_until' claiming that the 'Serial' object does not have this function. Below is the test code I am using and the error it spit back out:

import serial

gc = serial.Serial('/dev/ttyUSB0', baudrate = 230400)
print(gc.name)

def getCommand():
    gcData = gc.read_until('@', 1).decode('ascii')
    print(gcData)

getCommand()

gc.close()

And here is the output that this code gave me:

>>>
/dev/ttyUSB0
Traceback (most recent call last):
  File "/home/pi/Python/GC/serialRead1.py", line 10, in <module>
    getCommand()
  File "/home/pi/Python/GC/serialRead1.py", line 7, in getCommand
    gcData = gc.read_until('@', 1).decode('ascii')
AttributeError: 'Serial' object has no attribute 'read_until'
>>>

The frustration behind this is that 'read_until' should have been imported along with the serial library. Perhaps I am missing something quite simple, but it would be very helpful if I could use this attribute. Most other questions about 'read_until' are referencing the 'telnetlib' which as far as I know is not the same as what PySerial has.

Here is a link to the website where I found the documentation saying that 'read_until' should belong to PySerial: https://pyserial.readthedocs.io/en/latest/pyserial_api.html

Just so you are aware, I am very new to programming in general. Some of the jargon that seasoned programmers like yourselves use might go over my head and so I may have no idea how to perform the solutions you have. I ask for your patience as I learn more about programming and Python in general.

Thank you very much for your time!

  • which Python version are you using ?? – Mohammad Zain Abbas Mar 17 '20 at 15:20
  • I am using Python 3.4.2 on the Raspberry Pi 2B. – Sgt_Baconbits Mar 17 '20 at 15:23
  • Please run the following command in the terminal and add the result to your post: `python3 -c 'import serial; print(serial.__version__)'` – tttapa Mar 17 '20 at 16:30
  • @tttapa I used the command you provided verbatim in the terminal and here is the result it gave me: `Traceback (most recent call last): File "", line 1, in Attribute Error: 'module' object has no attribute '__version__'` – Sgt_Baconbits Mar 17 '20 at 16:43
  • That sounds like a really old version. [The `__version__` attribute was added in 2016](https://github.com/pyserial/pyserial/commit/fa3abd656e20acc525f5b8e3002100bda86d201d). How did you install PySerial? You can try `python3 -c 'import serial; print(serial.VERSION)'` instead. – tttapa Mar 17 '20 at 18:22
  • @tttapa That worked. It says it is at version 2.6. I installed it initially with `sudo apt-get install python-serial` I found it interesting that you said it was a really old version. I looked at updated ways to install pip, and tried `sudo python3 -m pip install --upgrade pyserial` It isn't giving me the Attribute Error anymore. Would I be jumping too far ahead to say this is fixed? – Sgt_Baconbits Mar 17 '20 at 18:56

1 Answers1

0

It seems you have PySerial version 2.6 installed. This version was released in 2011, while the read_until method was added in 2015.

The easiest way to install the latest version (as you've discovered) is to use pip:

sudo apt install python3-pip
python3 -m pip install --user pyserial

Note that using pip with sudo is bad practice.
It's best to use a virtual environment or to use the --user flag, as demonstrated above.

tttapa
  • 1,397
  • 12
  • 26