Segmentation fault (core dumped)

Asked by cjones

I'm compiling with gcc and with -O2 optimisation.
Here's the code:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/io.h>
#define PORT1 0x3F8

main()
{

if (ioperm(PORT1, 4, 1)) {perror("ioperm"); exit(1);}
char a;
outb_p(0 , PORT1 + 1); /* No interupts*/
outb_p(0x80 , PORT1 + 3); /* DLab*/
outb_p(0x02 , PORT1 + 0); /*Baud Rate--Low byte?*/
outb_p(0x03 , PORT1 + 3); /* * 8 Bits, No Parity, 1 Stop Bit */
/*outb_p(0x0B , PORT1 + 4); /* Turn on DTR, RTS, and OUT2 */

/*Roomba Control*/
printf("Roomba go!\n");

outb_p(128, PORT1); /* Begin SCI*/
outb_p(130, PORT1); /* Go To Safe Mode*/
outb_p(132, PORT1); /* Go To Full Mode*/

outb_p(137, PORT1);
outb_p(255, PORT1);
outb_p(56, PORT1);
outb_p(128, PORT1);
outb_p(0, PORT1);

while(a != 'a')
{
a = getchar();
}
printf("Roomba stop!\n");

outb_p(137, PORT1);
outb_p(0, PORT1);
outb_p(0, PORT1);
outb_p(0, PORT1);
outb_p(0, PORT1);
return(0);
}
I have to use sudo to run it or the ioperm won't work. I get a segmentation fault (core dumped) error.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Istvan Szekeres (szekeres) said :
#1

Hello,

one possible reason might be that the ports 0x3f8- are in use by the serial driver, so the kernel prevents messing with those ports. You could try to remove the serial driver before running this program.

Revision history for this message
cjones (cjones-designwesteng) said :
#2

Are you sure this is best? I thought that the driver was necessary for the port, and I also thought that Linux might allow direct communication with ports. As you can probably tell I'm new to C programming.

Revision history for this message
Istvan Szekeres (szekeres) said :
#3

linux allows direct communication with ports however normally it is the kernel's ("serial" module) job to do it. What exactly would you like to do? All normal tasks can be done without doing the port programming by simply using the serial driver.

http://www.lafn.org/~dave/linux/Serial-Programming-HOWTO.txt contains a nice tutorial about how to use it.

Revision history for this message
Istvan Szekeres (szekeres) said :
#4

http://tldp.org/HOWTO/Serial-HOWTO.html contains more generic information about linux+serial ports.

Can you help with this problem?

Provide an answer of your own, or ask cjones for more information if necessary.

To post a message you must log in.