Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

USB in python

4 views
Skip to first unread message

Astan Chee

unread,
Jan 22, 2009, 2:38:45 AM1/22/09
to pytho...@python.org
Hi,
Im trying to write a program for my USB device and I'm thinking of using
python to do this. The USB device is of my own making and it is
activated when one of the two data pins of the USB is given about 5V (or
similar to whatever the power pin is getting). Now I'm confused to if
the software to activate this can actually be written and how do I do
it? Any examples? I've seen pyUSB but it doesn't give me control over
the hardware and how much power is going through the data pins.
Thanks for any help.
Cheers
Astan

Diez B. Roggisch

unread,
Jan 22, 2009, 5:15:58 AM1/22/09
to
Astan Chee wrote:

This doesn't sound like an USB-device to me. The USB-standard defines a
serial line protocol, where of course the state of the TX-line is set to
whatever the protocol dictates. And as not all of the things put out on the
line are of the programmer's own doing, it will be on and off all the time.

And no piece of software is going to control that - it's already implemented
in the USB-host-controllers inside your computer.

Even with a considerably more primitive RS233 line you'd not stand a chance
with that design.

Unless I'm not getting something here.

I've created USB-devices based on the AT90USB1287 from Atmel, and there you
can of course use the libusb (and it's wrapping PyUSB) to acccess the
device, and e.g. drive a single port-pin of the uC high - to activate,
whatever you want to activate.

Diez

Tino Wildenhain

unread,
Jan 22, 2009, 5:21:00 AM1/22/09
to Astan Chee, pytho...@python.org
Astan Chee wrote:
> Hi,
> Im trying to write a program for my USB device and I'm thinking of using
> python to do this. The USB device is of my own making and it is
> activated when one of the two data pins of the USB is given about 5V (or
> similar to whatever the power pin is getting). Now I'm confused to if
> the software to activate this can actually be written and how do I do
> it? Any examples? I've seen pyUSB but it doesn't give me control over
> the hardware and how much power is going through the data pins.
> Thanks for any help.

Your approach doesn't sound right to me. I don't believe you can control
the PINs of the USB host interface directly. You need to configure the
host controller - OS is giving you interfaces and abstration for the
different device types. All you need to do is to implement the end
points as per spec in your hardware and write handler for your
hardware's USB ids.

Regards
Tino

Brian Allen Vanderburg II

unread,
Jan 22, 2009, 3:47:46 PM1/22/09
to pytho...@python.org
astan...@al.com.au wrote:
> Hi,
> Im trying to write a program for my USB device and I'm thinking of
> using python to do this. The USB device is of my own making and it is
> activated when one of the two data pins of the USB is given about 5V
> (or similar to whatever the power pin is getting). Now I'm confused to
> if the software to activate this can actually be written and how do I
> do it? Any examples? I've seen pyUSB but it doesn't give me control
> over the hardware and how much power is going through the data pins.
> Thanks for any help.
> Cheers
> Astan
> --
> http://mail.python.org/mailman/listinfo/python-list
I don't think you can actually control the USB port's data lines like
this. I've been searching for some ideas as well for some small
hobbyist projects I'm thinking about, and it seems that either the EZUSB
chip or maybe even easier the FTDI chips might be the way to go. I
think the FTDI chip is basically a USB-to-RS232 converter and there is a
libftdi that is built on top of libusb I think. Anyway for my design
(if I ever get around to it) I'm going to create a library in C that
uses the hardware and then I can create a Python wrapper around that
library.

Brian Vanderburg II

Astan Chee

unread,
Jan 22, 2009, 8:50:19 PM1/22/09
to pytho...@python.org
Diez B. Roggisch wrote:
> Astan Chee wrote:
>
>
>> Hi,
>> Im trying to write a program for my USB device and I'm thinking of using
>> python to do this. The USB device is of my own making and it is
>> activated when one of the two data pins of the USB is given about 5V (or
>> similar to whatever the power pin is getting). Now I'm confused to if
>> the software to activate this can actually be written and how do I do
>> it? Any examples? I've seen pyUSB but it doesn't give me control over
>> the hardware and how much power is going through the data pins.
>>
> Unless I'm not getting something here.
>
>
>
Hi,
Thanks for all the responses but I forgot to mention that I have very
little hardware understanding (at least in english) and the device
itself it very simple and only needs about 5V power to be active. The
problem here is that I want to control when the device is active using a
computer so I thought USB might be a good choice since its simple (but
didn't turn out to be). I'm open to any other suggestions on how I might
achieve this hardware and software-wise (as in what interface should I
use, etc). Also I'm trying to stay away from (complex) micro controllers.
Any ideas?
Thanks again
Astan

Grant Edwards

unread,
Jan 22, 2009, 10:26:01 PM1/22/09
to
On 2009-01-23, Astan Chee <astan...@al.com.au> wrote:

> Thanks for all the responses but I forgot to mention that I have very
> little hardware understanding (at least in english) and the device
> itself it very simple and only needs about 5V power to be active. The
> problem here is that I want to control when the device is active using a
> computer so I thought USB might be a good choice since its simple (but
> didn't turn out to be).

USB is definitely not simple. USB is exremely complicated (and
rather badly designed, IMO).

> I'm open to any other suggestions on how I might achieve this
> hardware and software-wise (as in what interface should I
> use, etc). Also I'm trying to stay away from (complex) micro
> controllers. Any ideas? Thanks again Astan

Standard IBM PC parallel printer ports offer a bunch of 5V I/O
lines that are fairly simple to control in software.

--

Brian Allen Vanderburg II

unread,
Jan 23, 2009, 12:53:25 AM1/23/09
to astan...@al.com.au, pytho...@python.org
astan...@al.com.au wrote:
> Hi,

> Thanks for all the responses but I forgot to mention that I have very
> little hardware understanding (at least in english) and the device
> itself it very simple and only needs about 5V power to be active. The
> problem here is that I want to control when the device is active using
> a computer so I thought USB might be a good choice since its simple
> (but didn't turn out to be). I'm open to any other suggestions on how
> I might achieve this hardware and software-wise (as in what interface
> should I use, etc). Also I'm trying to stay away from (complex) micro
> controllers.
> Any ideas?
> Thanks again
> Astan
> --
> http://mail.python.org/mailman/listinfo/python-list
How about a different interface? From what I have read the parallel
port is a bit easier to program. I think you can control the data lines
of the parallel port though. There is also a python wrapper for it on
the pyserial web site (pyparallel maybe?)

If you don't have a built-in parallel port then there are those USB to
serial/parallel converters.


Brian A. Vanderburg II

Diez B. Roggisch

unread,
Jan 23, 2009, 1:58:48 AM1/23/09
to
Astan Chee schrieb:

Others suggested the parallel port. It is the natural choice for such
things, with two caveats:

- it is legacy, and thus often not available on modern hardware,
especially on mobile ones. So if you want it be prepared to additionally
buy a usb2parallel-adapter.

- it's electrical specs aren't as robust I fear. USB allos up to 500mA
to be drawn, and shouldn't break if you try more & fail (albeit, that
might be something that isn't true all the time). So you can draw quite
a bit of current from it (the stupid USB-cup-warmers are an example of
that). I have often had broken parallel-ports, and I think the reason is
that they *ARE NOT* specified to drive anything - they only provide
low-current control-lines. So whatever you design, you need a second
power-source then.

All in all, using a USB-controller is IMHO the best solution. The
AT90USBKey is a low-cost evaluation-board. ATMEL provides quite a bit of
example-code, and there is other FOSS available.

I have to admit though that the whole USB-topic isn't the easiest thing.


Diez

Astan Chee

unread,
Jan 23, 2009, 2:24:37 AM1/23/09
to pytho...@python.org
Diez B. Roggisch wrote:
> Others suggested the parallel port. It is the natural choice for such
> things, with two caveats:
>
> - it is legacy, and thus often not available on modern hardware,
> especially on mobile ones. So if you want it be prepared to additionally
> buy a usb2parallel-adapter.
>
> - it's electrical specs aren't as robust I fear. USB allos up to 500mA
> to be drawn, and shouldn't break if you try more & fail (albeit, that
> might be something that isn't true all the time). So you can draw quite
> a bit of current from it (the stupid USB-cup-warmers are an example of
> that). I have often had broken parallel-ports, and I think the reason is
> that they *ARE NOT* specified to drive anything - they only provide
> low-current control-lines. So whatever you design, you need a second
> power-source then.
>
> All in all, using a USB-controller is IMHO the best solution. The
> AT90USBKey is a low-cost evaluation-board. ATMEL provides quite a bit of
> example-code, and there is other FOSS available.
>
> I have to admit though that the whole USB-topic isn't the easiest thing.
>
>
Yeah, I forgot to mention that the device is requires about 70-80mA and
the parallel port (according to the spec) only provides 1mA. Thats why I
was looking into the USB solution.
Thanks for the suggestion though. Also, yes, the device is rather mobile
and that is why it is powered by the computer/laptop but legacy isn't
really an issue for me I guess.
Cheers
Astan

Diez B. Roggisch

unread,
Jan 23, 2009, 2:30:18 AM1/23/09
to
Astan Chee schrieb:

If all you need is on-off - why can't you just use a switch?

Diez

Tim Roberts

unread,
Jan 25, 2009, 11:46:44 PM1/25/09
to

Sorry, but you have NOT created a USB device, and I sincerely hope you do
not try to plug it in to a real USB port.

Despite the "serial" in the name, USB is much more than just a pair of
wires, like RS-232. USB is a standard protocol-based bus, like Ethernet,
and anything that is plugged into it must follow the protocol. You don't
get a "voltage", you get a 480 MHz differential digital signal train. The
wires are controlled by a USB host controller. You cannot override its
behavior.

You might be able to achieve your goal by using a parallel port, or by
using a USB-to-serial port adapter, or by using one of the many simple and
affordable USB experimenter's kits in the world.
--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

Astan Chee

unread,
Jan 26, 2009, 12:12:16 AM1/26/09
to pytho...@python.org
Tim Roberts wrote:
> Sorry, but you have NOT created a USB device, and I sincerely hope you do
> not try to plug it in to a real USB port.
>
Sorry, by USB device, I meant a device that is powered/activated by a
bunch of wires that I want to control using a computer and since I had a
spare USB jack lying around, I used that instead. But so far I haven't
tried it, nor will try it if it wont work properly. Yes, it is not a
proper USB device, because I didnt build it to specifically interface
with the USB port; but I had to start somewhere. Also, the device
requires more power than the standard parallel port can give.
Anyway, it looks like the easiest solution for my case is a microcontroller

Lie Ryan

unread,
Jan 26, 2009, 3:21:26 AM1/26/09
to pytho...@python.org
On Fri, 23 Jan 2009 18:56:38 +1100, Astan Chee wrote:

> Diez B. Roggisch wrote:
>>>
>>>
>> If all you need is on-off - why can't you just use a switch?
>>
>>
>>

> Because I want to control the on-off the device using a computer and
> write software for it (which I am confident I can do if I had references
> to how the wrappers to said interface). Cheers
> Astan.
>

How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little
Hardware Experience). High pitched sound (or anything in sound-ology that
means high voltage) means the device is on and low pitched sound off. The
device will need an additional transistor to separate low voltage from
the high voltage. I don't know how much power can be pulled from jack
out, but for a home brewn device it is still feasible to draw power from
USB and signal from jack out.

Steve Holden

unread,
Jan 26, 2009, 9:22:57 AM1/26/09
to pytho...@python.org
Lie Ryan wrote:
> On Fri, 23 Jan 2009 18:56:38 +1100, Astan Chee wrote:
>
>> Diez B. Roggisch wrote:
>>>>
>>>>
>>> If all you need is on-off - why can't you just use a switch?
>>>
>>>
>>>
>> Because I want to control the on-off the device using a computer and
>> write software for it (which I am confident I can do if I had references
>> to how the wrappers to said interface). Cheers
>> Astan.
>>
>
> How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little
> Hardware Experience). High pitched sound (or anything in sound-ology that
> means high voltage) means the device is on and low pitched sound off. The
> device will need an additional transistor to separate low voltage from
> the high voltage. I don't know how much power can be pulled from jack
> out, but for a home brewn device it is still feasible to draw power from
> USB and signal from jack out.
>
Congratulations. You just invented the modem.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Steve Holden

unread,
Jan 26, 2009, 9:25:28 AM1/26/09
to pytho...@python.org
Astan Chee wrote:

> Tim Roberts wrote:
>> Sorry, but you have NOT created a USB device, and I sincerely hope you do
>> not try to plug it in to a real USB port.
>>
> Sorry, by USB device, I meant a device that is powered/activated by a
> bunch of wires that I want to control using a computer and since I had a
> spare USB jack lying around, I used that instead. But so far I haven't
> tried it, nor will try it if it wont work properly. Yes, it is not a
> proper USB device, because I didnt build it to specifically interface
> with the USB port; but I had to start somewhere. Also, the device
> requires more power than the standard parallel port can give.
> Anyway, it looks like the easiest solution for my case is a microcontroller

In which case the Arduino may be a good place to start. The recent
duemilanove boards use USB to communicate with the host, and so the USB
port should be available to the microcontroller. We areg etting some way
away from Python now, of course ...

Brian Allen Vanderburg II

unread,
Jan 26, 2009, 9:55:01 AM1/26/09
to pytho...@python.org
astan...@al.com.au wrote:

> Tim Roberts wrote:
>> Sorry, but you have NOT created a USB device, and I sincerely hope
>> you do
>> not try to plug it in to a real USB port.
>>
> Sorry, by USB device, I meant a device that is powered/activated by a
> bunch of wires that I want to control using a computer and since I had
> a spare USB jack lying around, I used that instead. But so far I
> haven't tried it, nor will try it if it wont work properly. Yes, it is
> not a proper USB device, because I didnt build it to specifically
> interface with the USB port; but I had to start somewhere. Also, the
> device requires more power than the standard parallel port can give.
> Anyway, it looks like the easiest solution for my case is a
> microcontroller
> --
> http://mail.python.org/mailman/listinfo/python-list
I've played around in this area a little bit. Microcontrollers still
require hardware programming and for simple circuits I think it is
overkill. If you want to use USB then you may be able to use the FTDI
chips. They have both serial (FT232) and parallel (FT245) chips and are
quite cheap. They are surface mount devices though, but you can get a
kit that includes USB port, the chip already connected to a board with a
DIP plug and some essential circuits. libftdi, which runs on top of
libusb, can control both of these and they require no programming
(unless you want to change the USB configuration settings such as vendor
ID, etc, from the default value)

This is the FT245 chip which is basically USB-to-Parallel.

Chips: http://www.ftdichip.com/Products/FT245R.htm
Kit/Board: http://www.ftdichip.com/Products/EvaluationKits/UM245R.htm

The spec sheet for the board seems quite simple. It's pin out is
similar to that of a parallel port in that you have your data lines
DB0-DB7, etc. It can also be connected in bus-powered configuration
(~100mA) or self-powered configuration. The kit is more expensive than
the chip itself, but probably easier especially if you don't have any
experience with surface mount.

You could build it into your device. You could also create a simple
switch box out of it to control external devices, maybe connecting each
of the data lines to relays to turn on/off eight devices, etc.

Brian Vanderburg II

Grant Edwards

unread,
Jan 26, 2009, 12:08:48 PM1/26/09
to
On 2009-01-26, Lie Ryan <lie....@gmail.com> wrote:

> How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little
> Hardware Experience). High pitched sound (or anything in sound-ology that
> means high voltage) means the device is on and low pitched sound off.

1) Pitch has nothing to do with voltage. A high-pitch sound
and a low pitch sound can have the exact same voltage.

