libftdi Archives

Subject: Reading chunk of data of known size

From: Uwe Bonnes <bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Date: Mon, 21 Jun 2010 14:11:37 +0200
Hello,

when doing MPSSE transfers, e.g. for JTAG, there is a constant need to read
back a fixed amount of data caused be the last write.
ftdi_read_data() doesn't guarantee to read back all  requested data, but can
also return from a timeout, a short read and probably more causes.

In my code I have a wrapper readusb() around ftdi_read_data() to only return
when all data has been read, or some hard error happened:

unsigned int readusb(unsigned char * rbuf, unsigned long len)
{
    int length = (int) len, read = 0;
    int timeout=0, last_errno, last_read;
    last_read = ftdi_read_data(&ftdi_a, rbuf, length );
    if (last_read > 0)
        read += last_read;
    while ((read <length) && ( timeout <1000)) 
    {
        last_errno = 0;
        last_read = ftdi_read_data(&ftdi_a, rbuf+read, length -read);
        if (last_read > 0)
            read += last_read;
        else
            last_errno = errno;
        timeout++;
    }
    if (timeout >= 1000)
    {
        if (last_errno)
        {
            fprintf(stderr,"error %s\n", strerror(last_errno));
            return EXIT_FAILURE;
        }
    }
    if (read <0)
    {
        fprintf(stderr,"Error %d str: %s\n", -read, strerror(-read));
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

Shouldn't some code like this be included in libftdi?

Bye
-- 
Uwe Bonnes                bon@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------

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

Current Thread
  • Reading chunk of data of known size, Uwe Bonnes <=