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?

Swinders
  • 255
  • 1
  • 4
  • 11

4 Answers4

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).

Peter Mortensen
  • 2,004
  • 2
  • 15
  • 18
Hexelpdkk
  • 86
  • 1
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
goldilocks
  • 60,325
  • 17
  • 117
  • 234
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().

Peter Mortensen
  • 2,004
  • 2
  • 15
  • 18
James Bennet
  • 291
  • 1
  • 2
-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