2) The OP's device requires quite a bit of power. There is
almost no power available from the line-out jack, and the
voltage is limited to about 1V. If his sound card has a
power-amp (none do these days), he might be able to get a
usable amount of power.

> The device will need an additional transistor to separate low
> voltage from the high voltage.

He'll need more than a transistor. He needs a power supply,
some sort of rectifier/detector, and a comparitor. It would be
more interesting to use notch filters to detect different
frequencies so that you could have multiple output "bits".

> I don't know how much power can be pulled from jack out,

Almost none, and what's there is very low voltage.

> but for a home brewn device it is still feasible to draw power
> from USB and signal from jack out.

It would probably be easier to buy a USB-parallel port chip.
Then he's got power from the USB bus and something like 14
parallel I/O pins he can control. Alternatively A USB-serial
chip will provide 2 outputs and 4 inputs.

--
Grant Edwards grante Yow! Not SENSUOUS ... only
at "FROLICSOME" ... and in
visi.com need of DENTAL WORK ... in
PAIN!!!

Дамјан Георгиевски

unread,
Jan 26, 2009, 5:09:28 PM1/26/09
to
>> Sorry, by USB device, I meant a device that is powered/activated by a
>> bunch of wires that I want to control using a computer and since I
>> had a spare USB jack lying around, I used that instead. But so far I
>> haven't tried it, nor will try it if it wont work properly. Yes, it
>> is not a proper USB device, because I didnt build it to specifically
>> interface with the USB port; but I had to start somewhere. Also, the
>> device requires more power than the standard parallel port can give.
>> Anyway, it looks like the easiest solution for my case is a
>> microcontroller
>
> In which case the Arduino may be a good place to start. The recent
> duemilanove boards use USB to communicate with the host, and so the
> USB port should be available to the microcontroller. We areg etting
> some way away from Python now, of course ...

