c - linux设置串口中断

标签 c ubuntu serial-port interrupt interruption

我试图在 ubuntu 中设置串行端口的中断(在用 C 编写的程序中),但它不起作用。我已经检查过串行通信是否正常工作而没有中断,所以我可能设置了错误。 代码如下:

    #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 接收数据”。永远不会显示。

有什么解决这个问题的建议吗?另外,系统如何将中断与串口联系起来?我已经阅读了 signal.h 但我还没有找到答案。我从这个页面得到了中断的想法:http://www.faqs.org/docs/Linux-HOWTO/Serial-Programming-HOWTO.html

在此先感谢您的帮助。 提前致谢。

最佳答案

我发现原始代码无法按预期工作,但缺少一段代码。下面的代码适用于使用 gcc 编译的 Linux。添加的代码行是标有 /**<<<<<<------This line made it work.**/ 的代码行
一行也被注释掉了://baudRate = B115200; .

#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());
     fcntl(fd, F_SETFL,  O_ASYNC ); /**<<<<<<------This line made it work.**/

     tcgetattr(fd,&termAttr);
     //baudRate = B115200;          /* Not needed */
     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");
}

程序的输出是:

./a.out 
UART1 configured....
received data from UART.
received data from UART.
received data from UART.
^C

我希望这对你也有用。

关于c - linux设置串口中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15119412/

相关文章:

c - 为什么我的访问权限不好

c - 具有自动重传问题的强大串行协议(protocol)

linux - 在大型 txt 文件中搜索和查找位置

python - Pymodbus : Wrong byte count in response

C++ COM口的打开、读写

c - 我需要遍历 getaddrinfo() 吗?

c - Visual Studio : Callback function problem (__cdecl *)

ubuntu - 试图在 ubuntu 下的 code::blocks 中运行 ogre 项目

ubuntu - Asterisk 和 a2billing 通话问题

python - 使用 Python 进行实时串行数据记录 - 设计建议