设置在linux下串口中断 [英] setting serial port interruption in linux
问题描述
我想设置为中断在Ubuntu串行端口(用C编写的程序),但它不工作。我已经检查串行通讯正常工作没有中断,所以我可能会设置一些错误。
在code是以下内容:
I am trying to set the interruption for a serial port in ubuntu (in program written in C), but it does not work. I have checked that the serial communication works correctly without the interruption, so I may be setting something wrong. The code is the following:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
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接收到的数据。永远不会显示。
So anytime time another device send a message through the configured port, the message "received data from UART." is never displayed.
任何建议来解决这个问题?此外,如何系统涉及与串口中断?我看了一下signal.h中,但我还没有找到一个答案。我从这个页面中断理念:<一href=\"http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html\">http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html
Any suggestion to solve this problem? Also, how does the system relate the interruption with the serial port?, I have read about signal.h but I have not found an answer for that. I got the interruption idea from this page: http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html
感谢您事先的任何帮助。
先谢谢了。
Thanks in advance for any help. Thanks in advance.
推荐答案
的问题是,你用<清除 FASYNC
标志禁止从文件描述符信号code> F_SETFL 。您需要设置,如果你想获得的信号:
The problem is that you're disabling signals from the file descriptor by clearing the FASYNC
flag with F_SETFL
. You need to set that if you want to get signals:
fcntl(fd, F_SETFL, FNDELAY|FASYNC);
此外,您可能想使用POSIX的名字,这些标志( O_NDELAY
和 O_ASYNC
),而不是更多的可移植性BSD的名字,虽然二者将工作在Linux上。
Also, you might want to use the POSIX names for these flags (O_NDELAY
and O_ASYNC
) rather than the BSD names for more portability, though either will work on Linux.
这篇关于设置在linux下串口中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!