Cypress Perform

Home > Design Support > Cypress Developer CommunityTM > Cypress Forums > USB Controllers > Detecting attached/removed USB devices with CyUSB.NET in a Windows Service

Bookmark and Share
Cypress Developer CommunityTM
Forums | Videos | Blogs | Training | Rewards Program | Community Components



Detecting attached/removed USB devices with CyUSB.NET in a Windows Service
Moderator:
RSKV

Post Reply
Follow this topic



Detecting attached/removed USB devices with CyUSB.NET in a Windows Service

bltse posted on 04 Jan 2012 8:11 PM PST
Member
6 Forum Posts

Hi,

I'm using CyUSB.NET from a Windows Service and it needs to detect devices that are attached and removed.

I find that the service does not get the USBDeviceList.DeviceAttached or DeviceRemoved events. However, if I move the code to a process then it does get these events.

Is there a way for a Windows service to get these events from USBDeviceList?

Thanks,

Ben

 




Re: Detecting attached/removed USB devices with CyUSB.NET in a Windows Service

aasi posted on 06 Jan 2012 08:41 AM PST
Cypress Employee
1090 Forum Posts

1. Please post your code here so that I can get a better idea of how you're implementing it.

2. Why do want to get the notification specifically through service

Regards,

Anand



Re: Detecting attached/removed USB devices with CyUSB.NET in a Windows Service

bltse posted on 09 Jan 2012 10:26 PM PST
Member
6 Forum Posts

Hi,

For Q2 - The structure of our application needs to use a Windows service to detect the USB devices.

For Q1 - I've attached a simplified VS2008 solution with what I am trying to do from a Windows service.

The CyUsbApp project has a Windows application that succesfully detects devices being added/removed.

The ServerService project has the code that creates the service (you can install the service using the InstallUtil utility). This logs to the event log when it detects a device being added/removed.

Both projects use the same code to detect devices. This code is in UsbManager.cs:

        private void RegisterDeviceNotifications()

        {


            mUsbDeviceList = new USBDeviceList(CyConst.DEVICES_CYUSB);
            mUsbDeviceList.DeviceAttached += new EventHandler(mUsbDeviceList_DeviceAttached);
            mUsbDeviceList.DeviceRemoved += new EventHandler(mUsbDeviceList_DeviceRemoved);
        }
 

USBDeviceList is populated with entries when the service starts. But I can't get the service to receive events from USBDeviceList.DeviceAttached() or USBDeviceList.DeviceRemoved().

Cheers,
Ben



Re: Detecting attached/removed USB devices with CyUSB.NET in a Windows Service

bltse posted on 17 Jan 2012 06:44 PM PST
Member
6 Forum Posts

Well, I found a work around to getting a service to recognise attached/detached devices.

Basically, you need to register a handler in the service for device notifications (instead of getting these events from USBDeviceList). Then handle the DEVICEARRIVAL and DEVICEREMOVECOMPLETE events when a device is attached/detached.

I found that I had to create a new USBDeviceList object each time a device is attached or detached. The one that I create at start up doesn't ever recognise new devices.

I also found that subscribing to the USBDeviceList.DeviceAttached and DeviceRemoved events would cause the device detached events to be delayed for some reason.

Anyway, hopefully the above information helps anyone else who is stuck with the same issue.



Re: Detecting attached/removed USB devices with CyUSB.NET in a Windows Service

Gayathri posted on 17 Jan 2012 09:40 PM PST
Cypress Employee
428 Forum Posts

 Hi,

 

Glad to hear that the problem is resolved, and thank you for posting the solution here. It might help some one in need later.

 

Regards,

Gayathri






ALL CONTENT AND MATERIALS ON THIS SITE ARE PROVIDED "AS IS". CYPRESS SEMICONDUCTOR AND ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THESE MATERIALS FOR ANY PURPOSE AND DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THESE MATERIALS, INCLUDING BUT NOT LIMITED TO, ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT OF ANY THIRD PARTY INTELLECTUAL PROPERTY RIGHT. NO LICENSE, EITHER EXPRESS OR IMPLIED, BY ESTOPPEL OR OTHERWISE, IS GRANTED BY CYPRESS SEMICONDUCTOR. USE OF THE INFORMATION ON THIS SITE MAY REQUIRE A LICENSE FROM A THIRD PARTY, OR A LICENSE FROM CYPRESS SEMICONDUCTOR.

Content on this site may contain or be subject to specific guidelines or limitations on use. All postings and use of the content on this site are subject to the Terms and Conditions of the site; third parties using this content agree to abide by any limitations or guidelines and to comply with the Terms and Conditions of this site. Cypress Semiconductor and its suppliers reserve the right to make corrections, deletions, modifications, enhancements, improvements and other changes to the content and materials, its products, programs and services at any time or to move or discontinue any content, products, programs, or services without notice.

Spec No: None; Sunset Owner: GRAA; Secondary Owner: RAIK; Sunset Date: 01/01/20