LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

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 7
(7,130 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 7
(7,102 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 😉

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 7
(7,080 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 7
(7,071 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 7
(7,023 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 7
(7,005 Views)

For LabWindows/CVI you need go to:

-Tools and create .NET controller,

-select  "System" in the list and a location and a name to place the controller you will create under the Target Instrument box, click ok

-A window warning you about it will take a long time to compile the CVI wrapper will appear, click Yes

-A list of identifiers will appear, check only the System.IO.Ports and click OK

-An instrument will be generated that will appear under the Instrument option in the menu, if it doesn't appear there you can select the Load option and load the instrument recently created.

 

Now you can use the System_IO_Ports_SerialPort_GetPortNames() function to know the number and names of all the serial ports available in the computer.

//-------------------------------------------------------------------------------------

char **port_names;

int number_of_ports;

int INSTATUS=0;

 

Initialize_System();

INSTATUS = System_IO_Ports_SerialPort_GetPortNames(&port_names, &number_of_ports, NULL);

 

//-------------------------------------

//here the code to parse the data from the port_names variable using the number_of_ports count

//--------------------------------------

 

Use CDotNetFreeMemory() to free the memory used by the port_names variable strings.

CloseSystem();

//-------------------------------------------------------------------------------------------- 

 

 

0 Kudos
Message 7 of 7
(3,550 Views)