33

The Pi is being promoted as an educational device primarily for Python. Inline with this, there have already been some great examples online of people accessing the GPIO from Python.

However, most off-the-shelf (rather than home brew) peripherals have USB interfaces. How does one go about accessing these using the standard Debian/Python card? Is there a Python library available, and a good tutorial?

Darth Vader
  • 4,156
  • 24
  • 43
  • 69
winwaed
  • 1,459
  • 2
  • 15
  • 25
  • 1
    I think this needs to be more specific. What are you trying to do with USB exactly? – Jivings Jun 13 '12 at 21:08
  • This is a very good question, I have always wondered how to make USB devices. – Shane Hudson Jun 13 '12 at 21:08
  • 1
    The specific application I have in mind is a JMRI type system to access a model railroad DCC controller interface (eg. such as the Digitrax PR3). But that is way too specific for this site! The question is aimed at the lower level "how do I talk to any device on USB from Python" - it is implicitly assumed that the programmer has the protocol documentation for the device they wish to program. – winwaed Jun 13 '12 at 21:12
  • I'm going to watch this question carefully. I'm looking at two touch panel displays, both with simple ASCII command sets and USB interfaces, which I want to try with the RasPi. – Ray Depew Jun 13 '12 at 22:15

2 Answers2

11

USB is at heart just a serial communication device. How the data is communicated is standard, but what that data consists of is device dependant. Your controller manufacturer will have invented an api that consists of commands and messages of the form 'do this', or 'this is happening'

If you know this api you should be able to communicate. Two possibly helpful links are

https://walac.github.io/pyusb/

https://web.archive.org/web/20130327005316/http://www.digitalmihailo.com/post/usb-programming-with-python-on-linux

A valuable source of information is USB Complete

Harry Tsai
  • 103
  • 3
David Sykes
  • 1,435
  • 3
  • 19
  • 28
  • Thanks - yes that is the kind of level I was aiming for my question. Protocols / product APIs for specific peripherals are other questions, or a matter for the vendor's documentation. – winwaed Jun 17 '12 at 22:18
2

Pasting this here for anyone else on the bunny trail that I was today (Oct 5, 2014). NOTE: this is from a very inexperienced PoV so, this may be obvious to many of you, but it wasn't for me and took awhile to figure out.

I'm trying to interface a simple laser barcode scanner to a RaspberryPi B+ via USB, literally no experience with Linux/RBPi etc. Eventually Google searching brought me to this page.

I got to the answer provided by David Sykes on Jun 15, 2012 with the PyUSB reference and from there I found ADAFruit's Installing Python & PyUSB, as part of their hacking of a Microsoft Kinect..

I feel I could have gotten through it pretty quickly if it were just on my PC. But doing this through a RaspberryPi was really throwing me for a loop. If you read through that guide, you'll hit the same roadblock I did where you can't download stuff and you can't simply mash in the things they say without the files. So how do you get the files if you can't download them? [Maybe there is a way to download these files, but on my Pi I kept getting an Error 400 on source forge and Midori wouldn't download the Tar or Zip files.]

Referencing Walac's PyUSB, you can enter to first part into the LXTerminal to get libusb but then the next command given assumes you have the files for PyUSB and that you can locate them through the directory.

So HERE are my altered steps to get LibUSB and PyUSB installed on a RaspberryPi:

Order of commands I entered into the LXTerminal:

sudo apt-get install python libusb-1.0

Confirm with Y

sudo apt-get install python-pip

then

sudo pip install --upgrade pyusb

NOTE: some of this may already be on your RBPi depending on what you've done before this, but doing the commands again won't hurt it, it simply told me that the file was up to date.

The last command "--upgrade pyusb" gave me the following:

Downloading/unpacking pyusb
Downloading pyusb-1.0.0b1.tar.gz (44Kb): 44Kb downloaded
Running setup.py egg_info for package pyusb

Installing collected packages: pyusb
Running setup.py install for pyusb

Successfully installed pyusb
Cleaning up...

SO, I think it's all good now and I can go back to my tutorials.

Again, I'm using a RaspberryPi B+, standard NOOBS install.. I've mostly referenced sources such as ADAFRUIT's LEARN system that was made with mostly the Model B, but everything I've done so far is working on the B+.

Hopefully that info will help anyone else who's gotten stuck at that same point.