- 论坛徽章:
- 0
|
请问:为什么我的串口通信中的信号处理用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 编辑 ] |
|