LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2007, 02:18 AM   #1
gzz
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Rep: Reputation: 0
Cannot catch SIGINT while serial break condition occurs


I setup termios structure with IGNBRK is not set and BRKINT is set.
To allow the process to receive signals I call: fcntl(fd, F_SETOWN, getpid());
I have made a signal handler to catch all signals. I can catch SIGIO when i send some data over serial and SIGINT when pressing ctrl+c, but when I send break signal over serial then it cannot catch SIGINT.
Is there anything I'm doing wrong?
Any suggestions and code samples would be appreciated.

Thanks
 
Old 09-17-2007, 01:19 AM   #2
gzz
LQ Newbie
 
Registered: Sep 2007
Posts: 2

Original Poster
Rep: Reputation: 0
I couldn't find any code how to receive SIGINT from serial driver while break condition occures, so I post it here, this code will do the trick.
Is there any suggestions that I should or should not do?

#include <sys/ioctl.h>
#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
void signal_handler(int status);

int main(void)
{
int fd;

switch (fork()) {
case 0:
break;
case -1:
// Error
printf("Error demonizing (fork)! %d - %s\n", errno, strerror(errno));
exit(0);
break;
default:
_exit(0);
}

if (setsid() == -1)
{
printf("Error demonizing (setsid)! %d - %s\n", errno, strerror(errno));
exit(0);
}
fd = open ("/dev/ttyS0", O_RDWR, 0 );

struct sigaction saio; /* definition of signal action */
saio.sa_handler = signal_handler;
// saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGINT,&saio,NULL);
struct termios options;
memset (&options, 0x00, sizeof (options));
options.c_cflag |= (CREAD);
options.c_cflag |= CLOCAL;
options.c_cflag |= CS8; // Select 8 l2_data bits
options.c_iflag |= (BRKINT);
// options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 1;
if (tcsetattr (fd, TCSAFLUSH, &options) == -1)
{
printf("port setup failure\n");
return -1;
}
ioctl(fd, TIOCSCTTY, (char *)NULL);
getchar();
}

void signal_handler(int status)
{
printf("received SIGINT %d signal.\n", status);
exit(0);
}

Last edited by gzz; 09-17-2007 at 08:00 AM.
 
  


Reply



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
SIGINT problem brentos Linux - General 1 12-17-2008 10:43 PM
overriding SIGINT xface66 Programming 3 04-02-2007 01:32 PM
SIGINT handling santhosh.bachu Linux - General 1 07-18-2006 01:18 AM
Detecting connection break on serial port listener on linux neelc20 Programming 6 12-04-2003 01:08 AM
Detecting connection break on serial port listener on linux neelc20 Linux - Newbie 3 11-29-2003 03:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:07 AM.

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
Open Source Consulting | Domain Registration