From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

What CVI functions do I need to use in order to check the available 'COM' ports on my computer?

Hi there,

 

I need to connect my PC to an OXI module. Before controlling and sending commands to the module, I need to determine the available 'COM' port and select the right one. After that I can connect my PC to the module. I managed to do this the easy way around my manually determining which serial COM port was free and try them out until I got the right one.

 

I want to do it more elegant now. I know that this can be done directly from the CVI, but I am not sure what functions to use. I imagine that in need a 'for' clause to check all the COM ports but I am not sure on how to determine which port is free and ready to use from all available.

 

Has anyone tried something similar?

I would appreciate a reply.

 

Many thanks.

 

Peter

0 Kudos
Message 1 of 9
(8,578 Views)
The easiest way you can do it is to iterate for a reasonably high number of ports (e.g. on first 10 COM ports?) and try to open each of them: if not present in the system or already opened by other applications it will return you an error so that you can exclude it from the list of available ports.


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?
Message 2 of 9
(8,568 Views)

let's get a bit technical...

 

on windows, thanks to plug'n'play, devices are 'discovered' at system startup. this means that they are enumerated (the way they are is not relevant) and the informationsretrieved about each device is stored for use by the system. every device is classified according to a 'device class', this means that every device of the same type are flaged as belonging to the same group. classes are defined as GUID, and declared somewhere all accross the windows header files. and i can tell you there is a 'communication port' class.

 

in fact, when someone writes a virtual COM port, he writes a driver for a device whose class is declared as a 'communication port'. installing the driver allows the system to list this virtual port as if it were a standard COM port. (you can see virtual COM ports with bluetooth radio devices for example).

 

now, there is a part of Windows SDK, called the 'setup API', a subset of which allows you to enumerate devices installed on the system. there used to be a very good example of enumerating windows devices in the windows SDK, but it is not available anymore. have a look at the SetupDiGetClassDevs() function, make your way through the SDK, try by yourself. i do have a function for doing this on my computer, but it is rather long: if you are interrested, tell me and i will post it here.

0 Kudos
Message 3 of 9
(8,551 Views)

The sample CVI 9.0 project in the zip attached to this post uses the Windows Setup API to locate either all available COM ports on a machine or those with friendly names containing a specific sub-string (typically used to find, say, all the "USB" COM ports).

 

See LocateCOMCVI.h for terse documentation of the function LocateCom() defined in LocateCOMCVI.c, and FindPortCVI.c for a simple example program that uses the function.

 

The code is mainly copied from a Microsoft example, with some conversion from C++ into C. Sorry to admit it is almost entirely uncommented (I wrote it in a hurry!).

--
Martin
Certified CVI Developer
Message 4 of 9
(8,537 Views)

Hi,

 

Yoy may enumerate the registry key:

 

HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM

 

to obtain the available serial communication ports.

 

To check the availability, you have to open them.

 

Jan

Message 5 of 9
(8,516 Views)

I also need to know all available serial port on my PC. So I tried opening all serial ports from 1 to 20. Physically I have only 1 serial port. Which is port number 1. But when I open port using OpenComConfig  I could also open serial port 8 which is not connected (Not physically present) and doesn't show up in device manager. 

I could open, and write data to this port. (Not sure where data is going though!) I use CVI built in library to open/write data to serial port.

 

When I open registry it shows up as Winachsf0. (See attached screenshot)

Any help is appreciated.

 

Thanks.

Thanks.
CVI 2010
LabVIEW 2011 SP1
Vision Builder AI 2011 SP1
0 Kudos
Message 6 of 9
(7,940 Views)

did you consider viFindRsrc? see here for example.

Message 7 of 9
(7,934 Views)

There maybe several possibilities to have unexpected com ports in a PC: first of all, some motherboards and some serial interfaces have two ports physically available, only one of which is cabled on the rear panel while the second is available as an internal connector; notebooks frequently have built-in modems which actually are com ports; there may be other cases which I am not aware of. In Device manager frequently you can see some additional resources by checking View >> Show hidden devices.

 

In any case, using viFindRsrc as wolfgang suggests is normally a good option, but you may find that your system actually has some additional unexpected com port. Normally, going through properties of the devices shown you can have some details more to identify which type of device is; as an ultimate resource you may want to disable the device so that is no more accessible from the system: this should exclude it also from the search functions.

 



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 8 of 9
(7,916 Views)

I believe winachfs0 is the name given to a port associated with a Conexant modem.

--
Martin
Certified CVI Developer
0 Kudos
Message 9 of 9
(7,910 Views)