PortIO is a Python wrapper for the port I/O macros like outb, inb, etc. provided by the C library on Linux x86 platforms. Both python 2 and 3 are supported. This module is useful when a general pourpose port I/O at the low level is needed. Programmers that want to perform I/O on the parallel port at an higher level, will be better satisfied by the pyParallel module. A similar module Ioport has inspired the writing of PortIO.
PortIO is released under the GNU General Public License.
At present, version 0.5, PortIO is in beta status. Any debugging aid is welcome.
For any question, suggestion, contribution contact the author Fabrizio Pollastri <f.pollastri_a_t_inrim.it>.
The PortIO web site is hosted at http://portio.inrim.it/.
This sample program toggle on and off all the data lines of the parallel port lp0 with a 6 seconds period. Note the check for root privileges before the call to ioperm to acquire the proper I/O permissions for the involved ports.
import sys, time, os
import portio
# check for root privileges
if os.getuid():
print('You need to be root! Exiting.')
sys.exit()
# acquire permission for I/O on lp0
status = portio.ioperm(0x378, 1, 1)
if status:
print('ioperm:',os.strerror(status))
sys.exit()
# toggle forever the data lines of lp0
data = 0
while 1:
lp0in = portio.inb(0x378)
portio.outb(data,0x378)
print('read %x from lp0, written %x to lp0' % (lp0in,data))
data = ~data & 0xff
time.sleep(3)
Download the sample program toggle.py
PortIO is a Python front end to the low level functions provided by the C library on Linux 386 platforms for the hardware input and output ports: outb, outw, outl, outsb, outsw, outsl, outb_p, outw_p, outl_p, inb, inw, inl, insb, insw, insl, inb_p, inw_p, inl_p, ioperm, iopl.
Before doing port I/O, it is mandatory to acquire proper privileges by calling ioperm or iopl. Otherwise you will get a segmentation fault.
Set port access permission starting from address from for extent bytes. If the enable is True, access is enabled, otherwise is disabled. On success, zero is returned. On error, the errno code is returned. The use of ioperm requires root privileges.
Only the first 0x3ff I/O ports can be specified in this manner. To gain access to any I/O port in the whole (0x0000-0xffff) address range, use the iopl function.
A linux on an X86 architecture.
To run the code, Python 2.6 or later or Python 3.0 or later must already be installed. The latest release is recommended. Python is available from http://www.python.org/.
Open a shell.
Get root privileges and install the package. Command:
easy_install portio
Download PortIO tarball from http://portio.inrim.it/portio-0.5.tar.gz .
The first step is to expand the .tgz archive in a temporary directory (not directly in Python’s site-packages). It contains a distutils setup file “setup.py”.
Open a shell.
Unpack the tarball in a temporary directory (not directly in Python’s site-packages). Command:
tar zxf portio-X.Y.tar.gz
X and Y are the major and minor version numbers of the tarball.
Go to the directory created by expanding the tarball. Command:
cd portio-X.Y
Get root privileges and install the package. Command:
su
(enter root password)
python setup.py install
If the python executable isn’t on your path, you’ll have to specify the complete path, such as /usr/local/bin/python.
Portio 0.5 released 25-Oct-2012
Portio 0.4 released 25-Aug-2009
Portio 0.3 released 21-May-2009
Portio 0.2 released 11-Nov-2008
Portio 0.1 released 23-Feb-2006
Copyright 2006-2012 by Fabrizio Pollastri <f.pollastri_a_t_inrim.it>