LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-15-2010, 07:08 AM   #1
PARIHAR
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Rep: Reputation: 0
segmentation fault help


the following code is giving segmentation fault. can anyone please help me in solving this problem.

#include<stdio.h>
#include <sys/io.h>
#include <unistd.h>
int ioperm();
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <termios.h>
#include <time.h>
#include <string.h>

#define PORT 0x03F8 /* the address of COM1 */

#define THR 0x0000
#define RDR 0x0000
#define BRDL 0x0000
#define IER 0x0001
#define BRDH 0x0001
#define LCR 0x0003
#define MCR 0x0004
#define LSR 0x0005
#define MSR 0x0006

/* File descriptors for the ports */
int fd1,fd2;
//char *buff,*buffer;//*bufptr;

/* Opening port one for read and write */
int open_port1(void)
{
fd1 = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
{
fcntl(fd1, F_SETFL, 0);
printf(" Port 1 has been sucessfully opened and %d is the file descriptor\n",fd1);
}
return (fd1);
}

int configure_port(int fd) // configure the port
{
struct termios port_settings; // structure to store the port settings in

cfsetispeed(&port_settings, B9600); // set baud rates
cfsetospeed(&port_settings, B9600);

port_settings.c_cflag &= ~PARENB; // set no parity, stop bits, data bits
port_settings.c_cflag &= ~CSTOPB;
port_settings.c_cflag &= ~CSIZE;
port_settings.c_cflag |= CS7;
port_settings.c_cc[VMIN] = 5;
port_settings.c_cc[VTIME] = 10;


tcsetattr(fd, TCSANOW, &port_settings); // apply the settings to the port
return(fd);

} //configure_port

int main()
{
open_port1();
//open_port2();
configure_port(fd1);

outb(0x08,PORT+MCR); /* interrupt enable */
outb(0x01,PORT+IER); /* interrupt as data in */
int j;
unsigned char rdat[17];
//int msg[]={0x3A,0x30,0x31,0x30,0x06,0x32,0x30,0x30,0x30,0x04,0x04,0x00,0x00};
//char msg1[]={':','0','1','0','6','2','0','0','0','0','0','0','0','D','9','\x','0','D','\x','0','A'};
//char msg2[]={':','0','1','0','6','2','0','0','0','0','0','0','2','D','7','\x','0','D','\x','0','A'};
char msg3[]={':','0','1','0','3','2','1','0','2','0','0','0','2','D','7','\r','\n'};
for(j=0;j<17;j++)
{
while(!(inb(PORT+LSR) & 0x20)); /* wait until THR empty */
outb(msg3[j],PORT+THR); /* send data to THR */
}
for(i=0;i<=16;i++)
{
if(inb(PORT+LSR) & 0x01)
{ /* b0==1, read data ready */
rdat[i]=inb(PORT+RDR); /* read data form RDR */
}
}
printf("The response message is %s",rdat);
}

Last edited by PARIHAR; 04-15-2010 at 07:09 AM.
 
Old 04-15-2010, 07:18 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,960

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by PARIHAR View Post
the following code is giving segmentation fault. can anyone please help me in solving this problem.
}
Since you don't say anything about version/distro of Linux, or what error(s) you're getting, or what this program DOES, we probably can't.

Short answer: your code is broken, fix it.
 
Old 04-16-2010, 01:17 AM   #3
PARIHAR
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
Since you don't say anything about version/distro of Linux, or what error(s) you're getting, or what this program DOES, we probably can't.

Short answer: your code is broken, fix it.
we are using this code on fedora.. When we compile the code using g++ it gets compiled but when we try to execute it says "segmentation error".. We are using this code to communicate with a motor drive using RS 232 to RS 485 converter.. we are using ASCII mode with 7 data bits, 2 stop bits and no parity bit.. a similar program is working in windows hence all the hardware is working fine..
 
Old 04-16-2010, 03:35 AM   #4
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,672
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Did you read the following :
http://linux.die.net/man/2/inb

Quote:
You use ioperm(2) or alternatively iopl(2) to tell the kernel to allow the user space application to access the I/O ports in question. Failure to do this will cause the application to receive a segmentation fault.
Since you have not indented your code or used code tags it difficult to read your code!
http://www.phpbb.com/community/faq.php?mode=bbcode

Last edited by TheIndependentAquarius; 04-16-2010 at 03:58 AM.
 
Old 04-22-2010, 04:54 AM   #5
PARIHAR
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
well now we are able to communicate thro the serial port. We are able to send a message successfully but we are unable to read the response data from the port. We actually expect a 17 byte message from the external device. So we have used for loop for reading the complete message.

[I]for(i=0;i<=16;i++)
{
rdat=inb(PORT); /* read data from PORT */
}



Where PORT=0x3F8

But we are getting only one char 16 times.
What changes should we make to get the desired response??
 
Old 04-22-2010, 05:19 AM   #6
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,672
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Have you used inb() inside an interrupt handler service routine ??
 
Old 04-23-2010, 01:19 AM   #7
PARIHAR
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
well we are using it in the following manner

i=0;
while(i<22)
{ /* b0==1, read data ready */
if(inb(LSR) & 0x01)
{
rdat[i++]=inb(PORT);
}
}


where LSR is the Line status register(PORT+5) and PORT is the address of serial port 0x3F8

Last edited by PARIHAR; 04-23-2010 at 01:21 AM.
 
Old 04-23-2010, 01:57 AM   #8
TheIndependentAquarius
Senior Member
 
Registered: Dec 2008
Posts: 4,672
Blog Entries: 29

Rep: Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917Reputation: 917
Yes but i am asking in which function is this code placed ? Is that function an Interrupt service routine ?

EDIT:
Do you know what a Interrupt service routine is ?

Last edited by TheIndependentAquarius; 04-23-2010 at 02:17 AM.
 
Old 05-03-2010, 07:24 AM   #9
PARIHAR
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
No Its not an ISR. It is a part of the main function.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault Bjorne Debian 3 05-23-2007 11:11 AM
yast segmentation fault, system freezing - nvidia driver at fault? BaltikaTroika SUSE / openSUSE 2 12-02-2005 10:34 AM
Segmentation fault with ls -l Sledge Fedora 8 03-29-2005 09:02 PM
Why do i get a Segmentation fault laclac01 Programming 20 12-10-2004 10:33 AM
segmentation fault for everything gsv Linux - Newbie 1 08-23-2004 07:19 PM


All times are GMT -5. The time now is 04:57 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration