FTDI Community

General Category => Discussion - Software => Topic started by: allenhuffman on April 30, 2020, 04:12:50 PM

Title: D2XX and FT4222 - how to detect USB cable unplugged? (I2C)
Post by: allenhuffman on April 30, 2020, 04:12:50 PM
I have a C# wrapper for the LibFT4222 and D2XX DLLs. The PC is acting as an I2C slave, and we have another project where the PC is the master.

I am trying to add recovery code to detect if the I2C connection is lost due to a USB cable unplug. Currently, we have close/reopen manually.

I have a loop that checks for data using FT4222_I2CSlave_GetRxStatus() and does a read if the expected bytes are available, then I do a FT4222_I2CSlave_Read().

I have noticed that the status returned from FT4222_I2CSlave_GetRxStatus continues to be FT4222_OK even if I unplug the USB cable.

I am trying to find a clue in these two manuals for how to detect a lost connection:

https://www.ftdichip.com/Support/Documents/ProgramGuides/D2XX_Programmer's_Guide(FT_000071).pdf
https://www.ftdichip.com/Support/Documents/AppNotes/AN_329_User_Guide_for_LibFT4222.pdf

My app also talks to a GPIB interface to a signal generator and have detection code for that working as expected. It returns an error when the device is turned off or unplugged.

Thanks for any pointers to what I need to be looking for in the manual.
Title: Re: D2XX and FT4222 - how to detect USB cable unplugged? (I2C)
Post by: FTDI Community on May 01, 2020, 05:18:50 PM
Hello,

Curiously neither FT4222_I2CSlave_GetRxStatus() or FT4222_I2CSlave_Read() are denoted as allowing for return codes which would indicate a USB disconnected.

In this case I would suggest calling  the D2XX function FT_GetStatus(), this will also return the number of bytes in the RX queue, but it should throw an FT_DEVICE_NOT_FOUND or FT_IO_ERROR on a USB disconnect.

Best Regards,
FTDI community