Linux-使用中断从串行通信接收数据

时间:2015-10-27 02:46:07

标签: c embedded-linux interrupt-handling

我正尝试使用中断通过串行连接(GPIO UART引脚)在Raspberry Pi 2(raspian wheezy)和PC之间发送/接收数据。我的代码如下。我的程序在发送和接收数据时都执行中断功能。但是,我只希望我的程序在接收数据时跳转到中断功能。你能给我一些我可以寻找的建议吗?谢谢!

#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 fd;
struct termios termAttr;
struct sigaction saio;

int main(int argc, char *argv[])
{
 fd = open("/dev/ttyAMA0", 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 ); 
 tcgetattr(fd,&termAttr);
 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");

 uart_send();
 while(1){
 }

 close(fd);
 exit(0);             
}

void signal_handler_IO (int status)
{
 printf("received data from UART.\n");
}

0 个答案:

没有答案