FTD2XX_NET.FTDI.CyclePort C# (CSharp) Method

CyclePort() public method

Causes the device to be re-enumerated on the USB bus. This is equivalent to unplugging and replugging the device. Also calls FT_Close if FT_CyclePort is successful, so no need to call this separately in the application.
public CyclePort ( ) : FT_STATUS
return FT_STATUS
        public FT_STATUS CyclePort()
        {
            // Initialise ftStatus to something other than FT_OK
            FT_STATUS ftStatus = FT_STATUS.FT_OTHER_ERROR;

            // If the DLL hasn't been loaded, just return here
            if (hFTD2XXDLL == IntPtr.Zero)
                return ftStatus;

            // Check for our required function pointers being set up
            if ((pFT_CyclePort != IntPtr.Zero) & (pFT_Close != IntPtr.Zero))
            {
                tFT_CyclePort FT_CyclePort = (tFT_CyclePort)Marshal.GetDelegateForFunctionPointer(pFT_CyclePort, typeof(tFT_CyclePort));
                tFT_Close FT_Close = (tFT_Close)Marshal.GetDelegateForFunctionPointer(pFT_Close, typeof(tFT_Close));

                if (ftHandle != IntPtr.Zero)
                {
                    // Call FT_CyclePort
                    ftStatus = FT_CyclePort(ftHandle);
                    if (ftStatus == FT_STATUS.FT_OK)
                    {
                        // If successful, call FT_Close
                        ftStatus = FT_Close(ftHandle);
                        if (ftStatus == FT_STATUS.FT_OK)
                        {
                            ftHandle = IntPtr.Zero;
                        }
                    }
                }
            }
            else
            {
                if (pFT_CyclePort == IntPtr.Zero)
                {
                    LogB.Debug("FTD2XX: Failed to load function FT_CyclePort.");
                }
                if (pFT_Close == IntPtr.Zero)
                {
                    LogB.Debug("FTD2XX: Failed to load function FT_Close.");
                }
            }
            return ftStatus;
        }

Usage Example

コード例 #1
0
ファイル: Program.cs プロジェクト: brand7n/FTDIconnect
        static void Main(string[] args)
        {
            program();
            Console.WriteLine("This program contains a software component made available under the GNU Lesser General Public License.  The source code of this component is available here:");
            Console.WriteLine("https://github.com/brand7n/lpc21isp");

            // Create new instance of the FTDI device class
            FTDI myFtdiDevice = new FTDI();

            attention();
            Console.WriteLine("ATTEMPTING TO CONNECT...");
            subdue();


            while (true)
            {
                try
                {
                    open(myFtdiDevice);
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    attention();
                    Console.WriteLine("DISCONNECTED.  RESTART OR REATTACH DEVICE.");
                    subdue();
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    myFtdiDevice.CyclePort();
                }
            }
        }