I am sure, many developer might have faced this similar issue. If any one knows the answer please share with me.
I know that when a device (serial or USB) is plugged into a computer, the computer automatically assigns a COM port number and next time if we pluged in the same device and if the previously assigned COM port is already engaged it will assign another COM port.
Obviously I cannot change my program all the time. What is the generic way to find the COM port assigned to a partcular device programatically ?
NOTE : I am not using NI-VISA.
I want to find out the COM number programatically and update in my program. Please help me.
Thanks and regards,
I suppose the correct procedure is to register for a WM_DEVICE_CHANGE OS message: if wParam equals to DBT_DEVICEARRIVAL then you can decode lParam in a DEV_BROADCAST_HDR: you'll probably find it to be DBT_DEVTYP_PORT, in which case you can go further (but I can't help you anymore) and look for the port associated to the device.
You can look at the sample code published here on MSDN that can serve as a framework to elaborate on.
All this imples to have the interface to Win32 API (formerly known as Win SDK) installed and some familiarity with OS programming.
If I understood everybody correctly, that would work only if the port swap occurs while the program is running.
But I think what he refers is the case that, the users may plug the USB-serial device to one of the many port available on a computer and this changes their COM port number.
He wants the program, probably during startup, to automatically discover the COM port number a specific USB-serial device is assigned to.
Am I right?
If yes, you need to read all hardware devices attached and get the attributes of one that you will be able to discriminate with a unique identifier.
This also requires making good use of Win32 API.
So, good luck
You could start by examining this library provided by msaxon: it enumerates com ports found in the system; you can probably elaborate on the code to add informations you want to get the desired com port.