Re: Serial interrupt



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



Relevant Pages

  • Re: stop application
    ... input seems to be an int or char, ... > But the main problem still exists: I'm reading data from a serial port. ... the while loop runs in the ...
    (comp.lang.c)
  • Re: Serial port monitoring
    ... There is no need for a timer; you want to get data when it is available, ... line in the serial port, then by the time of the next trigger the rest has ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)
  • Re: Serial port monitoring
    ... There is no need for a timer; you want to get data when it is available, ... The main thing is that every 100ms or so you check the serial port and read ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)
  • Re: Serial port monitoring
    ... There is no need for a timer; you want to get data when it is available, ... line in the serial port, then by the time of the next trigger the rest has ... I would then have a loop that checks for char in serial buffer. ...
    (microsoft.public.vc.mfc)
  • Re: Serial port monitoring
    ... I would then have a loop that checks for char in serial buffer. ... Then I would return to the loop which just sits there looking for chars in serial buffer. ... The built in serial port device driver handles inputting and queues the input to a buffer for you. ...
    (microsoft.public.vc.mfc)