[英]Select( ) always block on /dev/ttyUSB0
im using Select() sys cal on XBee rf module which is on /dev/ttyUSB0.but this syscal just doesnt return(returns only on timeout),but if i use read() in a WHILE loop on this port i can see the data comming 我在/ 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;
select returns only when TIMEOUT not when port is ready for reading select仅在超时时返回,而在端口准备读取时不返回
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;
but if i use following i can see the data being printed 但是如果我使用以下命令,我可以看到正在打印的数据
char ch;
while(1)
{
read(fd,&ch,1);
printf("\n0x%X",ch);
}
Please note: about command in system()
function,i got it by issuing stty -F /dev/USB0 -g
after having GTKterm opened on /dev/ttyUSB0
.(thats when i was able to talk to my modem from my program) so made a guess that GTKterm configured the port,and i used the exact same configuration. 请注意:关于
system()
函数中的命令,我在/dev/ttyUSB0
上打开GTKterm后通过发出stty -F /dev/USB0 -g
获得它(那是我能够从程序与调制解调器进行通讯的时候)因此,我猜测GTKterm配置了端口,而我使用了完全相同的配置。
If you are using select() in a loop (I suppose you do) take care to set fd_set() and tv_sec, tv_usec on every iteration of the loop , Also: your printf format does not end in an \\n so output will not be flushed. 如果在循环中使用select()(我想是的话),请注意在循环的每次迭代中设置fd_set()和tv_sec,tv_usec,而且:printf格式不会以\\ n结尾,因此输出不会被冲洗。 Instead it starts with a \\n so it will be flushed before the relevant output appears.
相反,它以\\ n开头,因此将在相关输出出现之前将其刷新。
The first argument to select()
is the highest file descriptor in the set plus one. select()
的第一个参数是集合中最高的文件描述符加一个。 Your statement should be: 您的声明应为:
if (select(fd + 1,&set, NULL,NULL,&timeout) == 1)
{
...
}
EDIT : Also you assume if select()
doesn't return 1, it's due to a timeout issue, which is only true if 0 is returned. 编辑 :还假设
select()
不返回1,这是由于超时问题所致,仅当返回0时才为true。 Check for -1 return and report the value of errno
. 检查-1返回值并报告
errno
的值。 Also ensure that the file descriptor is in non-blocking mode. 还要确保文件描述符处于非阻塞模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.