I second that.
Arduino to microcontrolers, is what Linux was to kernels back in the
90ties (now it's mainstream), or maybe what python is/was to Java.

--
дамјан ( http://softver.org.mk/damjan/ )

() ASCII Ribbon Campaign
/\ Keep it simple!

Astan Chee

unread,
Jan 26, 2009, 8:58:30 PM1/26/09
to pytho...@python.org

Brian Allen Vanderburg II wrote:
> This is the FT245 chip which is basically USB-to-Parallel.
>
> Chips: http://www.ftdichip.com/Products/FT245R.htm
> Kit/Board: http://www.ftdichip.com/Products/EvaluationKits/UM245R.htm
>
> The spec sheet for the board seems quite simple. It's pin out is
> similar to that of a parallel port in that you have your data lines
> DB0-DB7, etc. It can also be connected in bus-powered configuration
> (~100mA) or self-powered configuration. The kit is more expensive than
> the chip itself, but probably easier especially if you don't have any
> experience with surface mount.
>
>
That is a good idea. The main factor (aside from complexity) that I
forgot to mention is the cost. I live very far away from the US and
sometimes it is cheaper to buy a microcontroller here than have bits n
pieces shipped from the US.
Anyway, I'll see if I can find these parts here and maybe use that.
Thanks for all the ideas!
Cheers
Astan

Lie Ryan

unread,
Jan 27, 2009, 5:31:25 AM1/27/09
to pytho...@python.org
On Mon, 26 Jan 2009 11:08:48 -0600, Unknown wrote:

> On 2009-01-26, Lie Ryan <lie....@gmail.com> wrote:
>
>> How about (a crazy idea) using the audio jack out? (DISCLAIMER: Little
>> Hardware Experience). High pitched sound (or anything in sound-ology
>> that means high voltage) means the device is on and low pitched sound
>> off.
>
> 1) Pitch has nothing to do with voltage. A high-pitch sound
> and a low pitch sound can have the exact same voltage.
>
> 2) The OP's device requires quite a bit of power. There is
> almost no power available from the line-out jack, and the voltage is
> limited to about 1V. If his sound card has a power-amp (none do
> these days), he might be able to get a usable amount of power.
>
>> The device will need an additional transistor to separate low voltage
>> from the high voltage.
>
> He'll need more than a transistor. He needs a power supply, some sort
> of rectifier/detector, and a comparitor. It would be more interesting to
> use notch filters to detect different frequencies so that you could have
> multiple output "bits".

