02-08-2006 09:10 AM
Hi Martin,
You write about "I actually wrote it for locating the virtual com port associated with a particular USB device", could you give me more explanation because I need to use drive USB device from LabWindows CVI and I'm quite novice in that skill.
Best regards,
Jean-Noel Pomies
10-25-2014 03:11 AM
why use VISA when windows give the solution via the API function?
So this Code work to any C/C++ windows Compilers:
When you open the periphical windows itch RS232 port as Named COMx (x is the number of port) and you have maximaly 16 ports (COM0 to COM15).
So the strategy is for the COM1 ports is to open a peripherical connexion (CreateFile API function see the msdn documentation on internet) with the COM port and find if success or not.
//Code Exemple
int findcomdevice(int device,DWORD*error);
int main()
{
for(int i=0;i<16;i++)
{
DWORD error;
if(findcomdevice(i,error)>0)
{
printf("COM%d\n",i);
}
}
return 1;
}
//if this function is incorporete in a simple header file (.h/hpp) use this code
//inline int findcomdevice(int device,DWORD*error) instead of int findcomdevice(int device,DWORD*error)
int findcomdevice(int device,DWORD*error)
{
try
{
int test=0;
HANDLE devicehandle=NULL;
sprintf(v,"COM%d",device);
devicehandle=CreateFile(v,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(devicehandle!=NULL)
{
*error=GetlastError(0);
CloseHandle(devicehandle);
if(*error==0){test++;}
}
return test;
}
catch(...)
{
return 0;
}
}
10-25-2014 03:12 AM
why use VISA when windows give the solution via the API function?
So this Code work to any C/C++ windows Compilers:
When you open the periphical windows itch RS232 port as Named COMx (x is the number of port) and you have maximaly 16 ports (COM0 to COM15).
So the strategy is for the COM1 ports is to open a peripherical connexion (CreateFile API function see the msdn documentation on internet) with the COM port and find if success or not.
//Code Exemple
int findcomdevice(int device,DWORD*error);
int main()
{
for(int i=0;i<16;i++)
{
DWORD error;
if(findcomdevice(i,error)>0)
{
printf("COM%d\n",i);
}
}
return 1;
}
//if this function is incorporete in a simple header file (.h/hpp) use this code
//inline int findcomdevice(int device,DWORD*error) instead of int findcomdevice(int device,DWORD*error)
int findcomdevice(int device,DWORD*error)
{
try
{
int test=0;
HANDLE devicehandle=NULL;
sprintf(v,"COM%d",device);
devicehandle=CreateFile(v,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(devicehandle!=NULL)
{
*error=GetlastError(0);
CloseHandle(devicehandle);
if(*error==0){test++;}
}
return test;
}
catch(...)
{
return 0;
}
}
10-26-2014 03:26 PM
This is the good old trial-and-error method: very simple but prone to anomalies.
Even when ported to CVI (e.g. substituting try { } clause with a pair of SetBreakOnLibraryErrors calls) it causes a set of false positives: on my laptop, as an example, it would indicate COM3 as a valid and available port, when it really is the internal implementation of the built-in modem.