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
  • 1
    Interesting question. This might help with the USB part: http://raspberrypi.stackexchange.com/questions/258/how-to-program-a-usb-device-with-debian-python – Jivings Jun 17 '12 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 '12 at 13:20
  • There should be no difference between communicating with the on board serial port (UART pins) and a USB serial port. – Alex Chamberlain Jun 19 '12 at 06:39
  • This questions reads: how do I get started with [Configuration \ Litmus test for PL2303 TTL to Serial] (http://raspberrypi.stackexchange.com/questions/41553/usb-serial-port-configuration)? Once the hardware is confirmed, then one can entertain running pyserial to pull data from the serial port – gatorback Mar 10 '16 at 00:25

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
  • 58,859
  • 17
  • 112
  • 227
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