How to communicate with LED connected to LPT

wtorek, 6 kwietnia 2010 by Michał Jezierski
Here is a simple comparision, how can communication with parallel port be done using C and Python. We write a very simple piece of source code. You need to connect LED diode to LPT pins 5 and 20.




Be careful - it can harm your computer, so shut down PC first.

C

Program demonstrates basic parapin library usage. Get parapin library from http://parapin.sourceforge.net/ and install it.

Create led.c file:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "parapin.h"

int main(int argc, char *argv[]) {
 int pin = 5;

 if (pin_init_user(LPT1) < 0)
  exit(0);

 pin_output_mode(LP_DATA_PINS | LP_SWITCHABLE_PINS);

 set_pin(LP_PIN[pin]);
 sleep(5);
 clear_pin(LP_PIN[pin]);

 return 0;
}
Then compile above program with command
gcc led.c -O -lparapin -o led
and run
sudo ./led
The program will flash your LED diode for 5 seconds.

Python

The same can be done with Python and it seems to be much easier. Take a look at led.py
import parallel
import time

p=parallel.Parallel()
p.setData(0x8)
time.sleep(5)
p.setData(0x0)

Remarks

If you want to run led.py you need to remove lp module from kernel. C program doesn't have such limitations.
Note, that both programs need to be run with root privilages.
Posted in | 0 Comments »

Brak komentarzy:

Prześlij komentarz