>From the little I know on electronics, a simple, single transistor would
(almost) immediately switch from on-to-off-to-on depending on the voltage
of the control pin (I think it was the middle pin). I was suggesting this
simplistic hack because as far as I comprehend the OP's need, he only
need on-off switch instead of a complex multiple output bits.

>> I don't know how much power can be pulled from jack out,
>
> Almost none, and what's there is very low voltage.

That's why the power is taken from USB port.

>> but for a home brewn device it is still feasible to draw power from USB
>> and signal from jack out.
>
> It would probably be easier to buy a USB-parallel port chip. Then he's
> got power from the USB bus and something like 14 parallel I/O pins he
> can control. Alternatively A USB-serial chip will provide 2 outputs and
> 4 inputs.

The idea was made on the basis that a USB microcontroller is not used.
Getting power from USB should be much easier than getting data while the
jack out can provide simple on-off signal.

Grant Edwards

unread,
Jan 27, 2009, 11:05:15 AM1/27/09
to
On 2009-01-27, Lie Ryan <lie....@gmail.com> wrote:

> From the little I know on electronics, a simple, single
> transistor would (almost) immediately switch from
> on-to-off-to-on depending on the voltage of the control pin (I
> think it was the middle pin). I was suggesting this
> simplistic hack because as far as I comprehend the OP's need,
> he only need on-off switch instead of a complex multiple
> output bits.

