PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Ports abfragen LPT1



Enterprise
03-01-2005, 20:44
Hallo,

Ich möchte eine kleine Störmelde Zentrale per Web realisieren.
Da müsste ich noch z.B:Ports abfragen.
Wie kann ich ich Ports auf dem Parallelport abfragen ?
Z.B:Alle Minute soll dir Abfrage sein.
je nach zustand soll auf dem Intranet ein Ereigniß folgen.
Kann man dies unter der bash abfragen?

nobody0
05-01-2005, 00:14
Es könnte über /dev/port gehen, aber ich habe das nie ausprobiert.
Hier mal zwei Programme zum Parallelport für den Standard-Parallel-Port beim PC:



/* pario.c
* Dient zum Auslesen der Standard-Parallelport-Register und kann z. B.
* verwendet werden um zu sehen, welche Werte in das Status-Register
* geschrieben wurden.
*
* This Program won't work on the sparc, where there's no concept of I/O space
* Tested with Linux 2.2 on the x86.
*
* important: this program won't work without the compiler-option -O or -02 ...
* and this program can only be run by a superuser (or another user after
* "chown root.root pario; chmod 4755 pario").
*
* It takes approx. 1,5us for one Parallel Port I/O (with an onboard Parallel Port).
*
* dd if=/dev/port bs=1 count=1 skip=889

Rolf Freitag
Lizenz: GPL

*/

#include <stdio.h>
#include <errno.h> /* error codes */
#include <sys/io.h> /* or <asm/io.h> */
#include <stdlib.h> // exit

#ifndef BIT0 /* ifdef to avoid redefinition; existing definitions should be ok */
# define BIT0 (0x1) // 2^0
#endif
#ifndef BIT1
# define BIT1 (0x2)
#endif
#ifndef BIT2
# define BIT2 (0x4)
#endif
#ifndef BIT3
# define BIT3 (0x8)
#endif
#ifndef BIT4
# define BIT4 (0x10) // 16
#endif
#ifndef BIT5
# define BIT5 (0x20) // 32
#endif
#ifndef BIT6
# define BIT6 (0x40) // 64
#endif
#ifndef BIT7
# define BIT7 (0x80) // 128
#endif

int
main ()
{
const unsigned int b = 0x378; /* parallel port base (e. g. 0x3bc, 0x378 or 0x278), a wrong value may cause serious damage */
unsigned char b0;

// if (getuid()!=0)
// {
// printf("\a\n\nError: $UID==%d!=0 (you are not a superuser).\n\n",getuid());
// exit (-EPERM);
// }
iopl (3); /* allows access to all I/O-Ports, ioperm doesen't work above the 0x3ff-Limit e. g. at PCI-Cards */

b0 = inb (b + 2);
printf ("Der Wert, der von Port 0x%hx (Kontroll-Register des Parallelports) soeben gelesen wurde ist: 0x%hx\n", b + 2, b0);
printf
("BIT0: /STROBE (out, Pin 1), BIT1: /AUTOFD (out, Pin 14), BIT2: /INIT (not inverting, out, Pin 16), BIT3: /SELECT (out, Pin 17), BIT4: Interrupt enable, Bit5: data read /write\n");
b0 |= BIT5; // read mode
outb (b0, b + 2);
// outb (0xff, b);
b0 = inb (b + 1);
printf ("Der Wert, der von Port 0x%hx (Status-Register des Parallelports) soeben gelesen wurde ist: 0x%hx\n", b + 1, b0);
printf ("BIT3: /ERROR (in, Pin 15), BIT4: SELECTING (in, Pin 13), BIT5: PAPEREND (in, Pin 12), BIT6: /ACK (in, l->h irq, Pin 10), BIT7: BUSY (in, Pin 11)\n");
b0 = inb (b);
printf ("Der Wert, der von Port 0x%hx (Daten-Register des Parallelports) soeben gelesen wurde ist: 0x%hx\n", b, b0);
printf ("BIT0 - BIT7: Data0 - Data7 (out (mayby in), Pin 2 - 9)\n");
iopl (0); /* release region */
exit (0);
}




/* partest.c for testing the Parallel Port.
* E. g. "partest 888 255" puts the value 255 to the port 888 (e. g. lp0, lpt1)
* and prints if the port can be bidirectional and prints the status register
* Byte.
*
* This Program won't work on the sparc, where there's no concept of I/O space
* Tested with 2.2 on the x86
*
* important: this program won't work without the compiler-option -O or -02 ...
* and this program can only be run by a superuser (or another user after
* "chown root.root partest; chmod 4755 partest").
*
* It takes approx. 1,5us for one Parallel Port I/O (with an onboard Parallel Port).

The first version used a 150 Ohm resistor from a data pin to ground and another resistor to
a status pin but some parallel port do have status pins with a high level that is read (via
data port) as low.
This second version uses a 1000 uF capacitator between a data pin and ground and works correct with
every tested parallel port.
All tested parallel ports (CMOS and TTL versions), e. g. the one on the mainboard Tyan Tiger MPX,
are bidirectional.

Rolf Freitag
Lizenz: GPL

*/

#include <stdio.h>
#include <sys/io.h> /* or asm/io.h; for inb, ioperm, iopl ... */
#include <stdlib.h> /* atoi */
#include <unistd.h> // getuid()
#include <errno.h> // EPERM
#include <iso646.h> // and

int
main (int argc, char *argv[])
{ /* A wrong value can cause serious damage! */
int base = atoi (argv[1]);
int value = atoi (argv[2]);
unsigned char b = 0, b0;

if (geteuid () != 0)
{
printf ("\a\n\nError: $EUID==%d!=0 (you are not a superuser).\n\n", getuid ());
exit (-EPERM);
}
iopl (3); /* unlimited I/O access permission, nessesary above the 0x3ff limit e. g. at 0x9800=38912 */
outb (0x04, base + 2); /* write mode, interrupt disable, all controll pins high */
outb (0xb7, base + 1); // try to set the status pins high
outb (0x00, base); // write 0x00
usleep (1000000); // wait one second
outb (0x24, base + 2); /* read mode */
b = inb (base);
b0 = b; // store first byte
printf ("First read data port byte (after writing 0x00 for one second): 0x%2hhx.\n", b);
// if (b0)
// printf ("The port is not bidirectional (or damaged or something (maybe internall pullups) pulls up).\n");
outb (0x04, base + 2); /* write mode */
outb (0xff, base); // write 0xff
outb (0x24, base + 2); /* read mode */
usleep (1); // wait one microsecond
b = inb (base);
printf ("Second read data port byte (after writing 0xff for one microsecond): 0x%2hhx (should be 0xff if nothing is connected or all data pins are pulled up).\n", b);
if (b != 0xff) // port seems ok and something pulled down at second reading
printf ("The parallel port is bidirektional (or damaged)!\n");
else
{
printf ("The parallel port can be bidirektional. Check with one data pin connected to ground with 1000 uF capacitator (elko).\n");
printf ("If done so, the parallel port is not bidirektional.\n");
}
printf ("status register: %i = 0x%x\n", inb (base + 1), inb (base + 1));
outb (0x04, base + 2); /* write */
outb (value, base);
iopl (0); /* no I/O permission */
return 0;
};


Die Programme müssen mit Optimierung, z. B. -O2, kopiliert werden.

nobody0
05-01-2005, 00:19
Nachtrag: Das regelmäßige Abfragen macht man z. B. mittels udelay oder usleep zum Warten, aber sowas findet man ja auch im obigen Code.