I/O Hardware port extension for Python

NOTE: I recommend you use the superior pyParallel package instead of ioport. pyParallel works on Linux and all Windows platforms, making your applications much more portable.

Ioport is a Python 'C' extension that allows you to address hardware I/O ports from a Python script. It will only work on Linux x86 hardware. Its a simple wrapper around the standard Linux ioperm/outb/inb macros.

I wrote this because a friend asked me for help test some hardware he designed that is driven by the printer port. 'Python' I thought, but I was surprised to find that a Python extension for this didn't seem to exist. So I wrote one.

CAVEAT: Messing around with your hardware ports can be bad for your PC's health. Use ioport with caution.

Here's some sample code to set bit 0 on the first printer port.

 
    import ioport

    ioport.ioperm(0x3bc, 1, 1)
    ioport.outb(0x01, 0x3bc)

Download

Download the source tarball ioport.tgz. A Linux binary for RedHat 6.0 is here.

Build

Build ioport.so just like any other Python 'C' extension. Make sure that the Python Makefile build is optimized (-O or -O2), otherwise the i/o inlining won't work.

Test

The source tarball includes the script wiggle.py. This wiggles all bits on the printer port at 1 second intervals. You must be root to execute this.

If Python segfaults while using ioport, its most likely that you haven't called ioperm, or your trying to input/output to a port you haven't ioperm'd.

Documentation

Ioport has inline documentation, reproduced here for you pleasure.

Module ioport

 
ioport - A Python extension for input/output to hardware ports. Ioport
is a simple wrapper for the Linux ioperm/outb/inb calls, and it shares
many of their characteristics & limitations. In particular, Python
will segfault if you attempt i/o without calling ioperm first. See the
Linux IO-Port-Programming Mini-HOWTO by Riku Saikkonen
(Riku.Saikkonen@hut.fi) for more details.

The following calls are supported:

   ioperm(port, len, onoff)
   outb(value, port)
   inb(port)

Function ioperm

 
ioperm(port, len, onoff)

Turn onoff ioperm for calling process for port block at 'port',
length 'len'.

Function outb

 
outb(value, port)

Output the byte 'value' to io port 'port'.

Function inb

 
inb(port)

Return a byte with the input read from 'port'.

Consulting

I'm available for Python, MQ, Unix & C/C++ consulting assignments. See http://www.open-networks.co.uk and my cv for more details.

Contact Me

Feedback, Questions, Comments, Suggestions & Bugs to lsmithson@open-networks.co.uk. Or see my Home Page.

Disclaimer

You are free to use this code in any way you like, subject to the Python disclaimers & copyrights. I make no representations about the suitability of this software for any purpose. It is provided "AS-IS" without warranty of any kind, either express or implied. So there.
(c) Copyright L. Smithson 2000.
$Id: ioport.html,v 1.5 2006/01/24 17:22:01 lsmithso Exp $