Help - Search - Members - Calendar
Full Version: ioperm segmentation error
Linuxhelp > Support > Programming in Linux
linuxNoob17635
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");
}
}
michaelk
You have several errors.
You need to include string.h. Even though the program compiles without errors your statement dir = argv[1][0]; will not work without it.
You need to get access ( i.e. ioperm ) to the parallel port before calling your subroutines and you must be root.

See http://tldp.org/HOWTO/IO-Port-Programming.html
linuxNoob17635
QUOTE (michaelk @ Oct 11 2008, 09:08 AM) *
You have several errors.
You need to include string.h. Even though the program compiles without errors your statement dir = argv[1][0]; will not work without it.
You need to get access ( i.e. ioperm ) to the parallel port before calling your subroutines and you must be root.

See http://tldp.org/HOWTO/IO-Port-Programming.html


I implemented those changes. Working fine now. Thanks for that.
michaelk
Excellent and thanks for replying back that your program is working.
BTW what type of motor controller are you using?
linuxNoob17635
QUOTE (michaelk @ Oct 11 2008, 06:00 PM) *
Excellent and thanks for replying back that your program is working.
BTW what type of motor controller are you using?


I'm using a H-bridge (consists of just a few transistors and resistors) to control a small DC motor (mili amp ranges).
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2017 Invision Power Services, Inc.