Unable to access serial port data

Unable to access serial port data
0
#1

I’m trying to get the data from the serial port to which my Arduino is connected.
I’m always getting this error message:
AttributeError: ‘module’ object has no attribute ‘Serial’

I was first using Python 3.7 and did not work, so i went to Python 2.7 and did not work either.
I’m currently using Anaconda (Spyder) for python 3.7 and Canopy for Python 2.7.

Here’s my code:

import serial
ser = serial.Serial(‘COM20’)
ser.flushInput()

while True:
try:
ser_bytes = ser.readline()
decoded_bytes = float(ser_bytes[0:len(ser_bytes)-2].decode(“utf-8”))
print(decoded_bytes)
except:
print(“Keyboard Interrupt”)
break

Regards,

#2

That’s really odd, what happens if you go to the python interpreter and do:

import serial
help(serial)
dir(serial)

Do you by any chance have something else in PATH called serial? It could be that the local module it finds takes precedence over pyserial

#3

Hi,
Thanks for replaying.
Yes i have both serial and pyserial libraries installed and as suggested, i removed serial but the result is still the same.
It is curious that the code was first working and it stopped.
At first, removing the device (Arduino) from the COM port and running the code again fixed the problem, but now it does not do it anymore.

I appreciate your help.

#4

what does dir(serial) or help(serial) output? This might tell us something about what it thinks it’s importing

Alternatively, running the script via the python interpreter in verbose mode (python -v script.py) might tell us something (don’t paste that here though, it’s very long, put it in pastebin or something and link here)

#5

Hi,
When running

There are no errors.
It shows name, description, package contents, data and file location for serial.

#6

Sorry for the slow reply

Do they show the relevant things for the pyserial package?

Does that change when you add those into your script instead of running in the interpreter?

python -v yourscript.py is very hard to read but should also contain the useful information