I am trying to catch interrupts from a 633 USB device under Linux (Debian, 2.6).
This is my C code to do it:
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <pthread.h>
#define BAUDRATE B19200
main()
{
int fd;
int sig;
struct sigevent ev;
sigset_t set;
char buf[255];
struct termios oldtio, newtio;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if(fd < 0)
printf("Failed to open the device\n");
ev.sigev_notify = SIGEV_SIGNAL;
ev.sigev_signo = SIGIO;
sigemptyset(&set);
sigaddset(&set, SIGIO);
pthread_sigmask(SIG_BLOCK, &set, (sigset_t*)NULL);
if(fcntl(fd, F_SETOWN, getpid()) == -1)
printf("F_SETOWN error\n");
if(fcntl(fd, F_SETFL, FASYNC) == -1)
printf("F_SETOWN error\n");
tcgetattr(fd, &oldtio);
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
while(sigwait(&set, &sig) != -1) {
printf("INTERRUPT!\r\n");
}
tcsetattr(fd, TCSANOW, &oldtio);
}
Everything works when compiled like that, I get interrupts for all keypresses (both down up). The problem is that it doesn't work when the code is placed in any other function than main. For exampel, if I put exacly the same code in a function called test and then let main call test, I only get an interrupt when right arrow key is released.
Also, notice the struct sigevent ev that never is used, if I remove that one I won't receive any interrupts even in main. However, if I increase the size of char buf[255] it works again. This got me thinking the problem is caused by something going on with the stack.
Am I missing something?
This is my C code to do it:
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include <pthread.h>
#define BAUDRATE B19200
main()
{
int fd;
int sig;
struct sigevent ev;
sigset_t set;
char buf[255];
struct termios oldtio, newtio;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NONBLOCK);
if(fd < 0)
printf("Failed to open the device\n");
ev.sigev_notify = SIGEV_SIGNAL;
ev.sigev_signo = SIGIO;
sigemptyset(&set);
sigaddset(&set, SIGIO);
pthread_sigmask(SIG_BLOCK, &set, (sigset_t*)NULL);
if(fcntl(fd, F_SETOWN, getpid()) == -1)
printf("F_SETOWN error\n");
if(fcntl(fd, F_SETFL, FASYNC) == -1)
printf("F_SETOWN error\n");
tcgetattr(fd, &oldtio);
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
while(sigwait(&set, &sig) != -1) {
printf("INTERRUPT!\r\n");
}
tcsetattr(fd, TCSANOW, &oldtio);
}
Everything works when compiled like that, I get interrupts for all keypresses (both down up). The problem is that it doesn't work when the code is placed in any other function than main. For exampel, if I put exacly the same code in a function called test and then let main call test, I only get an interrupt when right arrow key is released.
Also, notice the struct sigevent ev that never is used, if I remove that one I won't receive any interrupts even in main. However, if I increase the size of char buf[255] it works again. This got me thinking the problem is caused by something going on with the stack.
Am I missing something?
Looking for additional LCD resources? Check out our LCD blog for the latest developments in LCD technology.