免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1164 | 回复: 0
打印 上一主题 下一主题

为什么串口通信中的信号处理在FC3 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-15 16:17 |只看该作者 |倒序浏览
请问:为什么我的串口通信中的信号处理用fedra3能够调通,而移植到linux7.2中就没有反应?这是为什么?花了一整个周末时间找问题,还没找出来,头大.知道的能跟我说下问题在哪里吗?谢谢
PS:FC3的内核是2.6.9,linux7.2的内核是2.4.7-10

附上我用的测试程序:
#include     
#include <stdio.h>   
#include <signal.h>      
#include <termio.h>      
#include <fcntl.h>   
#include <stdlib.h>     
      
#define BAUDRATE B38400      
#define MODEMDEVICE "/dev/ttyS0"      

#define _POSIX_SOURCE 1 /* POSIX compliant source */      
#define FALSE 0      
#define TRUE 1      

volatile int STOP=FALSE;
void signal_handler_IO (int status);   /* definition of signal handler */  
                                     // 定义信号处理程序
int wait_flag=TRUE;                   /* TRUE while no signal received */     
                                    // TRUE 代表没有受到信号,正在等待中   
main()   {        
int fd,c, res;   
struct termios oldtio,newtio;  
struct sigaction saio;         
/* definition of signal action */      
// 定义信号处理的结构
char buf[255];        

/* open the device to be non-blocking (read will return immediatly) */     
// 是用非阻塞模式打开设备 read 函数立刻返回,不会阻塞   
    fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);   
    if (fd <0) {perror(MODEMDEVICE); exit(-1); }        

/* install the signal handler before making the device asynchronous */   
    // 在进行设备异步传输前,安装信号处理程序   
    saio.sa_handler = signal_handler_IO;     
//saio.sa_mask = 0;  
sigemptyset(&sa_mask);
saio.sa_flags = 0;     
saio.sa_restorer = NULL;   
    sigaction(SIGIO,&saio,NULL);   

/* allow the process to receive SIGIO */
// 允许进程接收 SIGIO 信号      
fcntl(fd, F_SETOWN, getpid());   
    /* Make the file descriptor asynchronous (the manual page says only  
O_APPEND and O_NONBLOCK, will work with F_SETFL...) */   
// 设置串口的文件描述符为异步,man上说,只有 O_APPEND 和 O_NONBLOCK 才能使用F_SETFL
fcntl(fd, F_SETFL, FASYNC);        
tcgetattr(fd,&oldtio); /* save current port settings */   

    /* set new port settings for canonical input processing */   
// 设置新的串口为标准输入模式      
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;      
newtio.c_iflag = IGNPAR | ICRNL;     
newtio.c_oflag = 0;      
newtio.c_lflag = ~ICANON;  
newtio.c_cc[VMIN]=1;   
    newtio.c_cc[VTIME]=0;   
    tcflush(fd, TCIFLUSH);     
tcsetattr(fd,TCSANOW,&newtio);     

/* loop while waiting for input. normally we would do something      
useful here 循环等待输入,通常我们会在这里做些其它的事情 */
while (STOP==FALSE) {      
  printf(".\n");usleep(100000);        
  /* after receiving SIGIO, wait_flag = FALSE, input is availableand can be read */
  // 在收到 SIGIO 信号后,wait_flag = FALSE, 表示有输入进来,可以读取了
  if (wait_flag==FALSE) {         
   res = read(fd,buf,255);     
   buf[res]=0;        
   printf(":%s:%d\n", buf, res);  
   if (res==1) STOP=TRUE; /* stop loop if only a CR was input */   
   wait_flag = TRUE;      /* wait for new input 等待新的输入*/   
       }      
  }      
/* restore old port settings */     
tcsetattr(fd,TCSANOW,&oldtio);  
}            

/***************************************************************************   
* signal handler. sets wait_flag to FALSE, to indicate above loop that    *
* characters have been received.                                          *
***************************************************************************/   

// 信号处理函数,设置 wait_flag 为 FALSE, 以告知上面的循环函数串口收到字符了  
void signal_handler_IO (int status)   {   
printf("received SIGIO signal.\n");     
wait_flag = FALSE;  
}


[ 本帖最后由 57732690 于 2007-1-15 17:49 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP