/* This program demonstrates writing a character to the data port of the parallel port, pulsing the strobe pin on the control port, and reading the five input lines from the status port. compile this program using gcc -o parallel parallel.c */ #include #include #include #include #include /* Mem address of P port */ #define DATA 0x378 #define STATUS DATA+1 #define CONTROL DATA+2 main (int argc, char *argv[]) { unsigned char val; unsigned int d; int fd; // open /dev/port for reading and writing fd = open("/dev/port", O_RDWR); if(!fd) { printf("Error: unable to open /dev/port\n"); exit(-1); } // first write to the data port lseek(fd, DATA, SEEK_SET); // write a haracter if there is one on the command line, otherwise 0's if(argc > 1) { if(strlen(argv[1]) > 0) { sscanf(argv[1], "%x", &d); val = (unsigned char)d; } else val = 0x00; } else val = 0x00; write(fd, &val, 1); printf("Wrote %X\n", (int)val); // this is how you pulse the nStrobe line lseek(fd, CONTROL, SEEK_SET); val = 0x00; write(fd, &val, 1); // wait a bit usleep(100); // reset the strobe line lseek(fd, CONTROL, SEEK_SET); val = 0x01; write(fd, &val, 1); // this is how you read the five input lines on the status port lseek(fd, STATUS, SEEK_SET); read(fd, &val, 1); // this converts the status lines to positive logic and shifts // them to the low 5 bits of val. val = ( val^0x80) >> 3 ; // close the port close(fd); printf("Received %d\n", (int)val); }