access to serial port

Hi,
I wanted to test the serial port, i checked it on my laptop, I expected to have a pointer null or something like this.
Unfortunately I got "segmentation fault"
Is this program correct to detect serial port . I am working on linux.


#include <stdio.h>
// Just to check if serial ports are activated
int main(){
unsigned int *ptr, address;
int a,k;
ptr=(unsigned int *)0x00000400;
printf(" ptr = %X give a number (for pause )", (unsigned int)ptr);
scanf("%d",&k);
for(a=0; a<4; a++){
printf(" ptr = %X *ptr=%d give number", (unsigned int) ptr, *ptr);
scanf("%d",&k);
address =*ptr;
if (address == 0)
printf("The port COM%d disabled \n",a+1);
else
printf("The port COM%u is %X \n",a+1, address);
*ptr++;
}
}
/*
$ gcc port.c -o portSer
~$ ./portSer
ptr = 400 give a number (for pause )1
Segmentation fault

the error is at line 11 (just after for ) , if I remove *ptr
the program continues until lines 13 ( address =*ptr, so the problem is in
*ptr the object pointed

*/

thanks a lot
best regards
You can't just access the port like that -- the kernel won't let you.

You need to open the special device file /dev/ttyS* to read/write the serial port.
Topic archived. No new replies allowed.