Linux串口的中断方式和SIGIO 信号

46 篇文章 6 订阅

网络上有一些关于linux 中断方式的文章,但是都只有接收程序,没有发送。我编写中断程序时候遇到一些问题。小结一下大概有如下几点:

  1. 收发都会产生SIGIO 信号
  2. 产生SIGIO 会中断sleep,造成sleep 定时不准。

比如 原先将send 放在了主程序的while语句中

while(true)
{
send(buf,size)
sleep(1);
}

发现这个时候定时不准。SIGIO 信号一来,sleep就迫不及待地醒了。于是,我放在了一个线程中。

搞了差不多两天,总算调通了。不过,悲剧发生了,和同事交流时突然想起,为什么不开一个线程,阻塞方式等待接收数据呢?(主线程发送,子线程接收)那样就不要SIGIO了。感觉白忙活了。人生就是折腾。

还是将代码post 出来。有时,网上的一段代码简直是程序员的亲人。

#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>
#include <pthread.h>
void signal_handler_IO (int status);   /* definition of signal handler */

int n;
int fd;
int connected;
struct termios termAttr;
struct sigaction saio;
char sbuff[64];
char rbuff[64];
  int send_cnt;
 void* sender(void *)
 { int i;
     for (i=0;i<6;i++) sbuff[i]=0x30+i;sbuff[6]=0x0a;sbuff[7]=0x0d;
     send_cnt=8; 
     sleep(1);
     while(true)
     {   
         while(send_cnt>0)
         {
        int len= write(fd, &sbuff[8-send_cnt] ,1);
       if (len>0)
         send_cnt--;
         }
	    sleep(1);
        send_cnt=8;
     }
 } 
int main(int argc, char *argv[])
{    int i;
pthread_t ntid;
     fd = open("/dev/ttyACM4", O_RDWR| O_NOCTTY);// | 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_NDELAY| 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("serial configured....\n");
     
     pthread_create(&ntid,NULL,sender,NULL);
   
     while(true){
	for (i=0;i<10;i++) sleep(10);
     }

     close(fd);
     exit(0);             
}

void signal_handler_IO (int status)
{
    int nread = read(fd, rbuff, 64);
     if (nread>0)
     printf("%.*s",nread,rbuff);
  
}

 

  • 6
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值