Jump to content
xisto Community
Sign in to follow this  

Lpt Port And Python Using SWIG to build python wrappers

Recommended Posts

Some time ago i was asked to program device, connected to PC parallel port. Main program was written in python.

First of all i found nice C library which perform the task: parapin. Building and installation is straightforward, so i don't stop on this.

Then i start to build python wrapper. With SWIG its easy.

First create file parapin.i, here is head of it:

%module parapin


#include <parapin.h>



int pin_init_user(int lp_base);

#define LPT1 0x378

#define LPT2 0x278


void set_pin(int pins);

void clear_pin(int pins);

void change_pin(int pins, int state);

#define LP_CLEAR 0 /* for change_pin */

#define LP_SET 1 /* for change_pin */

Actually i wrote only first 3 line, rest is copypasted from library header paragui.h. I take only those functions and definitions, which i need for my task.


Then i wrote make config file Makefile:

all: _parapin.so


_parapin.so: parapin_wrap.o

gcc -o _parapin.so -shared parapin_wrap.o -lparapin


parapin_wrap.o: parapin_wrap.c

gcc -fpic -c parapin_wrap.c -I/usr/include/python2.4/ -I/usr/local/include


parapin_wrap.c: parapin.i

swig -python parapin.i



After running make utility i had ready for use python module parapin.py with functions set_pin, clear_pin and so on.


Not so hard, isnt't it?

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.