5 Replies Latest reply on Jun 10, 2011 2:57 AM by benjamin.tse

    How to reference USB devices in CyApi and CyUsb.Net

    benjamin.tse

      Hi,

         

      I'm writing an application that has multiple processes. One process is written in C# and uses CyUsb.Net to discover the Cypress USB devices that are connected. I then have another process written in C++ using CyApi that accesses the USB device. This C++ process is started by the C# process and is told which USB device to use.

         

      My devices do not have unique serial numbers.

         

      What can I use to identify the same device in the C# and C++ processes?

         

      From C# / CyUsb.Net I can go through each USBDevice in USBDeviceList and obtain the Path, USBAddress and SerialNumber (which is not unique).

         

      From C++ / CyApi I can call CCyUSBDevice::Open and tell it to open a particular device number. Is this device number the same as the USBAddress that CyUsb.Net returns? Or does it match something else in the CyUsb.Net interface?

         

      Alternatively, in CyApi, does the CCyUSBDevice::USBAddress match the CyUsb.Net USBAddress?

         

      Or can I use the CyUsb.Net Path to get hold of the same device in CyApi?

         

      Thanks,

         

      Ben

        • 1. Re: How to reference USB devices in CyApi and CyUsb.Net
          anand.srinivasan.asokan

          Both CyAPI and CyUSB.dll open handle in a similar manner so the device handle numbering should be the same. You'll have to keep in mind the scenario of device removal while you're triggering the C++ app. The numbering might change based on the device removed so having a way of distinguishing from the firmware (like unique serial number) is a good idea.

             

          USBAddress should come in handy since it is the bus address (will be same in both CyAPI and CyUSB) and is not bound to change based on device removal. There might a issue if the device you're addressing is removed and a new device plugged in at very quick succession and the host sets the bus address to the new device.

             

          Regards,

             

          Anand

          • 2. Re: How to reference USB devices in CyApi and CyUsb.Net
            benjamin.tse

            Thanks Anand,

               

            So the C++ application will need to iterate over all devices to find the relevant device. Are there any issues with calling Open on a device that might be in use by another process?

               

            e.g. My C++ code would be something like:

               

            UCHAR FindCyDevice(UCHAR usbAddress, bool& deviceFound)

               

            {

               

                CCyUSBDevice* usbDevice = new CCyUSBDevice(NULL);

               

               

                // iterate over all devices to find the matching USB address

               

                deviceFound = false;

               

                UCHAR deviceCount = usbDevice->DeviceCount();

               

                for (UCHAR deviceNum = 0; deviceNum < deviceCount; ++deviceNum)

               

                {

               

                    if (usbDevice->Open(deviceNum) && usbDevice->USBAddress == usbAddress)

               

                    {

               

                        usbDevice->Close();

               

                        deviceFound = true;

               

             

               

                        return deviceNum;

               

                    }

               

                }

               

                return 0;

               

            }

               

             

               
                 So will Open() return false if the device is open in another process? This isn't clear to me from reading the documentation.
               
               
                 Thanks,
            Ben
               
            • 3. Re: How to reference USB devices in CyApi and CyUsb.Net
              anand.srinivasan.asokan

              Nope. It will open a handle.

                 

              In the same application you should be able to check whether a handle is already open using the function IsOpen().

                 

              Opening a handle enumerates the device again to get the descriptors and build the descriptor structure. So as long as you don't interrupt the other process working by sending a transfer or doing other operations (since the other process won't know the things triggered by this process) that could affect the working of the other process, it should be fine.

                 

              Regards,

                 

              Anand

              • 4. Re: How to reference USB devices in CyApi and CyUsb.Net
                anand.srinivasan.asokan

                If you want only one handle to be openable to the device at a point of time you might want to take a look at this http://www.cypress.com/?id=4&rID=40554 .

                   

                Regards,

                   

                Anand

                • 5. Re: How to reference USB devices in CyApi and CyUsb.Net
                  benjamin.tse

                  Hi Anand,

                     

                  So from your description as long as the processes don't modify a shared data structure or initiate any USB transfer then I should be fine.

                     

                  This means that querying the USB address or serial number (if it can be unique) from multiple processes should be fine. I just need to be more careful with closing handles in the C++ version of the code.

                     

                  I'll give this a go and let you know if I have any issues.

                     

                  Thanks,
                  Ben