如何在中断时从两个串口接收

时间:2015-02-03 04:56:52

标签: c serial-port arm interrupt signal-handling

下面的代码从friendlyarm board的串口接收数据。如果只使用一个端口,信号处理程序能够接收数据。当我配置另一个端口时,信号处理程序在两个串口接收数据时执行(SGIO信号是从两个端口生成)。什么是识别数据源的方法,以便我可以正确地从两个端口分离数据。

main()
{ Spo2fd=open("/dev/ttySAC1", O_RDWR | O_NOCTTY | O_NDELAY,0);
         if (Spo2fd == -1)
         {
            perror("open_port: Unable to open /dev/ttySAC3\n");
            exit(1);
         }


         sbio.sa_sigaction = &spo2signal_handler_IO;//sbio is a    structure of type sigaction
         sbio.sa_flags = SA_SIGINFO;//=0
         sbio.sa_restorer = NULL;

         sigaction(SIGIO,&sbio,NULL);


         fcntl(Spo2fd, F_SETFL, FNDELAY);
         fcntl(Spo2fd, F_SETOWN, getpid());
         fcntl(Spo2fd, F_SETFL,  O_ASYNC ); 

         cfsetispeed(&spo2term,B4800);
         cfsetospeed(&spo2term,B4800);
              spo2term.c_cflag &= ~PARENB;
              spo2term.c_cflag &= ~CSTOPB;
              spo2term.c_cflag &= ~CSIZE;
              spo2term.c_cflag |= CS8;
              spo2term.c_cflag |= (CLOCAL | CREAD);
              spo2term.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
              spo2term.c_iflag &= ~(IXON | IXOFF | IXANY);
              spo2term.c_oflag &= ~OPOST;
              tcsetattr(Spo2fd,TCSANOW,&spo2term);
              printf("spo2term configured....\n");

             tcflush(Spo2fd,TCIFLUSH); // Flush input buffer
             tcflush(Spo2fd,TCOFLUSH);  // Flush output buffer


 while(1)
 {

  // process the received data here.
 }



 }

信号处理程序

static void spo2signal_handler_IO (int sig, siginfo_t *siginfo, void *context)
{printf("\nspo2 data received\n");
unsigned int temp1;
unsigned int a=128,c;
//printf("\nSpo2");
res_spo2 = read(Spo2fd,&bufspo2,1);
//printf("\n%x",bufspo2);
temp1=bufspo2;
c=a&temp1;

if(c==128)
    spo2count=1;
if(spo2count==1)
    firstdataflag=true;
else if(spo2count==2)
    seconddataflag=true;
else if(spo2count==3)
    thirddataflag=true;
else if(spo2count==4)
    fourthdataflag=true;
else if(spo2count==5)
    fifthdataflag=true;
}

0 个答案:

没有答案