You are here

Hardware (USB) Change Detecting | Cypress Semiconductor

Hardware (USB) Change Detecting

Summary: 1 Reply, Latest post by NIKL on 07 Oct 2013 11:21 PM PDT
Verified Answers: 1
Last post
Log in to post new comments.
Ahsin-Ch's picture
5 posts



I'm using Cypress C++ Library (CyUSB.dll in C++) for my Project. I've written a C++ Dll for a hardware controller. I want to add a hardware detection feature in my Dll code. Currently my software detects a USB controller only when the controller is powered up and returns a True Value. And similarly my software is returning a False Value when USB controller is not powered up. But I want to add this feature in my software that it should detect hardware changes at run time. In simple words, i want to add plug and play feature in my software. 

I have also written a test program in C# language. And as far as this Plug and Play feature is concerned, I know how to do it in C#. But I've written a controller Dll in C++ so I want to add this feature using C++. Kindly explain me how can I do this thing using C++....

C# Example:                                                                                                                                                                                                              ----------------

public Form1(){


evHandler = new App_PnP_Callback(PnP_Event_Handler);

usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB, evHandler);

usbDevices.DeviceAttached += new EventHandler(Device_Attached);

usbDevices.DeviceRemoved += new EventHandler(Device_Removed);



public void Device_Attached(object sender, EventArgs e){

Thread.Sleep(250); // Allow 250ms for device to boot up in case it was switched off

DeviceAttached(sender, e);


public void Device_Removed(object sender, EventArgs e){

DeviceRemoved(sender, e);


public void setDevice(){

  if (usbDevices.Count > 0){

           Text = "USB devices>0";

           loopDevice = usbDevices[PCSVID, PCSDID] as CyUSBDevice;


   if (loopDevice != null){

           Text = "CD940 stepper motor test";



           Text = "CD940 stepper motor test - CARD NOT DETECTED";



    //The paramaters specify the endpoint number and direction.

   outEndpoint = loopDevice.EndPointOf(0x01) as CyBulkEndPoint;

   outEndpoint.TimeOut = 1000;

    inEndpoint = loopDevice.EndPointOf(0x81) as CyBulkEndPoint;

    inEndpoint.TimeOut = 1000;

    inMcsEndpoint = loopDevice.EndPointOf(0x82) as CyBulkEndPoint;

    inMcsEndpoint.TimeOut = 1000;



public void PnP_Event_Handler(IntPtr pnpEvent, IntPtr hRemovedDevice){

            if (pnpEvent.Equals(CyConst.DBT_DEVICEREMOVECOMPLETE)){


                loopDevice = null;



            if (pnpEvent.Equals(CyConst.DBT_DEVICEARRIVAL)){






My C++ Code Sample:                                                                                                                                                                                       ------------------------------

bool FindCard(CCyUSBDevice *USBDevice, USHORT VID, USHORT PID){

int numDevices = USBDevice->DeviceCount();

UCHAR deviceNum = 0;

CD940Found = false;


USBDevice->Open(deviceNum);   // Open automatically  calls Close(  ) if nece

if((USBDevice->VendorID == VID) && (USBDevice->ProductID == PID))

BoardFound = true;


} while ((deviceNum < numDevices ) && !BoardFound );




int InitialiseDLL(HWND AppHandle){

int ErrorCode;

BoardUSBRegInterface = NULL;

BoardObj = NULL;

CyUSBDevice = NULL;

CyUSBDevice = new CCyUSBDevice(AppHandle);

if (FindCard(CyUSBDevice, BoardVID, BoardDID) && CyUSBDevice->IsOpen()) {

BoardUSBRegInterface = new CyUSBRegisters(CyUSBDevice); // Create USB Register handling object

BoardObj = new CD940FrontEndBoard(CD940USBRegInterface);


       if((BoardObj != NULL) && Found() && (CyUSBDevice != NULL)) {

ErrorCode = Board_SUCCESS;


else {

ErrorCode = Board_NOT_DETECTED;


return ErrorCode;



Kindly explain me how can I add USB Plug and play feature inside C++ dll code.






NIKL's picture
Cypress Employee
283 posts

 Please follow the thread in this link:

Please do not create multiple thread for a single query.

Log in to post new comments.