15

I have a device with a USB serial port cable that I'd like to connect to my Raspberry Pi. The chipset for this USB to serial cable it the PL-2303 from Prolific Technology, Inc.

How can I read data from the serial connection of this device using Python?

4
  • 1
    Interesting question. This might help with the USB part: raspberrypi.stackexchange.com/questions/258/…
    – Jivings
    Jun 17, 2012 at 21:33
  • 1
    Is this two questions? Specifically, 'how can I install the drivers for the serial devices?' and 'how can I connect to a serial device within Python?'. The question regarding drivers would be long on this site. However, the Python question might be more specific to a site like StackOverflow. I would assume communicating with a serial device on Python is the same, regardless of the linux-based architecture or the install mechanism.
    – RLH
    Jun 18, 2012 at 13:20
  • There should be no difference between communicating with the on board serial port (UART pins) and a USB serial port. Jun 19, 2012 at 6:39
  • This questions reads: how do I get started with [Configuration \ Litmus test for PL2303 TTL to Serial] (raspberrypi.stackexchange.com/questions/41553/…)? Once the hardware is confirmed, then one can entertain running pyserial to pull data from the serial port
    – gatorback
    Mar 10, 2016 at 0:25

4 Answers 4

7

To talk to a serial device using Python, use the pyserial module. If it is not available in your distribution, it can be installed by getting a copy of the source from the pyserial project page and running "python setup.py install"

Simple examples of using pyserial are available at the short introduction.

The module for the PL-2303 is available by default - see the firmware GitHub repository - when you plug the device in, you should see it fire up in /var/log/messages. I have connected up to an Arduino, and that "just worked" on communications port /dev/ttyUSB0 (different device, driver, chipset, etc., so your mileage may vary).

2
  • Connected the device and I now see /dev/ttyUSB0 so looks like there is a good chance it will "just work".
    – Swinders
    Jun 19, 2012 at 21:46
  • With pyserial installed and using the example miniterm I can see the data from the device :)
    – Swinders
    Jun 19, 2012 at 22:35
4

The PL-2303 is well supported, and it will appear as /dev/ttyUSBx. No drivers are needed. Read it as you would any normal serial port. I haven't used Python, but in C++, I open() it in non-blocking mode, select() to see if there is data to be read, and then do a read().

4

Download pySerial (https://pypi.python.org/pypi/pyserial)

wget http://pypi.python.org/packages/source/p/pyserial/pyserial-2.7.tar.gz?raw=true -O pyserial-2.7.tar.gz
tar -xzf pyserial-2.7.tar.gz
cd pyserial-2.7
sudo python setup.py install

You can check ttyUSB availability with the line

ls -ltr /dev|grep -i ttyUSB

To view the serial output use

tail -f /dev/ttyUSB<NUMBER FROM ABOVE>

To break out crtl+c

Create a testserial.py file paste this code

#!/usr/bin/python
from time import sleep
import serial

# Establish the connection on a specific port
ser = serial.Serial('/dev/ttyUSB0', 9600) 

x = 1 while True:
       print ser.readline() # Read the newest output 
       x += 1
-3

Great tutorial for that :

http://www.digitalmihailo.com/post/usb-programming-with-python-on-linux

A valuable source of information is http://www.lvr.com/usbc.htm Complete

1
  • 1
    Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Also your second link is broken. Sep 6, 2015 at 8:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.