2
# parallel port access using the ppdev driver
10
# Generated by h2py 0.1.1 from <linux/ppdev.h>,
11
# then cleaned up a bit by Michael P. Ashton and then a gain by chris ;-)
12
# Changes for Python2.2 support (c) September 2004 Alex.Perry@qm.com
15
def sizeof(type): return struct.calcsize(type)
16
def _IOC(dir, type, nr, size): return int((dir << _IOC_DIRSHIFT ) | (type << _IOC_TYPESHIFT ) |\
17
(nr << _IOC_NRSHIFT ) | (size << _IOC_SIZESHIFT))
18
def _IO(type, nr): return _IOC(_IOC_NONE, type, nr, 0)
19
def _IOR(type,nr,size): return _IOC(_IOC_READ, type, nr, sizeof(size))
20
def _IOW(type,nr,size): return _IOC(_IOC_WRITE, type, nr, sizeof(size))
23
_IOC_SIZEMASK = (1 << _IOC_SIZEBITS ) - 1
26
_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
28
_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
29
IOCSIZE_MASK = _IOC_SIZEMASK << _IOC_SIZESHIFT
30
IOCSIZE_SHIFT = _IOC_SIZESHIFT
32
# Python 2.2 uses a signed int for the ioctl() call, so ...
33
if ( sys.version_info[0] < 3 ) or ( sys.version_info[1] < 3 ):
42
_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS
43
IOC_INOUT = _IOC_INOUT << _IOC_DIRSHIFT
44
IOC_IN = _IOC_WRITE << _IOC_DIRSHIFT
45
IOC_OUT = _IOC_READ << _IOC_DIRSHIFT
49
PPCLAIM = _IO(PP_IOCTL, 0x8b)
50
PPCLRIRQ = _IOR(PP_IOCTL, 0x93, 'i')
52
PPDATADIR = _IOW(PP_IOCTL, 0x90, 'i')
53
PPEXCL = _IO(PP_IOCTL, 0x8f)
54
PPFCONTROL = _IOW(PP_IOCTL, 0x8e, 'BB')
55
PPGETFLAGS = _IOR(PP_IOCTL, 0x9a, 'i')
56
PPGETMODE = _IOR(PP_IOCTL, 0x98, 'i')
57
PPGETMODES = _IOR(PP_IOCTL, 0x97, 'I')
58
PPGETPHASE = _IOR(PP_IOCTL, 0x99, 'i')
59
PPGETTIME = _IOR(PP_IOCTL, 0x95, 'll')
60
PPNEGOT = _IOW(PP_IOCTL, 0x91, 'i')
61
PPRCONTROL = _IOR(PP_IOCTL, 0x83, 'B')
62
PPRDATA = _IOR(PP_IOCTL, 0x85, 'B')
63
#'OBSOLETE__IOR' undefined in 'PPRECONTROL'
64
PPRELEASE = _IO(PP_IOCTL, 0x8c)
65
#'OBSOLETE__IOR' undefined in 'PPRFIFO'
66
PPRSTATUS = _IOR(PP_IOCTL, 0x81, 'B')
67
PPSETFLAGS = _IOW(PP_IOCTL, 0x9b, 'i')
68
PPSETMODE = _IOW(PP_IOCTL, 0x80, 'i')
69
PPSETPHASE = _IOW(PP_IOCTL, 0x94, 'i')
70
PPSETTIME = _IOW(PP_IOCTL, 0x96, 'll')
71
PPWCONTROL = _IOW(PP_IOCTL, 0x84, 'B')
72
PPWCTLONIRQ = _IOW(PP_IOCTL, 0x92, 'B')
73
PPWDATA = _IOW(PP_IOCTL, 0x86, 'B')
74
#'OBSOLETE__IOW' undefined in 'PPWECONTROL'
75
#'OBSOLETE__IOW' undefined in 'PPWFIFO'
76
#'OBSOLETE__IOW' undefined in 'PPWSTATUS'
77
PPYIELD = _IO(PP_IOCTL, 0x8d)
81
PP_FLAGMASK = PP_FASTWRITE | PP_FASTREAD | PP_W91284PIC
83
_ASMI386_IOCTL_H= None
85
_IOC_DIRMASK = (1 << _IOC_DIRBITS) - 1
86
_IOC_NRMASK = (1 << _IOC_NRBITS) - 1
87
_IOC_TYPEMASK = (1 << _IOC_TYPEBITS ) - 1
89
def _IOC_DIR(nr): return (nr >> _IOC_DIRSHIFT) & _IOC_DIRMASK
90
def _IOC_NR(nr): return (nr >> _IOC_NRSHIFT) & _IOC_NRMASK
91
def _IOC_SIZE(nr): return (nr >> _IOC_SIZESHIFT) & _IOC_SIZEMASK
92
def _IOC_TYPE(nr): return (nr >> _IOC_TYPESHIFT) & _IOC_TYPEMASK
93
def _IOWR(type, nr, size): return _IOC(_IOC_READ | _IOC_WRITE, type, nr , sizeof(size))
106
#-------- Constants from <linux/parport.h>
108
PARPORT_CONTROL_STROBE = 0x1
109
PARPORT_CONTROL_AUTOFD = 0x2
110
PARPORT_CONTROL_INIT = 0x4
111
PARPORT_CONTROL_SELECT = 0x8
112
PARPORT_STATUS_ERROR = 8
113
PARPORT_STATUS_SELECT = 0x10
114
PARPORT_STATUS_PAPEROUT = 0x20
115
PARPORT_STATUS_ACK = 0x40
116
PARPORT_STATUS_BUSY = 0x80
118
IEEE1284_MODE_NIBBLE = 0
119
IEEE1284_MODE_BYTE = 1
120
IEEE1284_MODE_COMPAT = 1<<8
121
IEEE1284_MODE_BECP = 1<<9
122
IEEE1284_MODE_ECP = 1<<4
123
IEEE1284_MODE_ECPRLE = IEEE1284_MODE_ECP | (1<<5)
124
IEEE1284_MODE_ECPSWE = 1<<10
125
IEEE1284_MODE_EPP = 1<<6
126
IEEE1284_MODE_EPPSL = 1<<11
127
IEEE1284_MODE_EPPSWE = 1<<12
128
IEEE1284_DEVICEID = 1<<2
129
IEEE1284_EXT_LINK = 1<<14
131
IEEE1284_ADDR = 1<<13
135
PARPORT_W91284PIC = 2
139
"""Class for controlling the pins on a parallel port
141
This class provides bit-level access to the pins on a PC parallel
142
port. It is primarily designed for programs which must control
143
special circuitry - most often non-IEEE-1284-compliant devices
144
other than printers - using 'bit-banging' techniques.
146
The current implementation makes ioctl() calls to the Linux ppdev
147
driver, using the Python fcntl library. It might be rewritten in
148
C for extra speed. This particular implementation is written for
149
Linux; all of the upper-level calls can be ported to Windows as
152
On Linux, the ppdev device driver, from the Linux 2.4 parallel
153
port subsystem, is used to control the parallel port hardware.
154
This driver must be made available from a kernel compile. The
155
option is called "Support user-space parallel-port drivers". When
156
using the module, be sure to unload the lp module first: usually
157
the lp module claims exclusive access to the parallel port, and if
158
it is loaded, this class will fail to open the parallel port file,
159
and throw an exception.
161
The primary source of information about the Linux 2.4 parallel
162
port subsystem is Tim Waugh's documentation, the source for which
163
is available in the kernel tree. This document (called,
164
appropriately enough, "The Linux 2.4 Parallel Port Subsystem"),
165
thoroughly describes the parallel port drivers and how to use
168
This class provides a method for each of the ioctls supported by
169
the ppdev module. The ioctl methods are named, in uppercase, the
170
same as the ioctls they invoke. The documentation for these
171
methods was taken directly from the documentation for their
172
corresponding ioctl, and modified only where necessary.
174
Unless you have special reason to use the Linux ioctls, you should
175
use instead the upper-level functions, which are named in
176
lowerCase fashion and should be portable between Linux and
177
Windows. This way, any code you write for this class will (or
178
should) also work with the Windows version of this class.
181
def __init__(self, port = 0):
182
if type(port) == type(""):
185
self.device = "/dev/parport%d" % port
187
self._fd = os.open(self.device, os.O_RDWR)
199
if self._fd is not None:
203
def timevalToFloat(self, timeval):
204
t=struct.unpack('ll', timeval)
205
return t[0] + (t[1]/1000000.0)
207
def floatToTimeval(self, time):
209
usec = int(time*1000000.0)
210
return struct.pack('ll', sec, usec)
214
Claims access to the port. As a user-land device driver
215
writer, you will need to do this before you are able to
216
actually change the state of the parallel port in any
217
way. Note that some operations only affect the ppdev driver
218
and not the port, such as PPSETMODE; they can be performed
219
while access to the port is not claimed.
221
fcntl.ioctl(self._fd, PPCLAIM)
225
Instructs the kernel driver to forbid any sharing of the port
226
with other drivers, i.e. it requests exclusivity. The PPEXCL
227
command is only valid when the port is not already claimed for
228
use, and it may mean that the next PPCLAIM ioctl will fail:
229
some other driver may already have registered itself on that
232
Most device drivers don't need exclusive access to the
233
port. It's only provided in case it is really needed, for
234
example for devices where access to the port is required for
235
extensive periods of time (many seconds).
237
Note that the PPEXCL ioctl doesn't actually claim the port
238
there and then---action is deferred until the PPCLAIM ioctl is
241
fcntl.ioctl(self._fd, PPEXCL)
245
Releases the port. Releasing the port undoes the effect of
246
claiming the port. It allows other device drivers to talk to
247
their devices (assuming that there are any).
249
fcntl.ioctl(self._fd, PPRELEASE)
253
Yields the port to another driver. This ioctl is a kind of
254
short-hand for releasing the port and immediately reclaiming
255
it. It gives other drivers a chance to talk to their devices,
256
but afterwards claims the port back. An example of using this
257
would be in a user-land printer driver: once a few characters
258
have been written we could give the port to another device
259
driver for a while, but if we still have characters to send to
260
the printer we would want the port back as soon as possible.
262
It is important not to claim the parallel port for too long,
263
as other device drivers will have no time to service their
264
devices. If your device does not allow for parallel port
265
sharing at all, it is better to claim the parallel port
266
exclusively (see PPEXCL).
268
fcntl.ioctl(self._fd, PPYIELD)
270
def PPNEGOT(self, mode):
272
Performs IEEE 1284 negotiation into a particular
273
mode. Briefly, negotiation is the method by which the host and
274
the peripheral decide on a protocol to use when transferring
277
An IEEE 1284 compliant device will start out in compatibility
278
mode, and then the host can negotiate to another mode (such as
281
The 'mode' parameter should be one of the following constants
284
- IEEE1284_MODE_COMPAT
285
- IEEE1284_MODE_NIBBLE
290
The PPNEGOT ioctl actually does two things: it performs the
291
on-the-wire negotiation, and it sets the behaviour of
292
subsequent read/write calls so that they use that mode (but
295
fcntl.ioctl(self._fd, PPNEGOT, struct.pack('i', mode))
297
def PPSETMODE(self, mode):
299
Sets which IEEE 1284 protocol to use for the read and write
302
The 'mode' parameter should be one of the following constants
305
- IEEE1284_MODE_COMPAT
306
- IEEE1284_MODE_NIBBLE
311
fcntl.ioctl(self._fd, PPSETMODE, struct.pack('i', mode))
315
Retrieves the IEEE 1284 mode being used for read and
316
write. The return value is one of the following constants
319
- IEEE1284_MODE_COMPAT
320
- IEEE1284_MODE_NIBBLE
325
ret = struct.pack('i', 0)
326
ret = fcntl.ioctl(self._fd, PPGETMODE, ret)
327
return struct.unpack('i', ret)[0]
331
Retrieves the time-out value. The read and write calls will
332
time out if the peripheral doesn't respond quickly enough. The
333
PPGETTIME ioctl retrieves the length of time that the
334
peripheral is allowed to have before giving up.
336
Returns the timeout value in seconds as a floating-point value.
338
ret = struct.pack('ll', 0, 0)
339
ret = fcntl.ioctl(self._fd, PPGETTIME, ret)
340
return timevalToFloat(ret)
342
def PPSETTIME(self, time):
344
Sets the time-out (see PPGETTIME for more information).
345
'time' is the new time-out in seconds; floating-point values
348
fcntl.ioctl(self._fd, PPSETTIME, floatToTimeval(time))
350
def PPGETMODES(self):
352
Retrieves the capabilities of the hardware (i.e. the modes
353
field of the parport structure).
355
raise NotImplementedError
357
def PPSETFLAGS(self):
359
Sets flags on the ppdev device which can affect future I/O
360
operations. Available flags are:
366
raise NotImplementedError
368
def PPWCONTROL(self, lines):
370
Sets the control lines. The 'lines' parameter is a bitwise OR
371
of the following constants from PPDEV:
373
- PARPORT_CONTROL_STROBE
374
- PARPORT_CONTROL_AUTOFD
375
- PARPORT_CONTROL_INIT
376
- PARPORT_CONTROL_SELECT
378
fcntl.ioctl(self._fd, PPWCONTROL, struct.pack('B', lines))
380
def PPRCONTROL(self):
382
Returns the last value written to the control register, in the
383
form of an integer, for which each bit corresponds to a control
384
line (although some are unused).
386
This doesn't actually touch the hardware; the last value
387
written is remembered in software. This is because some
388
parallel port hardware does not offer read access to the
391
The control lines bits are defined by the following constants
394
- PARPORT_CONTROL_STROBE
395
- PARPORT_CONTROL_AUTOFD
396
- PARPORT_CONTROL_SELECT
397
- PARPORT_CONTROL_INIT
399
ret = struct.pack('B',0)
400
ret = fcntl.ioctl(self._fd, PPRCONTROL, ret)
401
return struct.unpack('B', ret)[0]
403
def PPFCONTROL(self, mask, val):
405
Frobs the control lines. Since a common operation is to change
406
one of the control signals while leaving the others alone, it
407
would be quite inefficient for the user-land driver to have to
408
use PPRCONTROL, make the change, and then use PPWCONTROL. Of
409
course, each driver could remember what state the control
410
lines are supposed to be in (they are never changed by
411
anything else), but in order to provide PPRCONTROL, ppdev must
412
remember the state of the control lines anyway.
414
The PPFCONTROL ioctl is for "frobbing" control lines, and is
415
like PPWCONTROL but acts on a restricted set of control
416
lines. The ioctl parameter is a pointer to a struct
419
struct ppdev_frob_struct {
424
The mask and val fields are bitwise ORs of control line names
425
(such as in PPWCONTROL). The operation performed by PPFCONTROL
428
new_ctr = (old_ctr & ~mask) | val
430
In other words, the signals named in mask are set to the
433
fcntl.ioctl(self._fd, PPFCONTROL, struct.pack('BB', mask, val))
437
Returns an unsigned char containing bits set for each status
438
line that is set (for instance, PARPORT_STATUS_BUSY). The
439
ioctl parameter should be a pointer to an unsigned char.
441
ret = struct.pack('B',0)
442
ret = fcntl.ioctl(self._fd, PPRSTATUS, ret)
443
return struct.unpack('B', ret)[0]
445
def PPDATADIR(self, out):
447
Controls the data line drivers. Normally the computer's
448
parallel port will drive the data lines, but for byte-wide
449
transfers from the peripheral to the host it is useful to turn
450
off those drivers and let the peripheral drive the
451
signals. (If the drivers on the computer's parallel port are
452
left on when this happens, the port might be damaged.)
453
This is only needed in conjunction with PPWDATA or PPRDATA.
455
The 'out' parameter indicates the desired port direction. If
456
'out' is true or non-zero, the drivers are turned on (forward
457
direction); otherwise, the drivers are turned off (reverse
461
msg=struct.pack('i',0)
463
msg=struct.pack('i',1)
464
fcntl.ioctl(self._fd, PPDATADIR, msg)
466
def PPWDATA(self, byte):
468
Sets the data lines (if in forward mode). The ioctl parameter
469
is a pointer to an unsigned char.
471
fcntl.ioctl(self._fd, PPWDATA,struct.pack('B',byte))
475
Reads the data lines (if in reverse mode). The ioctl parameter
476
is a pointer to an unsigned char.
478
ret=struct.pack('B',0)
479
ret=fcntl.ioctl(self._fd, PPRDATA,ret)
480
return struct.unpack('B',ret)[0]
484
Returns the current interrupt count, and clears it. The ppdev
485
driver keeps a count of interrupts as they are triggered.
487
ret=struct.pack('i',0)
488
ret=fcntl.ioctl(self._fd, PPCLRIRQ,ret)
489
return struct.unpack('i',ret)[0]
491
def PPWCTLONIRQ(self, lines):
493
Set a trigger response. Afterwards when an interrupt is
494
triggered, the interrupt handler will set the control lines as
495
requested. The ioctl parameter is a pointer to an unsigned
496
char, which is interpreted in the same way as for PPWCONTROL.
498
The reason for this ioctl is simply speed. Without this ioctl,
499
responding to an interrupt would start in the interrupt
500
handler, switch context to the user-land driver via poll or
501
select, and then switch context back to the kernel in order to
502
handle PPWCONTROL. Doing the whole lot in the interrupt
503
handler is a lot faster.
505
fcntl.ioctl(self._fd, PPWCTLONIRQ,struct.pack('B',lines))
509
## """Returns the states of the data bus line drivers (pins 2-9)"""
512
def setDataDir(self,out):
513
"""Activates or deactivates the data bus line drivers (pins 2-9)"""
518
"""Returns true if the data bus line drivers are on (pins 2-9)"""
523
## """Returns the state of the nStrobe output (pin 1)"""
524
## return (self.PPRCONTROL()&PARPORT_CONTROL_STROBE)==0
526
def setDataStrobe(self, level):
527
"""Sets the state of the nStrobe output (pin 1)"""
529
self.PPFCONTROL(PARPORT_CONTROL_STROBE, 0)
531
self.PPFCONTROL(PARPORT_CONTROL_STROBE, PARPORT_CONTROL_STROBE)
534
## """Returns the state of the nAutoFd output (pin 14)"""
535
## return (self.PPRCONTROL()&PARPORT_CONTROL_AUTOFD)==0
537
def setAutoFeed(self, level):
538
"""Sets the state of the nAutoFd output (pin 14)"""
540
self.PPFCONTROL(PARPORT_CONTROL_AUTOFD, 0)
542
self.PPFCONTROL(PARPORT_CONTROL_AUTOFD, PARPORT_CONTROL_AUTOFD)
545
## """Returns the state of the nInit output (pin 16)"""
546
## return (self.PPRCONTROL()&PARPORT_CONTROL_INIT)!=0
548
def setInitOut(self, level):
549
"""Sets the state of the nInit output (pin 16)"""
551
self.PPFCONTROL(PARPORT_CONTROL_INIT, PARPORT_CONTROL_INIT)
553
self.PPFCONTROL(PARPORT_CONTROL_INIT, 0)
555
## def selectIn(self):
556
## """Returns the state of the nSelectIn output (pin 17)"""
557
## return (self.PPRCONTROL()&PARPORT_CONTROL_SELECT)==0
559
def setSelect(self,level):
560
"""Sets the state of the nSelectIn output (pin 17)"""
562
self.PPFCONTROL(PARPORT_CONTROL_SELECT, 0)
564
self.PPFCONTROL(PARPORT_CONTROL_SELECT, PARPORT_CONTROL_SELECT)
567
"""Sets the states of the data bus line drivers (pins 2-9)"""
569
return self.PPWDATA(d)
572
"""Gets the states of the data bus line (pin 2-9)"""
573
return self.PPRDATA()
576
def getInError(self):
577
"""Returns the level on the nFault pin (15)"""
578
return (self.PPRSTATUS() & PARPORT_STATUS_ERROR) != 0
580
def getInSelected(self):
581
"""Returns the level on the Select pin (13)"""
582
return (self.PPRSTATUS() & PARPORT_STATUS_SELECT) != 0
584
def getInPaperOut(self):
585
"""Returns the level on the paperOut pin (12)"""
586
return (self.PPRSTATUS() & PARPORT_STATUS_PAPEROUT) != 0
588
def getInAcknowledge(self):
589
"""Returns the level on the nAck pin (10)"""
590
return (self.PPRSTATUS() & PARPORT_STATUS_ACK) != 0
593
"""Returns the level on the Busy pin (11)"""
594
return (self.PPRSTATUS() & PARPORT_STATUS_BUSY) == 0