Python serial error on Raspberry Pi

From a vanilla install, attempting to run the code

def init(self):
“”“Constructor.”“”
mavutil.set_dialect(“ardupilotmega”)
# Create the connection
self.master = mavutil.mavlink_connection(‘/dev/serial/by-id/usb-3D_Robotics_PX4_FMU_v2.x_0-if00’)
# Wait a heartbeat before sending commands
self.master.wait_heartbeat()

# Set all rc chans pwm to PWM_NEUTRAL_CENTER before arming:
self.rc_channels = [ self.PWM_NEUTRAL_CENTER ] * 8
self.master.mav.rc_channels_override_send(self.master.target_system,
                                          self.master.target_component,
                                          *self.rc_channels)

yields this following result:

(venv) pi@raspberrypi:~/projects/underquad/controller$ python
./controller.py Traceback (most recent call last):
File “./controller.py”, line 244, in
controller = UnderQuadController()
File “./controller.py”, line 79, in init
self.master = mavutil.mavlink_connection(‘/dev/serial/by-id/usb-3D_Robotics_PX4_FMU_v2.x_0-if00’)
File “/home/pi/projects/underquad/controller/venv/local/lib/python2.7/site-packages/pymavlink/mavutil.py”, line 1251, in mavlink_connection
return mavserial(device, baud=baud, source_system=source_system, source_component=source_component, autoreconnect=autoreconnect, use_native=use_native)
File “/home/pi/projects/underquad/controller/venv/local/lib/python2.7/site-packages/pymavlink/mavutil.py”, line 786, in init
import serial
ImportError: No module named serial

This means that the python instance is not recognizing the serial library that was installed with apt-get.

After running

pip install serial

And re-running the code, we get the following error.

Traceback (most recent call last):
File “./controller.py”, line 244, in
controller = UnderQuadController()
File “./controller.py”, line 79, in init
self.master = mavutil.mavlink_connection(‘/dev/serial/by-id/usb-3D_Robotics_PX4_FMU_v2.x_0-if00’)
File “/home/pi/projects/underquad/controller/venv/local/lib/python2.7/site-packages/pymavlink/mavutil.py”, line 1251, in mavlink_connection
return mavserial(device, baud=baud, source_system=source_system, source_component=source_component, autoreconnect=autoreconnect, use_native=use_native)
File “/home/pi/projects/underquad/controller/venv/local/lib/python2.7/site-packages/pymavlink/mavutil.py”, line 795, in init
self.port = serial.Serial(self.device, 1200, timeout=0,
AttributeError: ‘module’ object has no attribute ‘Serial’

Is it a vanilla install, or have you done apt-get install python-serial and apt-get install pymvalink?

Where did you get your image? Try using the one provided in the resources/downloads section of ardusub.com.

Hey Jacob, I’m working with Tim on this. The installation was the latest version from ardusub. I tried removing the built-in library and installing the PyPI version as a super user and it caused the “no attribute ‘Serial’” error in the mavlink startup script. After removing the pip version and reinstalling python-serial with apt-get, the mavlink script worked again.

However, when we are writing our own code, we cannot seem to get the proper version of python-serial installed that will work with mavlink. After creating a virtualenv, python cannot find the serial library that is being used by the mavlink scripts. Again, after pip installing, we get the “no attribute ‘Serial’” problem.

Any thoughts on why the PyPI version of serial differs from the apt-get version?

Annnnddd I think I figured it out! It looks like there’s a library called “serial” in PyPI, but it is not the python-serial library. By running “pip install pyserial” I was able to get the correct version installed. So confusing!

1 Like