Select( ) always block on /dev/ttyUSB0
i m 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
/*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
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.
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. Instead it starts with a \n so it will be flushed before the relevant output appears.
The first argument to select()
is the highest file descriptor in the set plus one. 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. Check for -1 return and report the value of errno
. Also ensure that the file descriptor is in non-blocking mode.
精彩评论