LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Comport physically present?

How can I find out which com-ports are physically present on a system ? I want to check the system for valid com-ports and then programm a list box for selection of a com port. Therefore i need to know which ports are "valid". I think that Windows must know the answer, how can i look it up? (Windows XP, CVI 7.0)
Message 1 of 3
(3,267 Views)
You could just open and close ports until you get an error. Something like this:

int nComPort;
int nOpenComError;
int bPrevBreak;
// ...
// ignore library errors trying to open com port
bPrevBreak = SetBreakOnLibraryErrors (0);
nComPort = 1;
do
{
nOpenComError = OpenCom (nComPort, "");
if (nOpenComError == 0)
// Close Com port and increment nComPort
CloseCom (nComPort++);
else
// return to the last successful port
// (0 if no ports opened)
nComPort--;
} while (nOpenComError == 0);
// restore previous break condition
SetBreakOnLibraryErrors (bPrevBreak);
// nComPort now contains the highest port number found
// (0 if none were found)

You could also be more selective in which OpenCom errors you allow. For example, if another application
has COM1 open, you'll get error -7 (Cannot open port), but not error -2 (invalid port number). (See the CVI help for the return value of ComOpen). If COM2 opens with no error, you could allow the user to select COM2 but not COM1.
0 Kudos
Message 2 of 3
(3,267 Views)

Hello

Another thing you could try is use the NI-VISA function called viFindRsrc(), which queires your VISA system to find the resoruces available for use. The resources include serial ports, gpib interfaces, etc. Check out the documentation for the function in the NI-VISA programmers reference manual

I hope this helps

Bilal Durrani
NI

Bilal Durrani
NI
Message 3 of 3
(3,267 Views)