Enumeration serial port

Source: Internet
Author: User

 

As the most basic computer communication I/O interface
But it is still quite common in the field of industrial instruments, because I need to use serial ports in my work, and I found that the enumeration serial ports are still not very clear, so I will first sort them out, hope you can stay with us
Give me some advice on what I don't understand and what's wrong.

 

 

1. query the Registry

The method for querying the registry is a common method seen on the Internet. This method is to use a programming method to read information in the registry, which is equivalent to entering "Regedit" (or regedit32) in the runtime box) open the registry and view"

HKEY_LOCAL_MACHINE/hardware/devicemap/serialcomm "to obtain the serial port information. The source code is as follows:

Cstring strseriallist [256]; // temporarily defines 256 string groups, because the system can have a maximum of 256 string groups.

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 );

Do

{

Status = regenumvalue (hkey, dwindex ++, name, & dwname, null, & type,

Szportname, & dwsizeofportname );

If (status = error_success) | (status = error_more_data ))

{

Strseriallist [I] = cstring (szportname); // Save the serial string

I ++; // serial port count

}

} While (status = error_success) | (status = error_more_data ));

Regclosekey (hkey );

The preceding method also supports parallel query, replace "Hardware/devicemap/serialcomm/" with "Hardware/devicemap/parallel ports.

Comparison: This method is the most time-saving. I tried it on my computer and completed it within 1 ms (I don't know how to program timing for less than 1 ms; at the same time, it can also solve the problem of USB to serial port equipment, which is more practical, the only drawback is,

If you have registered virtual serial ports in the Registry when installing some software and hardware, the serial ports obtained by enumeration using this method cannot be used as serial ports.

 

2. Use the enumport Method

This method calls the enumport () API function. This function is used to enumerate computer ports. It does not only enumerate serial ports. Therefore, you must analyze and select the obtained serial ports. The following is the source code:

Int m_nserialportnum (0); // serial port count

Cstring strseriallist [256]; // temporarily defines 256 string groups

Lpbyte pbite = NULL;

DWORD required needed = 0; // bytes encoded ed or required

DWORD pcreturned = 0; // number of ports already ed

M_nserialportnum = 0;

// Obtain the port information to obtain the port information pcbneeded

Enumports (null, 2, pbite, 0, & export needed, & pcreturned );

Pbite = new byte [effecneeded];

// Enumerate the port to obtain the port information 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;

// Determine the 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 comments are used to obtain the serial number.

// M_nserialportno [m_nserialportnum] = atoi (temp. Left (strlen (temp)-1 ));

M_nserialportnum ++;

}

}

In addition to the serial port, You can enumerate all the parallel port and printer interfaces, and find the virtual serial port (when some of these serial ports are not used, cannot be obtained in registry and Hardware Device Manager ). However, this method is slightly consumed.

Some, the author tried on his computer, probably need dozens of MS, the main problem is that some USB serial port of this method can not be found, so this method is not reliable.

 

3. How to enable the serial port in sequence

This method is to enable the serial port in sequence to check whether the serial port is successfully opened. The source code of this method is as follows:

Int m_nserialportnum (0); // Number of serial ports

Cstring strseriallist [256]; // temporarily defines 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 available serial ports. If a serial port is currently in use, the subsequent serial ports cannot be enumerated, of course, the above method can also be changed to calling the for loop to enable its enumeration to open 256 serial ports to avoid

In this case, this method is more time-consuming than the first two methods (generally it takes about 15 ms to search for a serial port), but you can enumerate all the currently open serial ports, of course, some virtual serial ports cannot be obtained through enumeration.

 

4. setupapi function set method

This method is the simplest method I have ever seen. It is simple because someone has encapsulated complicated code and I can complete the work by calling it like a fool, for more information, see

Http://www.codeguru.com/Cpp/W-P/system/hardwareinformation/article.php/c5721/, The following gives me the example code to call this method:

Int m_nserialportnum (0); // serial port count

Cstring strseriallist [256]; // temporarily defines 256 string groups

Carray <sserinfo, sserinfo &> ASI;

Enumserialports (ASI, true); // If the parameter is true, the serial ports that can be opened are enumerated,
// Otherwise, enumerate all serial ports

M_nserialportnum = asi. getsize ();

For (INT I = 0; I <asi. getsize (); I ++)

{

Cstring STR = ASI [I]. strfrien dlyname;

}

Add "enumserial. CPP and enumserial. h. LIB can be included in your project file. The time of this method may be the same as that of the third method.

The method is similar, but the serial port obtained by this method is completely the serial port in the Hardware Device Manager.

 

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.