Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working on detecting the new device arrival on the COM Port. I having problems in detecting new device arrival. please help.

I have implemented as below:

Register the device
DEV_BROADCAST_DEVICEINTERFACE notificationFilter = { 0 };
notificationFilter.dbcc_size = sizeof(notificationFilter);
notificationFilter.dbcc_devicetype =  DBT_DEVTYP_DEVICEINTERFACE;
notificationFilter.dbcc_classguid = GUID;

::RegisterDeviceNotification(
  hwnd, &notificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);

Wait for Message
C#
while( ::PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE ) )
{
   ::TranslateMessage( &msg );
   ::DispatchMessage( &msg );
   if( WM_DEVICECHANGE == msg.message )
   {
      if( DBT_DEVICEARRIVAL == msg.wParam )
      {
         // process
      }

C#
if( DBT_DEVNODES_CHANGED == msg.wParam  )
   {
   }
  }
}


I am not getting any DVT_DEVICEARRIVAL but only get DBT_DEVNODES_CHANGED even on inserting the device. Please help.
Posted
Updated 27-Nov-11 15:21pm
v3
Comments
suluvai 27-Nov-11 21:21pm    
I have a same problem too, did any one found solution for this?
[no name] 27-Nov-11 21:21pm    
EDIT: Added <pre> tag
suluvai 27-Nov-11 21:36pm    
Do u know the solution to this problem?
suluvai 27-Nov-11 22:51pm    
It is working fine I fixed it. The problem was with the GUID code not being right for the device i wanted to detect.

Normally doesn't work that way for serial devices.

You'll need a device driver that encapsulates the device, configured to use the com port.

Since it's the com port it will be the drivers responsibility to detect what happens on the com port.

This is usually not how "general" serial devices work. They can be accessed using the SerialPort component.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
GoonerGator 10-Jan-11 11:46am    
Hi Espen,

Thanks for the reply.

There is a driver already installed for driving the device. This code works at a layer above the driver. I got the UUID for the device from the driver inf itself. Please let me know if I am missing something.

Thanks,
Shishir
Espen Harlinn 10-Jan-11 11:52am    
Then it's up to the device driver to generate the required system notifications. You'll probably have to check with the manufacturer/software vendor...
GoonerGator 10-Jan-11 13:17pm    
Sorry, I am not too conversant with the internals. Is it not the OS which detects the device arrival and sends generic message to all the windows[mfc] and specific messages for the windows[mfc] which have registered for the specific device [using GUID] ?

Thanks,
Shishir
Espen Harlinn 10-Jan-11 14:57pm    
The OS communicates with hardware using device drivers. How well this works, and what "features" are enabled depends on the device driver. That's the really simple explaination, as it is actually a quite complicated issue.
[Update]

I found out what the problem is. Apparently, ONLY main windows[top level] would be able to receive the messages. Child windows and apparently some windows types simply don't have the ability to receive messages.

This should work :
[^]

If it is not working, then I would suggest you check if the window is getting created. Chances are it is failing once you register for the messages. [It calls NCCreate followed by NCDestroy]. I dunno how to make it work [I got a link which had same issue but hit a dead end there] but a way around is using the main window.

Hope that helps someone, somewhere and saves a day :-D
 
Share this answer
 
Comments
Espen Harlinn 11-Jan-11 15:19pm    
Since you had figured out things as far as you had I assumed you had read the first line of "Detecting Media Insertion or Removal" at http://msdn.microsoft.com/en-us/library/aa363215(VS.85).aspx: Windows sends all top-level windows a set of default WM_DEVICECHANGE messages when new devices or media ...
Anyway, nice that it worked out :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900