I'm trying to write a very simple program to open and close com 2. A good friend furnished me with this and I can't make it work on Mandrake 8.
If there is any help out there it would be appreciated.
Bo
/*
* relay.c: very simple port I/O
*
* Compile with `gcc -O2 -o relay relay.c',
* and run as root with `./relay'.
* This program reads from standard in until it gets a EOF (^D)
* to use in unattended mode just run as relay < inputfile
*/
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#define BASEPORT 0x3FC /*com 1 */
define BASEPORT 0x2FC /*com 2 */
int main()
{
int a;
int x;
/* Get access to the ports */
if (ioperm(BASEPORT, 1, 1)) {
fprintf(stderr,"error performing ioperm\n"
exit(1);
}
do {
a=fgetc(stdin);
if (a == EOF) continue; // Jump to end of loop on end condition
x = inb(BASEPORT);
if (a == 49) x &= ~2; // If you get a 0 in clear relay #1
if (a == 50) x |= 2; // If you get a 1 in set relay #1
if (a == 51) x &= ~1; // If you get a 2 in clear relay #2
if (a == 52) x |= 1; // If you get a 3 in set relay #3
outb(x, BASEPORT);
usleep(100); // Wait 100 microseconds.
} while (a != EOF);
/* We don't need the ports anymore */
if (ioperm(BASEPORT, 1, 0)) {
fprintf(stderr,"error closing ioperm\n"
exit(1);
}
exit(0);
}
If there is any help out there it would be appreciated.
Bo
/*
* relay.c: very simple port I/O
*
* Compile with `gcc -O2 -o relay relay.c',
* and run as root with `./relay'.
* This program reads from standard in until it gets a EOF (^D)
* to use in unattended mode just run as relay < inputfile
*/
#include <stdio.h>
#include <unistd.h>
#include <asm/io.h>
#define BASEPORT 0x3FC /*com 1 */
define BASEPORT 0x2FC /*com 2 */
int main()
{
int a;
int x;
/* Get access to the ports */
if (ioperm(BASEPORT, 1, 1)) {
fprintf(stderr,"error performing ioperm\n"
exit(1);
}
do {
a=fgetc(stdin);
if (a == EOF) continue; // Jump to end of loop on end condition
x = inb(BASEPORT);
if (a == 49) x &= ~2; // If you get a 0 in clear relay #1
if (a == 50) x |= 2; // If you get a 1 in set relay #1
if (a == 51) x &= ~1; // If you get a 2 in clear relay #2
if (a == 52) x |= 1; // If you get a 3 in set relay #3
outb(x, BASEPORT);
usleep(100); // Wait 100 microseconds.
} while (a != EOF);
/* We don't need the ports anymore */
if (ioperm(BASEPORT, 1, 0)) {
fprintf(stderr,"error closing ioperm\n"
exit(1);
}
exit(0);
}