0

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.

Yadnesh
  • 1,307
  • 3
  • 17
  • 36

2 Answers2

2

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.

wildplasser
  • 43,142
  • 8
  • 66
  • 109
1

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.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • subtle but important! but last time when i used it on /dev/ttyS0(the modem was on it),it just worked fine without having `fd+1` used. – Yadnesh Sep 27 '11 at 11:31
  • That was certainly just luck: if the file descriptor was less than whatever FD_SETSIZE is defined as it will work; if it's greater or equal, it won't. – trojanfoe Sep 27 '11 at 11:46
  • i checked fd every time its no larger than 20 anytime! – Yadnesh Sep 27 '11 at 12:19
  • not yet :( FD_SETSIZE is 1024 and fd is never greater than 20.so i dont get the reason of the failure – Yadnesh Sep 27 '11 at 12:30
  • how do i find out that?,i m notdoing it explicitly! when i had modem on ttyS0 i didnt do anything on termios regarding blocking mode it was defualt.but i read somewhere that USB ports are unaffected by termios structure. so i used `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");` – Yadnesh Sep 27 '11 at 12:54
  • about command in `system()` function,i got it by issuing `stty -F /dev/ttyUSB0 -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. – Yadnesh Sep 27 '11 at 12:58
  • Please see my edits and make the changes to your code. `select()` is used on non-blocking file descriptors (which must be explicitly set using `fcntl()`). – trojanfoe Sep 27 '11 at 13:12