LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Available RS232 Ports

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

0 Kudos
Message 11 of 14
(1,860 Views)

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;

  }

}

 

0 Kudos
Message 12 of 14
(1,203 Views)

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;

  }

}

 

0 Kudos
Message 13 of 14
(1,203 Views)

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.



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 14 of 14
(1,185 Views)