-->

设置在linux下串口中断(setting serial port interruption in

2019-07-20 15:49发布

我想设置中断了在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

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

Answer 1:

问题是,你被清除禁用从文件描述符信号FASYNC与标志F_SETFL 。 您需要设置,如果你想获得的信号:

fcntl(fd, F_SETFL, FNDELAY|FASYNC);

此外,您可能需要使用这些标志的POSIX名称( O_NDELAYO_ASYNC ),而不是BSD的名字更多的便携性,虽然二者将在Linux上运行。



Answer 2:

我发现了一段代码缺少预期的原始代码工作。 下面的代码是工作在Linux上用gcc编译。 添加的代码行是一个标有/**<<<<<<------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

我希望,也为你的作品。



Answer 3:

任何建议来解决这个问题? 此外,系统怎样涉及与串口中断?

串口中断在Linux内核的串口驱动的设备部分里面写。 中断是由司机自己处理,这样你就可以在它已经无法控制。

上面有哪些程序正在做的是,通知您通过一个信号,当接收串行部分设备的中断被触发。

上述的信号处理是不相关的中断中,更甚至处理。 首先,你注册,你的程序,得到一个信号,当一个IO中断occours,在你的程序中的信号处理程序将然后显示printf的消息。

我想在你的程序处理信号,无法正常执行只是纠正你的节目的信号部分

sigset_t mskvar_1                   //Variable of signal bitfieldtype
struct sigaction sigio_action       //Structure which describes signal handler
void sio_handler(void);             //Signal handler function

int main()
{
  sigfillset(&mskvar_1);                    //set all mask bits of maskbit variable
  sigprocmask(SIG_SETMASK,&mskvar_1,NULL);  //write the mask info present in mskvar_1 to the pd
  sigdelset(&mskvar_1,SIGIO);               //Unmask SIGIO , to register for IO Interrupt Events

  sigio_action.sa_handler = sio_handler;    //Configure Signal Handler
  sigio_action.sa_flags  = 0;
  sigfillset(&sigio_action.sa_mask);
  sigaction(SIGIO,&sigio_action,NULL);      //Install Signal handler

  // Serial port initializtion here
  // Set Serial port parameters , Baud Rate and flags


while(1);
return 0;
}


void sio_handler()
{
  printf("\nSIGIO RECEIVED , I/O interrupt signalled?\n");
  return;
}


文章来源: setting serial port interruption in linux