The audio output jack outputs AC (alternating current). If you
put AC into a transistor, you get AC out. Hence the need for a
rectifier/detector to convert the AC signal into a DC level. To
reliably get an on-off state from an analog DC level, you need
something with some hystersis (e.g. a comparitor). Depending on
the requirements it may possible to do impliment a comparitor
with hysteresis using a single transistor.

If you're looking for a digital output signal whose DC level
you can contol, then either the printer port or the serial port
is a better option than using the sound-card's line-out which
is an analog AC output.

>>> I don't know how much power can be pulled from jack out,
>>
>> Almost none, and what's there is very low voltage.
>
> That's why the power is taken from USB port.
>
>>> but for a home brewn device it is still feasible to draw power
>>> from USB and signal from jack out.
>>
>> It would probably be easier to buy a USB-parallel port chip.
>> Then he's got power from the USB bus and something like 14
>> parallel I/O pins he can control. Alternatively A USB-serial
>> chip will provide 2 outputs and 4 inputs.
>
> The idea was made on the basis that a USB microcontroller is
> not used.

I didn't say anything about using a USB microcontroller. There
are dead-simple and cheap USB-serial and USB-parallel chips
that you can buy that provide the equivalent of a serial port
or parallel printer port via USB.

> Getting power from USB should be much easier than getting data

