我在 /dev/ttyUSB0 上的 XBee rf 模块上使用 Select() sys cal。但是这个 syscal 只是不返回(仅在超时时返回),但是如果我在这个端口上的 WHILE 循环中使用 read(),我可以看到数据传入
/*code to open the port*/
system("stty -F /dev/ttyUSB0 5:0:8bd:0:3:1c:7f:15:1:64:0:0:11:13:1a:0:12:f:17:16:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0:0");
fd = open("/dev/ttyUSB0", O_RDWR );
printf("fd is %d",fd);
if(fd == -1)
return ERR_PORT;
选择仅在 TIMEOUT 时返回,而不是在端口准备好读取时返回
FD_ZERO (&set);
FD_SET (fd, &set);//fd is an opened file des. for /dev/ttyUSB0
struct timeval timeout;
timeout.tv_sec = 50;
timeout.tv_usec = 0;
if(select(FD_SETSIZE,&set, NULL,NULL,&timeout)==1)
Do_stuff();
else
return TIMEOUT;
但如果我使用以下我可以看到正在打印的数据
char ch;
while(1)
{
read(fd,&ch,1);
printf("\n0x%X",ch);
}
请注意:关于system()
函数中的命令,我在stty -F /dev/USB0 -g
打开 GTKterm 后发出它/dev/ttyUSB0
。(那是我能够从我的程序与我的调制解调器交谈的时候)所以猜测 GTKterm 配置了端口,我使用了完全相同配置。