我试图在ubuntu中设置串口的中断(在用C编写的程序中),但它不起作用.我已经检查过串口通讯是否正常工作而没有中断,所以我可能会设置错误.
代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
void signal_handler_IO (int status); /* definition of signal handler */
int n;
int fd;
int connected;
struct termios termAttr;
struct sigaction saio;
int main(int argc, char *argv[])
{
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyO1\n");
exit(1);
}
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
fcntl(fd, F_SETFL, FNDELAY);
fcntl(fd, F_SETOWN, getpid());
tcgetattr(fd,&termAttr);
baudRate = B115200;
cfsetispeed(&termAttr,B115200);
cfsetospeed(&termAttr,B115200);
termAttr.c_cflag &= ~PARENB;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
tcsetattr(fd,TCSANOW,&termAttr);
printf("UART1 configured....\n");
connected = 1;
while(connected == 1){
// some code
}
close(fd);
exit(0);
}
void signal_handler_IO (int status)
{
printf("received data from UART.\n");
}
因此,任何时候其他设备通过配置的端口发送消息,消息“从UART接收数据”.永远不会显示.
有什么建议可以解决这个问题吗?另外,系统如何将中断与串口相关联?,我已经读过有关signal.h但我没有找到答案.我从这个页面得到了中断的想法:http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html
在此先感谢您的帮助.
提前致谢.