LPT programming example in C for ubuntu linux

LED with LPT port A while back I wanted to try out LPT printer port programming for upcoming project. After hours of learning, testing and searching, I came up with fallowing piece of code what finally worked for me. This example code makes LED blink in rate of 2 blinks per second wich is soldered between ground and any of the data pins.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
#include <sys/types.h>
#include <fcntl.h>

#define BASEPORT 0x378 /* lp1 */

int main() {
	char c;
	int n, tem;
	
	printf("Hit any key to stop\n");
	
	//set permissions to access port
	if (ioperm(BASEPORT, 3, 1)) {perror("ioperm"); exit(1);}
	
	tem = fcntl(0, F_GETFL, 0);
	fcntl (0, F_SETFL, (tem | O_NDELAY));
	
	//main loop where actual blinking is done
	while (1) {
		//if some key is pressed, break out from loop
		n = read(0, &c, 1);
		if (n > 0) break;
		
		//write 'on' bit on all data pins and wait 1/4 second
		outb(255, BASEPORT);
		usleep(250000);
		
		//write 'off' bit on all data pins and wait 1/4 second
		outb(0, BASEPORT);
		usleep(250000);
	}
	
	fcntl(0, F_SETFL, tem);
	outb(0, BASEPORT);
	
	//take away permissions to access port
	if (ioperm(BASEPORT, 3, 0)) {perror("ioperm"); exit(1);}
	
	exit(0);
}

I compiled it with command where lpt.c is source code and lpt is program name compiled.

gcc lpt.c -O2 -o lpt

After that check that program has excecute permissions and run it with command.

./lpt

I used LED with 550 ohms resistor in series and soldered them between pin 2 and 20 to test out this program. It worked fine.

You may also like to check out these links

Magnus #
Thanks a lot man! I was looking for this but everything seemed to be wrong for some reason until I found this. This worked for me. Thanks.
No gravatar aivable
Riccardo #
Hello, could you add some comments to you code so that we can understand it? I'd like to learn to open it and read data...what documentation do i need?
No gravatar aivable
david #
Could I use your code to writing my bachelor of science dissertation? I'm looking forward your respond.
No gravatar aivable
pk #
Work well under ubuntu 9.10 thanks
No gravatar aivable
Joe L #
This looks like just what I need! I've noticed that my lpt port seems to be /dev/lpt0 though. Do you know the BASEPORT value that should be used for this (how do you find out?)... or is this still the same value?
No gravatar aivable
Joe #
I've tried running your example and watching the voltage over pins 2, 3 and 4 (data0, data1, data2) using a multimeter. Unfortunately the voltage just stays at 5v (surely it should be dropping to low voltage a couple of times per second??)
Janar #
Joe,
I think it should, but maybe it is changing too fast for multimeter to register the voltage drop? With LED would be much better to see it.
No gravatar aivable
Bijoy #
There is no error, no warning
but my light is still blind
Why?
No gravatar aivable
leo #
bijoy.. check your circuit.....
the code works..
No gravatar aivable
Nadeera #
Ya this works.tnx lot
No gravatar aivable
J.W. #
It is wonderful ! Thank you a lot ! I would like make a lot of lpt port program and electronic machine and I would like process a lot of thing with lpt port. Have a nice day !
J.W. from Hungary.
R.S. #
thank you so much.
This saved me tons of time.

Cheers!
No gravatar aivable
shailesh tatware #
what can i do for my desired output on databit pins?
Janar #
Maybe this thread can help you- http://cboard.cprogramming.com/tech-board/92965-parallel-port-sending-signals-binary.html#post666615

My article is old and I can't remember much of the stuff going on here.
No gravatar aivable
sesil #
thanks dude that was very useful to me ....
i was trying to test the port for two days finally i got your post.....i'm happy
No gravatar aivable
Omni Tech Support #
I know the basics of Linux. What you have written is beyond my knowledge. The device looks quite cool. I tried to make one before few months. But it never worked properly. Anyway I will try it again and let me see how it is going.
No gravatar aivable
vlad #
Thanks a lot!
I had to run the code with
sudo ./ltp
to aviod ioperm error.
No gravatar aivable
george #
Work well under wheezy thanks,Thanks a lot man! There is no error, no warning. excellent to do. It is wonderful !
George Sariev #
Work
sudo gcc lpt.c -O2 -o lpt
sudo ./lpt
No gravatar aivable
Susanne #
hi,
I'm using your code and I'm fascinated from the simplicity and functionality but can you give me a short explanation what the file operation commands {fcntl (0, F_SETFL, (tem | O_NDELAY))....} are good for, since no file is beeing opened ?
tnx
Susi
No gravatar aivable
nattakiet #
it is work, thank you.
No gravatar aivable
Vinod #
how to confirm in program whether data is written or not to parallel port. I am doing as follows:

printf("writing data");
outb(); // write some data
sleep(2);
outb();
sleep(2);
printf("write complete");

Output : writing data write complete.
OVH Studio #
You are a Genius it works, Thanks a lot
George Sariev #
in IBM have problem just go to bios and put not
#define BASEPORT 0x378 /* lp1 */
put in code
#define BASEPORT 0x278 /* lp1 */
just see bios setings
No gravatar aivable
Cristian #
Very nice and useful! It's work well under Puppy Linux. Thank you!
No gravatar aivable
leah melda #
Criminology Papers Assignment Services online produces different assignments, including Criminology Research Paper Services, to help students deal with the Criminology Essay Writing Services challenges they face every day.
No gravatar aivable
Mamet #
Thanks a lot, this code sequence has saved me a lot of time.
No gravatar aivable
Penni McConachy #
YOU NEED QUALITY VISITORS FOR YOUR: eagerfish.eu

WE PROVIDE ORGANIC VISITORS BY KEYWORD FROM SEARCH ENGINES OR SOCIAL MEDIA

YOU GET HIGH-QUALITY VISITORS
- visitors from search engines
- visitors from social media
- visitors from any country you want

CLAIM YOUR 24 HOURS FREE TEST => https://bit.ly/2V9Gsw2
No gravatar aivable
Halley Robichaud #
YOU NEED QUALITY VISITORS FOR YOUR: eagerfish.eu

WE PROVIDE ORGANIC VISITORS BY KEYWORD FROM SEARCH ENGINES OR SOCIAL MEDIA

YOU GET HIGH-QUALITY VISITORS
- visitors from search engines
- visitors from social media
- visitors from any country you want

CLAIM YOUR 24 HOURS FREE TEST => https://bit.ly/2V9Gsw2
I am co-founder of web/media studio GIVE me. and (android)developer at start-up named Choco. Read my about page to learn more.