Using a USB-serial or USB-parallel chip isn't really much
harder than getting power from the USB port, and it's simpler
than building a detector/comparity circuit to connect to the
audio line-out.

> while the jack out can provide simple on-off signal.

With the addition of a rectifier/detector and an appropriate
comparitor circuit, yes.

--
Grant Edwards grante Yow! Civilization is fun!
at Anyway, it keeps me busy!!
visi.com

Tino Wildenhain

unread,
Jan 27, 2009, 5:43:00 PM1/27/09
to Astan Chee, pytho...@python.org
Hi,

Astan Chee wrote:
>
> Diez B. Roggisch wrote:
>>>
>>

>> If all you need is on-off - why can't you just use a switch?
>>
>>
>>

> Because I want to control the on-off the device using a computer and
> write software for it (which I am confident I can do if I had references
> to how the wrappers to said interface).

What I'm using is dallas 1wire stuff to do such things.
The dongle (which is available as USB too) connects
to the host and you have a lib with bindings even to python
for it: http://www.owfs.org/

The other end is just a bus with a telephone wire...

The most interesting part might be temperature sensors but
there are a lot of GPIO chips, electronic resistors, A/D converters
etc. available. Most of the stuff can be even ordered as sample
from dallas/maxim.

Regards
Tino

0 new messages