VC enumeration serial port application _c language

Source: Internet
Author: User

This article describes the VC serial port application, share for everyone to reference. The specific usage analysis is as follows:

Serial port as the most basic computer communication I/O interface, its use although less on the PC, but in the field of industrial instrumentation is still quite common, so that they first sorted out, I hope the heroes and peers are not hesitate to enlighten.

1. Query the Registration Form

The method of querying the registry is a more common method seen on the web, using programmatic methods to read information in the registry, which means that the user opens the registry directly by entering "regedit" (or Regedit32) in the Run box to view the
Hkey_local_machine/hardware/devicemap/serialcomm "Item to get the serial port information. The following is the source code:

Copy Code code as follows:
CString strseriallist[256]; 256 string groups are temporarily defined because the system is up to 256
HKEY hkey;
LPCTSTR data_set= "hardware//devicemap//serialcomm//";
Long ret0 = (:: RegOpenKeyEx (HKEY_LOCAL_MACHINE, Data_set, 0, Key_read, &hkey));
if (ret0!= error_success)
{
return-1;
}
int i = 0;
CHAR name[25];
Uchar szportname[25];
LONG Status;
DWORD dwindex = 0;
DWORD Dwname;
DWORD Dwsizeofportname;
DWORD Type;
Dwname = sizeof (Name);
Dwsizeofportname = sizeof (szportname);
Todo
{
Status = RegEnumValue (hkey, dwindex++, Name, &dwname, NULL, &type,
Szportname, &dwsizeofportname);
if (Status = = ERROR_SUCCESS) | | (Status = = Error_more_data))
{
Strseriallist[i] = CString (szportname); Serial string Save
i++;//Serial Port Count
}
while (Status = = ERROR_SUCCESS) | | (Status = = Error_more_data));
RegCloseKey (HKEY);

The above method can also realize the query of the same port, as long as the "hardware//devicemap//serialcomm//" with "Hardware//devicemap//parallel ports//" instead on the line.

Comparison: The method time is the most province, the author tried on their own computer, in 1ms (less than 1ms I do not know how to program the timing) can be completed, but also can solve the problem of USB serial port equipment, more practical, the only drawback is that
If the user in the installation of some software and hardware in the registry to register the virtual serial port, such as the enumeration obtained by this type of serial port can not be used as a serial port.

2. Using Enumport method

The method calls the Enumport () API function, which itself is used to enumerate the computer port, it enumerates not only the serial port, so it must be the analysis of the resulting serial port selection, the following is the source code:

Copy Code code as follows:
int M_nserialportnum (0);//serial Port Count
CString strseriallist[256]; Temporary definition of 256 string groups
Lpbyte pbite = NULL;
DWORD pcbneeded = 0; Bytes Received or required
DWORD pcreturned = 0; Number of ports received
M_nserialportnum = 0;
Gets the port information and can get the size of the port information pcbneeded
Enumports (NULL, 2, pbite, 0, &pcbneeded, &pcreturned);
Pbite = new byte[pcbneeded];
Enumeration port, can get the specific information of the port Pbite and the number of ports pcreturned
Enumports (NULL, 2, Pbite, pcbneeded, &pcbneeded, &pcreturned);
Port_info_2 *pport;
Pport = (port_info_2*) pbite;
for (i = 0; i < pcreturned; i++)
{
CString str = pport[i].pportname;
The specific determination of serial port information
if (str. Left (3) = = "COM")
{
Strseriallist[m_nserialportnum] = str. Left (strlen (str)-1);
CString temp = str. Right (strlen (str)-3);//The following two lines of comment get serial number with
M_nserialportno[m_nserialportnum] = atoi (temp. Left (strlen (temp)-1));
m_nserialportnum++;
}
}

The above method in addition to the serial port, can also enumerate all the interfaces and printers, and can find virtual serial port (some of these serial ports are not used, in the registry and Hardware Device Manager can not be achieved). But this method is a little time-consuming, the author on their own computer tried, probably need dozens of MS, the main problem is that some USB serial port can not be found, so this method is not reliable.

3, turn on the serial port method

The method is a good order to open the serial port, to see if the success of the serial port to determine whether there is no, the method source code is as follows:

Copy Code code as follows:
int M_nserialportnum (0);//serial number
CString strseriallist[256]; Temporary definition of 30 string groups
int ncom = 0;
int count = 0;
HANDLE hcom;
do {
ncom++;
Strcom.format ("com%d", ncom);
hcom = CreateFile (strcom, 0, 0, 0,
open_existing, File_attribute_normal, 0);
if (Invalid_handle_value = = hcom)
Break
Strseriallist[m_nserialportnum] = strcom;
m_nserialportnum++;
CloseHandle (hcom);
while (1);

The above methods enumerate all the currently available serial ports, if there is a serial port is currently occupied then the serial port will not be enumerated, of course, the above method can also be changed to call for loop to let it enumerate open 256 serial port method to avoid this situation, but this method is more time-consuming than the first two (general search for a serial port will be 15ms or so), but can enumerate all the currently open serial port, of course, can not enumerate some virtual serial port.

4, using the SetupAPI function set method

This is the simplest method I have ever seen, simply because someone has already encapsulated complex code, I just call like a fool to complete the work, the specific instructions please see
HTTP://WWW.CODEGURU.COM/CPP/W-P/SYSTEM/HARDWAREINFORMATION/ARTICLE.PHP/C5721/, here is the example code that I call this method:

Copy Code code as follows:
int M_nserialportnum (0);//serial Port Count
CString strseriallist[256]; Temporary definition of 256 string groups
Carray<sserinfo,sserinfo&> ASI;
Enumserialports (asi,true), or when the argument is TRUE, enumerates the current ports that can be opened.
Otherwise enumerate all serial ports
M_nserialportnum = ASI. GetSize ();
for (int i=0; I<asi. GetSize (); i++)
{
CString str = Asi[i].strfrien dlyname;
}

To add, use this method as long as in your program, adding "EnumSerial.cpp" and "EnumSerial.h" two files, and the Setupapi.lib included in your project file, the method time May and the third
method is similar, but this method obtains the serial port completely is the hardware Device Manager's serial port.

I hope this article on the VC program for everyone to help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.