libftdi Archives

Subject: Re: use timeout on read

From: Ladislav Vaiz <spam@xxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Fri, 27 Mar 2009 13:58:54 +0100
Jonatan Magnusson napsal(a):
Hi

I can't figure out how to use ftdi_read_data with a timeout ... ?

Previously I've set a latency time of 1 and then repeat the call to
ftdi_read_data 750 times to achieve the timeout i need, but that is
*hardly* optimal, and also it doesn't give an exact timeout.

I see there is a usb_read_timeout variable in the ftdi-context, but it
is set to 5000, and still i get an immediate return from ftdi_read_data
if there is no data available. I've tried to increase it to 100000 with
the same result.

Is it possible?

Regards,

Jonatan Magnusson

Hi Jonatan,
I use my own wrapper around ftdi_read_data. It returns immediately when error occur or any amount of data is read. Timeout should be measured with good precision.
Lada

int my_ftdi_read(struct sconn *sconn, void *data, size_t len, int timeout_ms){
   int ret;
   struct timeval start, stop;
   int usec, sec;

   gettimeofday(&start, NULL);
   while(1){
       ret = ftdi_read_data(sconn->ftdi, data, len);
       if (ret) break;
       gettimeofday(&stop, NULL);
       usec = stop.tv_usec - start.tv_usec;
       sec = stop.tv_sec - start.tv_sec;
       if (usec<0) usec+=1000000,sec--;
       if (sec>timeout_ms/1000) break;
       if (usec/1000>timeout_ms) break;
       usleep(1000);
   }
   return ret;
}



--
libftdi - see http://www.intra2net.com/en/developer/libftdi for details.
To unsubscribe send a mail to libftdi+unsubscribe@xxxxxxxxxxxxxxxxxxxxxxx
Current Thread