Skip to: Site menu | Main content

Automation > Code Snippets > Writing to the parallel port

Writing to the parallel port

Getting direct access to the parallel port from scripting languages like PHP, Perl and Python can sometimes be a pain. You can compile a trivial helper program in C that you can then call from your script to write abitrary values to the port like this:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/io.h> #define portaddress 0x378 main(int argc, char **argv) { int value; sscanf(argv[1], "%i", &value); ioperm(portaddress,1,1); outb((unsigned char)value, portaddress); }

Save that snippet into a file such as "lptout.c", then compile it like this:

gcc -O lptout.c -o lptout

Then run it with root privileges to write a value to the port:

sudo ./lptout 0 sudo ./lptout 27 sudo ./lptout 255 ... etc