Hi guys, I'm having trouble with my program accessing the parallel port. I'm getting a segmentation error when I run it.
I'm quite sure that it has something to do with the way I'm using ioperm. Am I looking in the right direction?
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <sys/io.h>
#include <time.h>
#include <unistd.h>
#define base 0x0378
void forward(int high, int low);
void reverse(int high, int low);
char dir;
int value, high, low;
int main(int argc, char *argv[])
{
dir = argv[1][0];
value = atoi(argv[2]);
high = ((100 - value) * 10);
low = (1000 - high);
if (argc!=3)
{
fprintf(stderr, "Error: Wrong number of arguments. Please enter direction (d or r) followed by speed (0 - 100).\n"), exit(1);
}
if ((value<0) || (value>100))
{
fprintf(stderr, "Error: motor speed must be between 0 and 100.\n"), exit(1);
}
if (dir == 'f')
{
forward(high,low);
}
if (dir == 'r')
{
reverse(high,low);
}
if (ioperm(base,1,1))
{
fprintf(stderr, "Couldn't get the port at %x\n", base), exit(1);
}
return 0;
}
void forward(int high, int low)
{
for(;;)
{
printf("Motor running forward.\n");
usleep(high);
outb(0x0F, base);
printf("high\n");
usleep(low);
outb(0x0, base);
printf("low\n");
}
}
void reverse(int high, int low)
{
for(;;)
{
printf("Motor running in reverse.\n");
usleep(high);
outb(0xF0, base);
printf("high\n");
usleep(low);
outb(0x0, base);
printf("low\n");
}
}
