LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 
Reply

Finding COM port number programatically.

Hi All,

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,

Rahul.

 

0 Kudos
Message 1 of 6
(2,788 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 6
(2,760 Views)

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 Smiley Wink

S. Eren BALCI
www.aselsan.com.tr
0 Kudos
Message 3 of 6
(2,738 Views)

Hi Mr. Eren BALCI,

 

Yes you are right. Smiley Happy

Is some one here has some example prog/doc ?

 

Thanks and Regards,

Rahul.

 

0 Kudos
Message 4 of 6
(2,729 Views)
You need to use the win sdk (win32 Ali) features and enumerate the registered com port. Once you have a list of ports you can look at their properties to find your usb device.

Search the forum for comport enumeration, this was discussed at some stage. If you're still stuck for samples send me a pm.
Jattie van der Linde
Engineering Manager, Software & Automation
TEL Magnetic Solutions Ltd
0 Kudos
Message 5 of 6
(2,681 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 6 of 6
(2,663 Views)