libftdi Archives

Subject: RE: How to recover from ftdi_write_data() error?

From: "rick.walker@xxxxxxxxxxx" <rick.walker@xxxxxxxxxxx>
To: "libftdi@xxxxxxxxxxxxxxxxxxxxxxx" <libftdi@xxxxxxxxxxxxxxxxxxxxxxx>
Cc: "rick.walker@xxxxxxxxxxx" <rick.walker@xxxxxxxxxxx>
Date: Wed, 9 Oct 2024 00:51:04 +0000

Sorry about that.  I just realized I’d already asked this question in 2022 and had this proposal rejected.

 

I would have thought that properly written code should only check for negative values and not for the particular value since ftdi_write_data() can only return -1 at present.  The current architecture makes it impossible to figure out what caused the error.

 

I’m resorting to closing the filehandle and restarting everything from scratch.  Flushing buffers doesn’t work.

--

Rick Walker

 

From: WALKER,RICK (Agilent USA) <rick.walker@xxxxxxxxxxx>
Sent: Tuesday, October 8, 2024 5:20 PM
To: libftdi@xxxxxxxxxxxxxxxxxxxxxxx
Cc: WALKER,RICK (Agilent USA) <rick.walker@xxxxxxxxxxx>
Subject: How to recover from ftdi_write_data() error?

 

 

 

I occasionally get my libftdi application locking up with an error on ftdi_write_data().  I would like to recover from this automatically, but I don't know what caused the error.  The code for ftdi_write_data() calls libusb_bulk_transfer().  If it gets an error it returns a generic (-1). 

 

Would it be reasonable to pass through the error from libusb_bulk_transfer() directly so I can diagnose the error?

 

Here’s a snippet of the existing code.  I’ve highlighted the problem area in yellow.

 

 

int ftdi_write_data(struct ftdi_context *ftdi, const unsigned char *buf, int size)

{

     int offset = 0;

     int actual_length;

 

     if (ftdi == NULL || ftdi->usb_dev == NULL)

         ftdi_error_return(-666, "USB device unavailable");

 

     while (offset < size)

     {

         int write_size = ftdi->writebuffer_chunksize;

 

         if (offset+write_size > size)

             write_size = size-offset;

 

         if (libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)

             ftdi_error_return(-1, "usb bulk write failed");

 

 

Could we change to something like this where we pass through the specific negative error code from libusb_bulk_transfer()?  It seems that it might make it easier to figure out how to recover from an ftdi_write_data error.

 

     int err;

     if ((err=libusb_bulk_transfer(ftdi->usb_dev, ftdi->in_ep, (unsigned char *)buf+offset, write_size, &actual_length, ftdi->usb_write_timeout) < 0)) {

          Ftdi_error_return(err, “usb bulk write failed”);

      }

 

Any suggestions for how to recover from ftdi_write_data() errors would be appreciated.  It seems like knowing the lower level error code might be helpful..

 

Currently, I just flush the buffers but it rarely fixes the problem.

 

Kind regards,

--

Rick Walker

 



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


Current Thread