/// <summary>
    /// //This class contain all the function used as C# DLL
    /// //In the application you can call these functions in the following way
    /// //TE_USB_FX2.TE_USB_FX2.FunctionName()
    /// </summary>
    /// <returns></returns>

    /// <summary>
    /// 3.1   TE_USB_FX2_ScanCards()
    /// 
    ///3.1.1   Declaration
    ///public static int TE_USB_FX2_ScanCards(ref USBDeviceList USBdevList)
    ///
    ///3.1.2   Function Call
    ///Your application program shall call this function like this:
    ///TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_ScanCards( ref USBdevList);
    ///
    ///3.1.3   Description
    ///This function takes (a null initialized or an already initialized) USB device list, (re-)creates a USB device list, 
    ///searches for Trenz Electronic USB FX2 devices (Cypress driver derivative and VID = 0xbd0, PID=0x0300) devices and counts them.
    ///This function returns the number of Trenz Electronic USB FX2 devices attached to the USB bus of the host computer.
    ///
    ///3.1.4   Parameters
    ///1. ref USBDeviceList USBdevList
    ///USBDeviceList is a type defined in CyUSB.dll.
    ///USBdevList is the list of devices served by the CyUSB.sys driver (or a derivative like TE_USB_FX2.sys). This parameter is 
    ///passed by reference (ref). See page 139-140 of CyUSB.NET.pdf (Cypress CyUSB .NET DLL Programmer's Reference).
    ///
    ///3.1.5   Return Value
    ///1. int : integer type.
    ///This function returns the number of USB devices attached to the host computer USB bus.
    /// </summary>
    /// <param name="USBdevList"></param>
    /// <returns = int></returns>

    public static int TE_USB_FX2_ScanCards(ref USBDeviceList USBdevList)
    {
      int CardCount = 0;
      UInt16 PID = 0x0000;
      UInt16 VID = 0x0000;
      //Creation of a list of USB device that use the CYUSB.SYS driver
      USBdevList = new USBDeviceList(CyConst.DEVICES_CYUSB);

      //USBdevList.Count : this parameter give the number of card that use CyUSB.sys and its derivative 
      //(like TE_USB_FX2_64.sys and TE_USB_FX2_32.sys used by Trenz Electronic)

      //If exist at least an USB device that use the CYUSB.SYS driver,
      //I search and count the number of these devices that are of Trenz Electronic
      if (USBdevList.Count != 0)
      {
        // Look for a device having VID = 0bd0, PID = 0300
        foreach (USBDevice dev in USBdevList)
        {
          PID = dev.ProductID;
          VID = dev.VendorID;
          if ((((PID == 0x0300) && (VID == 0x0bd0)) == true)) CardCount++;
        }
        USBdevList.Dispose();
        return CardCount;
      }
      else
      {
        USBdevList.Dispose();
        return 0;
      }
    }
Example #2
0
        //This function is called when the form Form1 is closed aka when Program OpenFutNet is closed.
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {

            //The resources of PnpQuery used to search PnP connection/disconnection of DEWESoft device(s) 
            //are disposed
            //ManagementEventWatcher watcherDEWESoft = new ManagementEventWatcher(new WqlEventQuery(QUERY));
            watcherDEWESoft.Stop();
            watcherDEWESoft.EventArrived -= new EventArrivedEventHandler(OnPnPWMIEvent);
            watcherDEWESoft.Dispose();

            //watcher_TE_USB_FX2Gen3_or_GenericCypress_NotInstalled.Stop();
            //watcher_TE_USB_FX2Gen3_or_GenericCypress_NotInstalled.EventArrived -= new EventArrivedEventHandler(OnPnPWMIEvent);
            //watcher_TE_USB_FX2Gen3_or_GenericCypress_NotInstalled.Dispose();

            //The resource used by Trenz Electronic/Cypress device(s) are disposed
            USBdevList = new USBDeviceList(CyConst.DEVICES_CYUSB);
            if (USBdevList != null)
            {
                USBdevList.DeviceRemoved -= USBdevList_DeviceRemoved;
                USBdevList.DeviceAttached -= USBdevList_DeviceAttached;
                USBdevList.Dispose();
            }

            //The BackGroundWorker BW1_FPGA_SPIFlash used for FPGA and SPI Flash is disposed
            //Maybe is necessary to add -new Events
            BW1_FPGA_SPIFlash.Dispose();

            //The BackGroundWorker  BW2_CypressUSB_I2CEEPROM used for FX2 microcontroller EEPROM is disposed
            //Maybe is necessary to add -new Events
            BW2_CypressUSB_I2CEEPROM.Dispose();         
        }
    /*
     * 3.3   TE_USB_FX2_Close()
     * 
     * 3.3.1   Declaration
     * public static bool TE_USB_FX2_Close(ref USBDeviceList USBdevList)
     * 
     * 3.3.2   Function Call
     * Your application program shall call this function like this:
     * TE_USB_FX2.TE_USB_FX2.TE_USB_FX2_Close(ref USBdevList);
     * 
     * 3.3.3   Description
     * This function takes an already initialized USB device list and disposes it.
     * Due to the fact that we are coding C# here, the device list can or cannot be erased; this is in the scope of the garbage 
     * collector and it cannot be forced by the user. Sometimes it is erased instantly, some other times it is never erased, 
     * until the user closes the application program that uses this data.
     * Use of TE_USB_FX2_Close() function is NOT recommended for new software projects. Users may use this function only just before 
     * exiting their applications. If users use this function anywhere else, they shall
     * manage System.ObjectDisposedException exceptions (try – catch) or
     * avoid using disposed resources.
     * Note: USBdevList is disposed, not set to null.
     * try
     * {
     *   Application Code
     * }
     * catch (System.ObjectDisposedException)
     * {
     *   Console.WriteLine("TE_USB_FX2_USBDevice disposed: you have used the wrong procedure!");
     * }
     * 
     * If you want to close the current USB device (card) without opening another one, you shall use TE_USB_FX2_Open() with a device 
     * number (CardNumber) that certainly does not exist (e.g. CardNumber = 200, because there can be a maximum of 127 USB devices 
     * connected to a single host controller). The reason of this behavior is due to the CyUSB.dll as explained by Cypress document 
     * CyUSB.NET.pdf, pages 132-133 and pages 139-140: “You should never invoke the Dispose method of a USBDevice directly. Rather, 
     * the appropriate technique is to call the Dispose method of the USBDeviceList object that contains the USBDevice objects”
     * This function differs from its homonym of the previous TE0300DLL.dll in that it does not close a Handle but disposes (erases) 
     * all USB devices in the list.
     * A more intuitive name for this function would have been TE_USB_FX2_CloseAll or TE_USB_FX2_Dispose.
     * 
     * 3.3.4   Parameters
     * 1. ref USBDeviceList USBdevList
     * USBDeviceList is a type defined in CyUSB.dll. USBdevList is the list of Trenz Electronic USB FX2 devices attached to the USB bus 
     * host computer. This parameter is passed by reference (ref). See page 139-140 of CyUSB.NET.pdf (Cypress CyUSB .NET DLL Programmer's
     * Reference).
     * 3.3.5   Return Value
     * 1. bool : logical type
     * This function returns true if it is able to dispose the list. If unable to do so, it returns false.
     */

    /// <summary>
    /// //////
    /// </summary>
    /// <param name="USBdevList"></param>
    /// <returns></returns>

    public static bool TE_USB_FX2_Close(ref USBDeviceList USBdevList)
    {
      if (USBdevList != null)
      {
        USBdevList.Dispose();
        return true;
      }
      else
        return false;
    }