Control the Parallel (or Serial) Port with Python

par.jpgSean writes "Here's a follow-up to your recent perlParallel post. It's a python module for controlling the parallel port. This module encapsulates the access for the parallel port. It provides backends for Python running on Windows and Linux. They also have a serial port controller here. Link.

Recent Entries

Comments

Oldest comments listed first.

Posted by: saichele on May 9, 2005 at 6:57 PM

This is wonderful! Now how 'bout PHP? (not as groovy as python, but...)


Posted by: wrybread on July 11, 2006 at 10:48 PM

Wondering why my post on using Pyserial to interface with external buttons didn't get posted? Is it just a long approval process, or was it rejected?

wrybread@gmail.com


Posted by: wrybread on July 11, 2006 at 11:07 PM

Ok, reposting.

Here's a python script that uses PySerial to interact with buttons connected to your serial port. So you can use those buttons to do whatever you'd like on your computer. The circuit is super simple, just shorts 4 of the pins to ground:

http://www.ovelha.org/pasteler0/2005/11/17/howto-external-winamp-control/

I personally find it easier to splice an old serial cable than the way he did it. Cheaper too. And I hate to admit it, but there's been a couple of times that I didn't have a continuity meter handy and I simply spliced wires and connected them randomly until I got a reaction, and I havn't fried any serial ports yet.

And it works great with those USB to serial adaptors too.

Note that there's a really good Winamp plugin on that page that also interfaces with this circuit, and in that program are instructions to connect up to 15 buttons to a single serial port, and this script can probably be easily adapted to that. Also note that the above circuit works with the excellent Windows automation program Girder.

Feel free to email wrybread@gmail dot-you-know-what with any questions.

Anyway, here's the script, which for some reason is showing up double-spaced:

import serial # This is pyserial, you need that.
import time

# Adjust this for whatever number your comport is.
com_port = 1

print "Attempting to open your com port..."

try:
# Initialize our com port:
buttons = serial.Serial(com_port)

print "Successfully opened the com port."
print "Your com port returned the following information:\n"
print buttons

except:
print "Whoops, failed to initialize your com port"

print "\nListening for button presses..."

# Start monitoring button presses:
while True:
#modes are CTS, DSR, RI and CD. As in getCTS, getDSR, etc.
if buttons.getCTS():
print "Button 1 pressed!"
# Do something here.

if buttons.getDSR():
print "Button 2 pressed!"
# Do something here.

if buttons.getRI():
print "Button 3 pressed!"
# Do something here.

if buttons.getCD():
print "Button 4 pressed!"
# Do something here.

time.sleep(.02)

And to the person wishing for PHP support, I'd highly recommend sucking it up and learning Python...


Posted by: alok on March 11, 2008 at 6:18 AM

The article is really wonderful.Well how to control a board with ATmega16 microcontroller connected to the serial port with buttons on the screen.The buttons on the screen should function as toggle switches and pressing them should control any device such as a dc motor connected to the controller.


Leave a comment


Subscribe to MAKE!Subscribe to MAKE Magazine!

Subscribe today, save 42% and get web access to MAKE free. MAKE Digital Edition is available only to subscribers.

$34.95 / 1 year
(4 Quarterly Issues)

Subscribe now


Void your warranty, violate a user agreement, fry a circuit, blow a fuse, poke an eye out. Make: The risk-takers, the doers, the makers of things... Welcome to Make: Online!


CRAFT Maker Shed Maker Faire MAKE television




Check out more videos from MAKE.

Maker SHED

Connect with MAKE

Be a MAKE fan on Facebook MAKE on Facebook
Visit our Facebook page and become a fan of MAKE!
MAKE on Twitter MAKE on Twitter
Follow our MAKE tweets!
MAKE Flickr Pool MAKE on Flickr
Join our MAKE Flickr Pool!
    make_tips on Twitter



    MAKE Archives

    Make: Money

    Make: Science Room
    Subscribe to MAKE Magazine!

    Make: Online editors and authors!

    Gareth BranwynGareth Branwyn
    Editor-in-Chief


    Phillip TorronePhillip Torrone
    Senior Editor
    | Web | Twitter


    Becky SternBecky Stern
    Associate Editor
    | AIM | Twitter


    Marc de VinckMarc de Vinck
    Contributing Writer
    | AIM | Twitter


    John ParkJohn Park
    Contributing Writer
    | Twitter


    Sean RaganSean Ragan
    Contributing Writer
    | Twitter


    Matt MetsMatt Mets
    Contributing Writer
    | AIM | Twitter


    Dale DoughertyDale Dougherty
    Editor & Publisher
    | Twitter


    Shawn ConnallyShawn Connally
    Managing Editor
    | Twitter


    Goli MohammadiGoli Mohammadi
    Associate Managing Editor

    Kip KayKip Kay
    Weekend Projects
    | AIM | Twitter


    Collin CunninghamCollin Cunningham
    Contributing Writer
    | AIM | Twitter

    Adam FlahertyAdam Flaherty
    Contributing Writer
    | AIM | Twitter


    John BaichtalJohn Baichtal
    Contributing Writer
    | AIM | Twitter



    More contributors: Mark Frauenfelder (Editor-in-Chief, MAKE magazine), Kipp Bradford (Technical Consultant/Writer), Chris Connors (Education), Diana Eng (Guest Author), Peter Horvath (Intern), Brian Jepson (O'Reilly Media), Robert Bruce Thompson (Science Room)

    Suggest a Site!

    Advertise here with FM.

    Why advertise on MAKE?
    Read what folks are saying about us!

    Click here to advertise on MAKE!



    Current Podcast

    itunesdl.gif Behind the Scenes at MAKE and CRAFT In January, many of the remote MAKE/CRAFT team members (myself included) convened at the Maker Media headquarters at O'Reilly Media in Sebastopol, California. Take a look behind the scenes of your favorite DIY publications as Goli Mohammadi gives us... More...

    Get the Make: Online sent via email
    Enter your email to receive Make: Online each day:



    Sign up for the Make: Newsletter

    Our Make: Newsletter covers news from maker Media, has original columns, Shed deals, and more! You can also read the archives of past issues.


     



    MAKE Fascination video series brought to you by Dow

    Make: Education
    MAKE: en EspaƱol MAKE: Japan
    Important please read


    Subscribe to MAKE Magazine!

    Recent Posts from the Craft: Blog