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.
|