Parallel Port
From JasonAntmanWiki
Input/Output
PHP
This site provides code for controlling parallel port via PHP. It also includes code to control relays using the parallel port.
<?php /* Program: lptout.php * Desc: Takes data from the form and sends it to the * parallel port program (lptout). * Values should be given in hex (0 - ff) * requires lptout placed in /usr/sbin/ * * By Andrew Nickson (www.re-mastered.co.uk) */ ?> <?php switch (@$_GET['do']) { case "update": { echo ("<center><br>Data sent to parallel port (0x$fvalue)</br></center>"); exec ("/usr/sbin/lptout 0x$fvalue"); include("parallel.inc"); } break; default: include("parallel.inc"); } ?> Here is the source code of parallel.inc file that is used by the previous source code. This source code prints out the form that is shown to user for parallel port controlling. <?php /* Program: Parallel.inc * Desc: Contains the form data for the parallel control * This will call parallel.php. * * By Andrew Nickson 2005 (www.re-mastered.co.uk) */ ?> <html> <head><title>Parallel Port Controller</title></head> <body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0"> <center><form action="parallel.php?do=update" method="POST"> <table border="0"> <tr><td align=right><b>Value (HEX)</b></td> <td><input type="text" name="fvalue" size="5" maxsize="3"> </td></tr> <br><input type="submit" name="Update" value="Update"> </td></tr> </table> </body></html>
Getting Input
From this page:
Reading the input pins in parallel port input pins PC parallel port has 5 input pins. Those inputs can accept TTL level signals (0-0.7V = logic 0, 2.4-5V = logic 1). You can connect a TTL level output signal to it directly (remeber to attach the signal source ground to parallel port ground). You can connect siple switches to the inputs by connecting the switch bbetween parallel port ground and input pin, and then adding a 10 kohm pull-up resistor from the pin to +5V. When the switch is activated, the pin goes to logic state 0. Usually it is a good idea to isolate the PC from the signal source, and in this case it is usually a good idea to use a relay or optocoupler (the relay/optocoupler output is wired in the place of the switch, otherwise works in the same way as the switch connection). TIP: You can avoid using external +5V power source by using one of the parallel port output pins as one. So set one free output pin to logic 1 and wire the 10 kohm pull-up resistors to it instead of dedicated extra +5V source. For other ideas how to get that +5V power take a look at How to get power from PC to your circuits and Simple 5V power supply for digital circuits. The input pins can be read from the I/O address LPT port base address + 1. The meaning of the buts in byte you read from that I/O port: * D0: state not specified * D1: state not specified * D2: state not specified * D3: state of pin 15 (ERROR) inverted * D4: state of pin 13 (SELECTED) * D5: state of pin 12 (PAPER OUT) * D6: state of pin 10 (ACK) * D7: state of pin 11 (BUSY) inverted Here are some code snipplets to read LPT port: Assembler MOV DX,0379H IN AL,DX You get the result fo read from AL register BASIC N = INP(&H379); Where N is the numerical value you read. C in = inportb(0x379); or in = inp(0x379); Where N is the data you want to output. The actual I/O port controlling command varies from compiler to compiler because it is not part of standardized C libraries. Here is a Linux reading program example that reads the state of parallel port input pins: #include #include #include #include #define base 0x379 /* I/O address to read */ main(int argc, char **argv) { int value; if (ioperm(base,1,1)) fprintf(stderr, "Couldn't get the port at %x\n", base), exit(1); value = inb(base); printf("Port 0x%x read value is %i \n",base,value); } /* compile with command line gcc -O lpt_read.c -o lpt_read */ The user has to have the previledges to have access to the ports for the program to run, so you have to be root to be able to ron this kind of programs without access problems. If you want to make a program which can be run by anybody then you have to first set the owner of the program to be root (for example do compilation when yhou are root), give the users rights to execute the program and then set the program to be always executed with owner (root) rights instead of the right of the user who runs it. You can set the programn to be run on owner rights by using following command: chmod +s lpt_read
There is also information on a parallel port relay board here.