Re: Serial interrupt
- From: floyd@xxxxxxxxxx (Floyd L. Davidson)
- Date: Sun, 26 Mar 2006 05:50:31 -0900
"Manu" <info@xxxxxxxx> wrote:
Hi
I want to generate an interrupt and launch a function everytime a char is
recieve with the serial port. (/dev/ttyS0)
How to do this Please ?
Tanks
Manu
Here are the significant parts of a program that enables SIGIO for a
serial port and then simply reads data from the port and writes it to
stdout. For the complete program see,
<http://www.apaflo.com/floyd_davidson/terminal/>
volatile sig_atomic_t read_flag = 1;
void
sig_handler(int sig)
{
read_flag = sig;
}
int
serial_cnfg(int fd)
{
struct termios tty;
struct sigaction sa;
sa.sa_handler = sig_handler;
sa.sa_flags = 0;
sa.sa_restorer = NULL;
sigemptyset(&sa.sa_mask);
sigaction(SIGIO, &sa, NULL);
fcntl(fd, F_SETOWN, getpid()); /* allow the process to receive SIGIO */
fcntl(fd, F_SETFL, O_ASYNC); /* Make the file descriptor asynchronous */
tcgetattr(fd, &tty);
/* raw io, hardware flow control, 8n1 */
tty.c_iflag = (IGNBRK | IGNPAR); /* input flags */
tty.c_cflag = CRTSCTS | CS8 | CREAD; /* control flags */
tty.c_lflag = 0; /* local flags */
tty.c_oflag = 0; /* output flags */
tty.c_cc[VMIN] = 1; /* wait for 1 byte */
tty.c_cc[VTIME] = 0; /* turn off timer */
#ifdef __linux__
/* for linux only */
tty.c_line = N_TTY; /* set line discipline */
#endif
return tcsetattr(fd, TCSADRAIN, &tty);
}
int main(void)
{
...
while (!stop) {
if (read_flag) {
unsigned char s[MAX_INPUT];
int cnt;
/* sig_handler() has flagged input data is available */
/* get input from serial port and write it to stdout */
switch (cnt = read(fd, &s, MAX_INPUT)) {
default:
write(STDOUT_FILENO, &s, cnt); /* write char to stdout */
break;
case -1:
if (errno != EAGAIN) {
perror("serial port read()");
exit(EXIT_FAILURE);
}
case 0:
break;
}
read_flag = 0;
}
/* get input from keyboard and write to serial port */
ch = getchar();
switch (ch) {
case EXITSEQUENCE:
fprintf(stderr, "Exit terminal program? Y/n ?\b");
if (('N' == (ch = getchar())) || 'n' == ch) {
fprintf(stderr, "\rExit aborted... continuing. \r\n");
break;
}
stop = 1;
tcsetattr(STDIN_FILENO, TCSANOW, &otty);
fprintf(stderr, "\n\r\n");
break;
case EOF:
/*
* getchar() was interupted by SIGIO, and returns an
* error when it resumes. We ignore it.
*/
break;
default:
{
unsigned char c;
c = (unsigned char) ch;
write(fd, &c, 1);
#if LOCALECHO
write(STDOUT_FILENO, &c, 1);
#endif
}
}
}
return EXIT_SUCCESS;
}
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@xxxxxxxxxx
.
- References:
- Serial interrupt
- From: Manu
- Serial interrupt
- Prev by Date: Re: Serial interrupt
- Next by Date: Re: feature tests for pthreads implementation and configuration?
- Previous by thread: Re: Serial interrupt
- Next by thread: GNU Make implicit rules
- Index(es):
Relevant Pages
|