Implementation of Windows automatic recognition serial port

Source: Internet
Author: User

<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " >* Windows Automatic recognition of the implementation of the serial port, the following is based on the development of MFC, the following is said to be built on the serial port registry </span>

* To realize the automatic identification of the serial port under the Windows system, three Windows API functions are called:

Mainly used to open the serial port
1. LONG RegOpenKeyEx (   <span style= "White-space:pre" ></span>     HKEY hkey,<span style= " White-space:pre "></span>//primary key, that is, the serial port information stored folder   <span style=" White-space:pre "></span>     LPCTSTR lpsubkey,<span style= "White-space:pre" ></span>//sub-keys, the specific folder where the serial port   <span style= "White-space :p Re "></span>     DWORD uloptions,<span style=" White-space:pre "></span>//reserved value, no tube, must be set to 0                     regsam samdesired,<span style= "White-space:pre" ></span>//access rights                     Phkey phkresult<span style= "White-space:pre" ></span>//the returned serial handle, the following two functions use <span style= "White-space:pre "></span>    );
Mainly used to obtain the number of serial ports in the current serial port registry
2. LONG RegQueryInfoKey (<span style= "White-space:pre" ></span>hkey hkey,<span style= "White-space:pre" ></span>//regopenkeyex Five parameters returns the child key handle <span style= "White-space:pre" ></span>lptstr LPCLASS,&L T;span style= "White-space:pre" ></span>//null <span style= "White-space:pre" ></span>LPDWORD Lpcclass,<span style= "White-space:pre" ></span>//null <span style= "White-space:pre" ></span >lpdword lpreserved,<span style= "White-space:pre" ></span>//null <span style= "White-space:pre" ></span>lpdword lpcsubkeys,<span style= "White-space:pre" ></span>//the number of sub-keys <span style= " White-space:pre "></span>lpdword lpcmaxsubkeylen,<span style=" White-space:pre "></span>// Maximum child key length <span style= "White-space:pre" ></span>lpdword lpcmaxclasslen,<span style= "White-space:pre" ></span>//null <span style= "White-space:pre" ></span>lpdword lpcValues, <span style= "White-space:pre" ></span>//serial number <span style= "White-space:pre" ></span> Lpdword lpcmaxvaluenamelen,<span style= "White-space:pre" ></span>//the length of the maximum name <span style= "White-space: Pre "></span>lpdword lpcmaxvaluelen,<span style=" White-space:pre "></span>//maximum serial length <span Style= "White-space:pre" ></span>lpdword lpcbsecuritydescriptor,<span style= "White-space:pre" ></ Span>//null <span style= "White-space:pre" ></span>pfiletime lpftlastwritetime<span style= " White-space:pre "></span>//null <span style=" White-space:pre "></span>);
//is mainly used to obtain the serial port name, such as "COM3" and so on 
3. LONG RegEnumValue (<span style= "White-space:pre" ></span> HKEY hkey,<span style= "White-space:pre" ; </span>//string opening key handle <span style= "White-space:pre" ></span> DWORD dwindex,<span style= "White-space: Pre "></span>//index in the registry <span style=" White-space:pre "></span> LPTSTR lpvaluename,<span Style = "White-space:pre" ></span>//Value name <span style= "White-space:pre" ></span> Lpdword lpcvaluename, <span style= "White-space:pre" ></span>//the length of the value name <span style= "White-space:pre" ></span> LPDWORD Lpreserved,<span style= "White-space:pre" ></span>//null <span style= "White-space:pre" ></span > Lpdword lptype,<span style= "White-space:pre" ></span>//serial data type <span style= "White-space:pre" > </span> lpbyte lpdata,<span style= "White-space:pre" ></span>//serial name <span style= "White-space:pre" ></span> Lpdword Lpcbdata<span StYle= "White-space:pre" ></span>//the length of the serial name <span style= "White-space:pre" ></span>); 

Automatic identification of the implementation of the serial port:

struct Uartinfo{dword uartnum; WCHAR uartname[20];};/ /Get serial list bool Enumcoms (struct Uartinfo **uartcom, Lpdword uartcomnumber, Cnuprogdlg *pmaindlg) {//LPCTSTR is const char * *uar Tcomnumber = 0; HKEY hNewKey;   LONG Lresult=regopenkeyex (HKEY_LOCAL_MACHINE, L "Hardware\\devicemap\\serialcomm", 0, Key_all_access, &hNewKey); if (lResult! = ERROR_SUCCESS) {pmaindlg->addtoinfout (_t ("Open COM Registry failed!!!   "); return FALSE; }else{pmaindlg->addtoinfout (_t ("Open COM Registry succeeded!!!" "), 1, 1);} The DWORD is unsigned longdword valuesnumber;dword maxvaluenamelen;dword maxvaluelen; CString str;//retrieves the number of value entries under a specified subkey LResult = RegQueryInfoKey (hNewKey, NULL, NULL, NULL, NULL, NULL, NULL, &AMP;VALUESNU Mber, &maxvaluenamelen, &maxvaluelen, NULL, and NULL); if (lResult! = ERROR_SUCCESS) {RegCloseKey (hNewKey);//pmaindlg->addtoinfout ("Retrieving the number of serial ports connected to the PC failed!!!") "), 1, 1); return FALSE; } else {//str. Format (_t ("Number of serial ports connected to PC:%ld"), valuesnumber);//Pmaindlg->addtoinfout (str,1,1); *uartcom = (struct Uartinfo *) malloc Valuesnumber * sizeof (struct uartinfo)); } DWORD Index; DWORD uartindex = 0;  CHAR Valuename[max_value_name];  WCHAR valuename[100]; DWORD valuenamesize = max_value_name; DWORD valuenamesize; DWORD DataType; BYTE databuffer[100]; DWORD datalen = 100; LPTSTR is char *, lpbyte is char *//retrieves each value entry, gets the value name, data type, data for (index = 0; index < valuesnumber; index++) {memset (Valuenam E, 0, sizeof (ValueName)); memset (databuffer, 0, sizeof (databuffer)); valuenamesize = 100; Datalen = 100; LResult = RegEnumValue (Hnewkey,index,valuename,&valuenamesize,null, &datatype, DataBuffer, &DataLen); if (LResult = = ERROR_SUCCESS) {switch (DataType) {case Reg_none://No. value type (0) break; case Reg_sz://unicode nul t erminated string (1) break; Case reg_expand_sz://Unicode nul terminated string (2) break; Case reg_binary://Free Form BINARY (3) break; Case reg_dword://32-bit Number (4) break; Case REG_MULTI_SZ://Multiple Unicode strings (7) break; Default:break; } memcpy ((*uartcom) [UartinDex]. Uartname, DataBuffer, datalen); (*uartcom) [Uartindex]. Uartnum = Valuesnumber; uartindex++; } else if (LResult = = Error_no_more_items) {//pmaindlg->addtoinfout (_t ("Retrieve Serial port complete!!! "), 1, 1);} else {DWORD DW = GetLastError ();//Str. Format (_t ("Error retrieving serial port: 0x%08x"), DW);//Pmaindlg->addtoinfout (str,1,1); return FALSE; }} *uartcomnumber = Uartindex; return TRUE;}

Calls in the main function:

DWORD uartcomnumber = 0;struct uartinfo *puartcom; BOOL Bresult;bresult = enumcoms (&puartcom, &uartcomnumber, Pmaindlg);D word index;if (bresult) {pMainDlg-> Addtoinfout (_t ("Get serial list Success"), 1, 1);} Else{pmaindlg->addtoinfout (_t ("Get serial list Failed"), 1, 1);} for (index= 0;index < uartcomnumber; index++) {  pmaindlg->m_combobox.addstring (Puartcom[index]. Uartname);}




Implementation of Windows automatic recognition serial port

Related Article

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.