Le réseau pour les développeurs >
Forums - Accueil
>
Visual C# General
>
add/remove serial port names to a combo box as they become available
add/remove serial port names to a combo box as they become available
- In my application, I have a combo box that lists the available serial ports. One problem is that I want to be able to detect when a new serial port is added or an existing one is removed.
For example, if I have a serial to USB connector and it is COM3, when I unplug the USB cable from my machine, COM3 is no longer there. Is there an event that is signaled when a new port is becomes available or an existing one, connected or otherwise, port is lost?
Right now I use a timer with 1 sec interval to keep updating the combo box with SerialPort.GetPortNames() but I do not really like this idea and would like to know if there is a better way of doing it.
Thanks,
Réponses
- Hi
I currently use something very similar to this.
At present, i override the WndProc method on a form (actually, i override WndProc in a custom class inheriting NativeWindow) and catch the WM_DEVICECHANGE event looking for the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages. I then have to do a little bit more background code to determine whether the device attached/removed is a USB to Serial port device.
Unfortunately i don't have access to any of this code until tomorrow but this may be a starting point for you.
I can post some sample code tomorrow if it helps
Kev- Marqué comme réponsexenoverse mercredi 4 novembre 2009 23:46
Toutes les réponses
- Hi
I currently use something very similar to this.
At present, i override the WndProc method on a form (actually, i override WndProc in a custom class inheriting NativeWindow) and catch the WM_DEVICECHANGE event looking for the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages. I then have to do a little bit more background code to determine whether the device attached/removed is a USB to Serial port device.
Unfortunately i don't have access to any of this code until tomorrow but this may be a starting point for you.
I can post some sample code tomorrow if it helps
Kev- Marqué comme réponsexenoverse mercredi 4 novembre 2009 23:46
- Thanks, that is at least a starting point for me. I have just looked at MSDN and there is an example on overriding the WndProc method to handle other messages. I'll see if I can figure out how to do it, but still, sample code would be greatly appreciated if it isn't too much a trouble for you.
Thanks - No problem, i will post some code at some point tomorrow when i get a chance.
One MSDN page which may be helpful is http://msdn.microsoft.com/en-us/library/aa363480(VS.85).aspx which, if you scroll down the page a little, contains the DBT_xxx events you need to handle.
Kev - Thanks for your help.
After a little bit of reading on MSDN, I solved this problem. Your answer provided a lot of help.
Eventually, through some Marshaling and other message filtering, I was able to find out the device_name of the newly added or removed device, which is exactly what I needed.

