Linux串口信号中断

#include <sys/msg.h>
#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/ipc.h>
 
/*信号相关*/
#include<sys/signal.h>
 
/*信号量相关*/
#include <semaphore.h>
 
/*串口相关*/
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>
 
int wait_flag =1;
int stop = 1;
unsigned char buf[255] = {0};
int serial_fd;
/******************************************
 信号处理函数,设备wait_flag=FASLE
 ******************************************************/
void signal_handler_IO(int status)
{
   printf("received SIGIO signale.\n");
   wait_flag = 0;
}
void ReceiveTaskHandler(void *argv)
{
 
    int res, i;
    while(1)
     {
       //usleep(100);
       /* after receving SIGIO ,wait_flag = FALSE,input is availabe and can be read*/
       if(wait_flag == 0)
       {
         memset(buf,0,255);
         res = read(serial_fd,buf,255);
         printf("nread=%d\n",res);
         for(i=0 ;i <res; i++)
         {
             printf("%02x\n",buf[i]);
          
         }
          wait_flag = 1; /*wait for new input*/
       }
     }
}
int main()
{
    struct sigaction saio;
 
    struct termios old_cfg;
    struct termios new_cfg;
 
    //1.打开串口设备文件
    serial_fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);

    if (serial_fd == -1)
    {
        perror("open");
        exit(-1);
    }
    printf("serial configured....\n");
 
    //2.备份现有的串口配置
    if (-1 == tcgetattr(serial_fd, &old_cfg))
    {
        perror("tcgetattr");
        exit(-1);
    }
 
    //3.原始模式
    new_cfg = old_cfg;
    cfmakeraw(&new_cfg);
 
    //4.配置波特率
    cfsetispeed(&new_cfg, 9600);
    cfsetospeed(&new_cfg, 9600); 
    //5.设置控制标志
    new_cfg.c_cflag |= CREAD | CLOCAL;                                          //使能数据接收和本地模式
 
    //6.设置帧结构
    new_cfg.c_cflag &= ~CSTOPB;                                                 //1位停止位
    new_cfg.c_cflag &= ~CSIZE;                                                  //去掉数据位屏蔽
    new_cfg.c_cflag |= CS8;                                                     //8位数据位
    new_cfg.c_cflag &= ~PARENB;                                                 //无校验
 
    //7.设置阻塞模式
    tcflush(serial_fd, TCIOFLUSH);
 
    //无阻塞
    new_cfg.c_cc[VTIME] = 0;
    new_cfg.c_cc[VMIN] = 0;
    tcflush(serial_fd, TCIOFLUSH);
 
    /*注册信号*/
    saio.sa_handler = signal_handler_IO;
    sigemptyset(&saio.sa_mask);
    saio.sa_flags = 0;
    saio.sa_restorer = NULL;
    sigaction(SIGIO, &saio, NULL);
 
    //allow the process to receive SIGIO
    fcntl(serial_fd, F_SETOWN, getpid());
    //make the file descriptor asynchronous
    fcntl(serial_fd, F_SETFL, FASYNC);
 
    //8.使能配置生效
    if (-1 == tcsetattr(serial_fd, TCSANOW, &new_cfg))
    {
        perror("tcgetattr");
        exit(-1);
    }
    pthread_t id;
    pthread_create(&id, NULL, (void *)ReceiveTaskHandler, NULL);
    pthread_join(id,NULL);
    close(serial_fd);
    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux中,可以使用中断来处理串口数据的接收和发送。在没有操作系统的情况下,可以使用UART的中断来处理数据的接收和发送。而在Linux操作系统下,可以使用软中断的方式来处理数据的接收和发送,其中主要使用的是信号SIGIO,也就是异步I/O。可以参考《UNIX环境高级编程》中的第14章和第18章,这两章描述了串口编程和异步I/O方面的内容。此外,还有一本书《linux serial programming how-to》和《Serial Programming Guide for POSIX Operating Systems》,这些都是串口编程的必读和经典书籍。在Linux中,串口参数的设置包括波特率、起始位数量、数据位、停止位和流控协议。在接收端和发送端要配置成相同的参数设置。在Linux中,串口设备文件一般位于"/dev"目录下,串口一对应的设备名是"/dev/ttyS0",串口二对应的设备名是"/dev/ttyS1"。可以通过查看"/dev"目录下的文件来确认。在串口编程中,可以使用select函数来实现多个串口的读写。在代码中,可以使用open_port函数打开串口,使用set_opt函数设置串口参数,然后使用select函数来实现串口的读写。具体的代码示例可以参考引用\[3\]中的代码。 #### 引用[.reference_title] - *1* *2* *3* [Linux串口编程(中断方式和select方式)](https://blog.csdn.net/mao0514/article/details/118049670)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值