Solution: Direct access to Printer I/O lines

jkndeja at jkndeja at
Wed Dec 13 05:50:20 EST 2000

    People have been asking about getting _direct_ access to a printer
port over the last few weeks, and I've already made some comments on
the matter myself.

By 'direct' in this case, I'm assuming that people want to be able to
individually twiddle the bits of the printer port, so that they can
light LEDs, read switches, control a nuclear power station ;-), etc.

As several people have pointed out, this is very OS-dependant.
Nevertheless, it's prompted me to write a simple python extension DLL.
This should allow access to the ports of LPT1, LPT2, and LPT3 from
Python, under windows 95 at least (see below for caveats)

You can download a .ZIP file containing the .C source, and the DLL:

    - This is untested!! in the sense that I have not yet got my I/O
hardware connected up, and therefor can't verify that it actually does
what I intend it to. It does behave as expected in other ways though. I
would be very keen to learn of experiences in using it.
    - It's intended to work under Windows 95 only. It won't work under
NT (you need a kernel-mode driver to access I/O ports under NT, and I
haven't written that yet ;-). I don't know about W98. It won't work
under linux either ;-). I'm happy to write a similar extension for
linux if people are interested - it's good practice for me...
    - to be safe, I have restricted the range over which I/O port
access is allowed. This is currently limited to the address spaces of
the LPT ports:

    0x278 to 0x27a (LPT1:)
    0x378 to 0x37a (LPT2:)
    0x3bc to 0x3bf (LPT3:)

    You'll get a ValueError exception
        "ValueError: Access to I/O ports outside LPT[1-3]: not allowed"

    if you try to go outside this range. If you want to remove this
lmitation, you'll have to modify and recompile the code, in which case
I assume you know what you're doing...

    - Oh, I built it with Python v1.5.2 (ie. this is the import Library
I used). Does this mean it will need re-building for V2.0? not sure...

Installation and use:
    - download the .ZIP file
    - unzip, and put the .DLL file in the appropriate directory. On my
W95 setup, it's in <Python Base Dir>\DLLs

Use like this:

import ioport

print ioport.inb(0x278)    # byte read
ioport.outb(0x279, 0x00)   # byte write: (port, val)

# you also have word I/O
# print ioport.inw(0x278)      # word read
# ioport.outw(0x279, 0x0f0f)   # word write: (port, val)

I haven't even checked yet which of the LPT: port addresses are Read-
only, which are Write only, and which are Read/Write - that's how
tested this is! Track down Ralph Browns Interrupt list for the gen. on

Despite all these caveats, I do actually expect that this will work!
I'm happy to receive reports: mailto:jkn at

    jon Nicoll

Sent via

More information about the Python-list mailing list