Python Forum

Full Version: Data transfer from Arduino to Pyhton
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi

I am having trouble with getting this code to work. I am working on a project where I want to transfer data from Arduino through a com terminal and having Python grabbing it. The problem is that the serial module does not seem to work. I have installed serial using "pip install serial" and it said that it installed sucessfully, but I am not so sure. Lastly, I do not have anything plugged into the com port or Arduino IDE running. To me this should not matter, but I am a noob :/

By the way, I am watching a youtube video by Paul McWorther, Using an Arduino with Pyhton LESSON 3: Passing Data From Arduino to Pyhton

This is the code so far:
import vpython
import time
import serial

arduinoData = serial.Serial('COM6',9600)

time.sleep(1)

while True: 
    while(arduinoData.inWaiting()==0):
        pass
    dataPacket = arduinoData.readline()
    print(dataPacket)
This is the console message I get back:
Error:
Users/user/Programmering/vPython') Traceback (most recent call last): File C:\ProgramData\Anaconda3\Lib\site-packages\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals) File c:\users\user\programmering\vpython\vpython_test.py:13 arduinoData = serial.Serial('COM6',9600) AttributeError: module 'serial' has no attribute 'Serial'
(Jan-29-2025, 06:52 PM)code_noob Wrote: [ -> ]I have installed serial using "pip install serial"

That's the problem. The "serial" package is not for serial/UART communications, but for serializing some data structures.

The previous video (LESSON 2) showed the installation of the libraries and there he does "pip install pyserial". This is the package you want.
You have installed this: https://pypi.org/project/serial/
But you need this: https://pypi.org/project/pyserial/

pip uninstall serial
pip install pyserial
Another source for error could be a file next to your program called serial.py. The path of your program is the first place, where Python tries to import modules. If there is a serial.py next to your program, you import this instead of pyserial.

Another source of errors: Names of packages you can install via pip, could differ of the package/module name you've to import. You install pyserial, but you've to import serial. You've installed serial and imported serial.