开发者社区> 问答> 正文

SIGIO什么时候解雇?

我正在尝试接收串行数据,但是我无法确定时间。

当接收到串行数据时,什么时候SIGIO会触发-接收到数据的第一个字节时,还是当接收到特定字符(CR,NL ...)时?

设置串行数据端口:

/*  Open serial port.
*   O_RDONLY - Only receive data
*   O_NOCTTY - Leave process control to other 'jobs' for better portability.
*   O_NDELAY - Enable non-blocking read
*/
fd = open(PORT_PATHNAME, O_RDONLY | O_NOCTTY | O_NDELAY);
fd = open(PORT_PATHNAME, O_RDONLY | O_NOCTTY | O_NDELAY);
/* Set process ID, that will receive SIGIO signals for file desc. events */
fcntl (fd, F_SETOWN, getpid());
/* Enable generation of signals */
fcntl (fd, F_SETFL, O_ASYNC);

...

options.c_oflag &= ~OPOST;

/* Set terminal options using the file descriptor */
tcsetattr(fd, TCSANOW, &options);

struct termios options;
tcgetattr(fd, &options);

/* Set baudrate */
options.c_cflag = B115200;

设置信号中断:

/* Add UART handler function */
saio.sa_handler = signal_handler_IO;
/* Non-zero used for calling sighandler on alternative stacks and so on */
saio.sa_flags = 0;
/* Not specified by POSIX -> not in use */
saio.sa_restorer = NULL;
/* Bind SIGIO (async I/O signals) to the defined structure */
int status = sigaction(SIGIO, &saio, NULL);

展开
收起
祖安文状元 2020-01-06 16:28:39 479 0
1 条回答
写回答
取消 提交回答
  • 收到一字节数据后,SIGIO应该立即启动。

    这是在带驱动程序的Linux上测试的最小示例ftdi_sio:

    #include <stdio.h>
      #include <stdlib.h>
      #include <fcntl.h>
      #include <time.h>
      #include <unistd.h>
      #include <termios.h>
      #include <errno.h>
      #include <string.h>
      #include <sys/types.h>
      #include <sys/uio.h>
      #include <signal.h>      
      #include <termios.h>     
      #include <unistd.h>     
    
      #define tty   "/dev/ttyUSB0"
      #define baudrate  B9600
    
    
    
      void byteCallback(int status);      // interrupt function 
    
    
    
      int main(){
    
        int ttyDev = open(tty, O_RDWR | O_NOCTTY | O_NONBLOCK);
    
        if (!ttyDev)
        {
          printf("couldn't open serial device");
          return -1;
        }
        struct sigaction byteAction;      // set the serial interrupt handler
        struct termios oldtio, newtio; 
        //create signal handler
        byteAction.sa_handler = byteCallback;
        sigemptyset(&byteAction.sa_mask); //sa_mask = 0
        byteAction.sa_flags = SA_RESTART;
        sigaction(SIGIO, &byteAction, NULL);
    
        //Allow process to detect SIGIO
        fcntl(ttyDev, F_SETOWN, getpid());
            fcntl(ttyDev, F_SETFL, FASYNC);
    
    
        tcgetattr(ttyDev, &oldtio); //backup current settings
        newtio.c_cflag = baudrate | CS8 | CLOCAL | CREAD;
        newtio.c_cflag &= ~CRTSCTS; //disable hw flow control
        newtio.c_iflag &= ~(IXON | IXOFF | IXANY); //disable flow control
        newtio.c_iflag |= IGNPAR; //ignore parity
        newtio.c_oflag = 0;
        newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //raw mode
        newtio.c_cc[VMIN] = 1; 
        newtio.c_cc[VTIME] = 0;
        tcflush(ttyDev, TCIFLUSH);
        tcsetattr(ttyDev, TCSANOW, &newtio);
    
            while(1){
                //Wait for SIGIO to fire
            }
    
      }
    
      void byteCallback(int status){
            printf("SIGIO fired!\r\n");
    
    
      }
    
    2020-01-06 16:28:48
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
创办公司手续指南 立即下载
女性移动App安全攻防战 立即下载
打赢企业信息安全这场仗 立即下载