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;
}
 

以下是一个在Linux系统下实现串口接收中断的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> #include <signal.h> #include <sys/ioctl.h> #include <linux/serial.h> #define UART_DEV "/dev/ttyUSB0" // 中断处理函数 void uart_interrupt_handler(int signum) { int fd = open(UART_DEV, O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { perror("Open failed"); return; } char buffer[256]; int n = read(fd, buffer, sizeof(buffer)); if (n > 0) { buffer[n] = '\0'; printf("Received: %s\n", buffer); } close(fd); } int main() { int fd = open(UART_DEV, O_RDWR | O_NOCTTY | O_NDELAY); if (fd < 0) { perror("Open failed"); return -1; } // 配置串口参数 struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); options.c_cflag |= (CLOCAL | CREAD); options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; tcsetattr(fd, TCSANOW, &options); // 设置异步I/O fcntl(fd, F_SETOWN, getpid()); fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_ASYNC); // 注册信号处理函数 signal(SIGIO, uart_interrupt_handler); // 主循环 while (1) { sleep(1); } close(fd); return 0; } ``` ### 代码说明: 1. **打开串口设备**:使用`open`函数打开指定的串口设备文件,如`/dev/ttyUSB0`。 2. **配置串口参数**:使用`termios`结构体配置串口的波特率、数据位、停止位、校验位等参数。 3. **设置异步I/O**:使用`fcntl`函数设置异步I/O,当有数据到达时,内核会发送`SIGIO`信号。 4. **注册信号处理函数**:使用`signal`函数注册`SIGIO`信号的处理函数`uart_interrupt_handler`。 5. **主循环**:程序进入一个无限循环,等待中断信号的到来。 ### 编译和运行: 将上述代码保存为`uart_interrupt.c`,然后使用以下命令编译: ```sh gcc uart_interrupt.c -o uart_interrupt ``` 运行程序: ```sh ./uart_interrupt ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值