DB25 connector pinouts.

Python Programming Controlling PC Parallel Port Data Bits

by Lewis Loflin

If one has installed pyparallel from my page Programming the PC Printer Port in Python we saw the 8 LEDs counting in binary. Unfortunately pyparallel has no function to cut on-off individual bits on the data port. I wrote my own as illustrated below.

There are eight functions writeD0(x) - writeD7(x). Simply use "1" to turn on a LED or "0" to turn off. The example will turn on LEDs D0,D1, and D7.


writeD0(1)
writeD1(1)
writeD2(0)
writeD3(0)
writeD4(0)
writeD5(0)
writeD6(0)
writeD7(1)

This is useful for connecting differing pieces of hardware to the data port and controlling them individually.

Also see Additional Commands for Py-Parallel.


#!/usr/bin/env python
# File count.py
# http://www.bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# For use with pyparallel

import parallel
import time
p = parallel.Parallel()

# Count 0-255 binary on 8 LEDs 
for var in range(0,256):
    p.setData(var)
    print var
    time.sleep(.5) #delay 500 mSec.

p.setData(0x00) # turn LEDs off
    
print "Good by!"

exit 

Data Port Bit Functions

#!/usr/bin/env python
# File pportbits.py
# http://www.bristolwatch.com/pport/index.htm
# By Lewis Loflin - lewis@bvu.net
# Example code to turn ON-OFF individual bits on PC 
# printer port Db25 pins 2-9.
# Must use my version of pyparallel on website for p.data().
# Bitwise AND is used to clear a bit while bitwise OR used to set bit.

import parallel
p = parallel.Parallel()

def writeD0(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 1) # set bit 0
    else: # clear bit 0
        p.setData(p.data() & (255 - 1))
    return

def writeD1(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 2) #set bit 1
    else: # clear bit 1
        p.setData(p.data() & (255 - 2))
    return

def writeD2(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 4) #set bit 2
    else: # clear bit 2
        p.setData(p.data() & (255 - 4))
    return

def writeD3(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 8) #set bit 3
    else: # clear bit 3
        p.setData(p.data() & (255 - 8))
    return

def writeD4(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 16) #set bit 4
    else: # clear bit 4
        p.setData(p.data() & (255 - 16))
    return

def writeD5(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 32) #set bit 5
    else: # clear bit 5
        p.setData(p.data() & (255 - 32))
    return

def writeD6(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 64) #set bit 6
    else: # clear bit 6
        p.setData(p.data() & (255 - 64))
    return

def writeD7(bit_val):
    if bit_val == 1:
        p.setData(p.data() | 128) #set bit 7
    else: # clear bit 7
        p.setData(p.data() & (255 - 128))
    return

# convert a 8-bit number (integer) to a binary. 
# Returns string.
# unlike python bin() this doesn't drop leading zeros
def convBinary(value):
    binaryValue = 'b'
    for  x in range(0, 8):
        temp = value & 0x80
        if temp == 0x80:
           binaryValue = binaryValue + '1'
        else:
            binaryValue = binaryValue + '0'
        value = value << 1
    return binaryValue
    

# Set all data port bits to 0
p.setData(0) # LEDs off
print "Port data latches =", p.data() 
# read port data latches - should be 0

# use differing combinations 

# set bits D0, D1, D7 
writeD0(1)
writeD1(1)
writeD7(1)

# Read and print data port:
xp = p.data()
print "Value of data port =", convBinary(xp), " ", hex(xp) 
# should be Value of data port = b10000011   0x83 
# LEDs connected to port will show 10000011  
print
print "Good by!"

exit 


Download pport-1.0.iso from Sourceforge.com then burn to DVD (file size 920 meg.), insert into DVD drive and reboot. Make sure PC is set to boot from DVD ROM.

This is pre-configured by myself to use Python to control the printer port. Python can be run from IDLE or Geany.

All of my PPORT electronics projects will work without installation to a PC.

Programs can be saved to thumb drive in LIVE mode.

Projects

Below are listed a series of projects using pyparallel and electronics. Starting with routines I wrote to aid students I'd advise walking through this in sequence. Have fun and send comments and/or corrections to lewis@bvu.net.

Linux Videos

Printer Port in C


Web site Copyright Lewis Loflin, All rights reserved.
If using this material on another site, please provide a link back to my site.