資源簡介
SerialPort在VC上實現串口通訊相對來說比較簡便,有許多用C++做上位機開發的,不可避免的使用到串口。這里根據itas109維護的3.02版本編寫了一個實例。希望對大家有幫助。代碼在VC2017上編寫測試,能夠正常使用,請大家放心。
代碼片段和文件信息
/*
** FILENAME CSerialPort.cpp
**
** PURPOSE This?class?can?read?write?and?watch?one?serial?port.
** It?sends?messages?to?its?owner?when?something?happends?on?the?port
** The?class?creates?a?thread?for?reading?and?writing?so?the?main
** program?is?not?blocked.
**
** CREATION?DATE 15-09-1997
** LAST?MODIFICATION 12-11-1997
**
** AUTHOR Remon?Spekreijse
**
**??2007-12-25?mrlong????https://code.google.com/p/mycom/
**??2011-11-06?liquanhai?http://blog.csdn.net/liquanhai/article/details/6941574
**??2013-12-04?viruscamp?https://github.com/viruscamp
**??2014-01-10?itas109???http://blog.csdn.net/itas109
**??2014-12-18?liquanhai?http://blog.csdn.net/liquanhai/article/details/6941574
**??2016-05-06?itas109???http://blog.csdn.net/itas109
**??2016-06-22?itas109???http://blog.csdn.net/itas109
**??2016-06-29?itas109???http://blog.csdn.net/itas109
**??2016-08-02?itas109???http://blog.csdn.net/itas109
**??2016-08-10?itas109???http://blog.csdn.net/itas109
**??2017-02-14?itas109???http://blog.csdn.net/itas109
**??2017-03-12?itas109???http://blog.csdn.net/itas109
**??2017-12-16?itas109???http://blog.csdn.net/itas109
**??2017-02-14?itas109???http://blog.csdn.net/itas109
*/
#include?“SerialPort.h“
#include?“assert.h“
using?namespace?itas109;
//獲取注冊表指定數據到list
bool?getRegKeyValues(std::string?regKeyPath?std::list?&?portsList)
{
#define?MAX_KEY_LENGTH?255
#define?MAX_VALUE_NAME?16383
HKEY?hKey;
TCHAR achValue[MAX_VALUE_NAME]; //?buffer?for?subkey?name
DWORD cchValue?=?MAX_VALUE_NAME; //?size?of?name?string?
TCHAR achClass[MAX_PATH]?=?TEXT(““); //?buffer?for?class?name?
DWORD cchClassName?=?MAX_PATH; //?size?of?class?string?
DWORD cSubKeys?=?0; //?number?of?subkeys?
DWORD cbMaxSubKey; //?longest?subkey?size?
DWORD cchMaxClass; //?longest?class?string?
DWORD cKeyNum; //?number?of?values?for?key?
DWORD cchMaxValue; //?longest?value?name?
DWORD cbMaxValueData; //?longest?value?data?
DWORD cbSecurityDescriptor; //?size?of?security?descriptor?
FILETIME ftLastWriteTime; //?last?write?time?
int?iRet?=?-1;
bool?bRet?=?false;
std::string?m_keyValue;
TCHAR?m_regKeyPath[MAX_KEY_LENGTH];
TCHAR?strDSName[MAX_VALUE_NAME];
memset(strDSName?0?MAX_VALUE_NAME);
DWORD?nValueType?=?0;
DWORD?nBuffLen?=?10;
#ifdef?UNICODE
int?iLength;
const?char?*?_char?=?regKeyPath.c_str();
iLength?=?MultiByteToWideChar(CP_ACP?0?_char?strlen(_char)?+?1?NULL?0);
MultiByteToWideChar(CP_ACP?0?_char?strlen(_char)?+?1?m_regKeyPath?iLength);
#else
strcpy_s(m_regKeyPath?MAX_KEY_LENGTH?regKeyPath.c_str());
#endif
if?(ERROR_SUCCESS?==?RegOpenKeyEx(HKEY_LOCAL_MACHINE?m_regKeyPath?0?KEY_READ?&hKey))
{
//?Get?the?class?name?and?the?value?count.?
iRet?=?RegQueryInfoKey(
hKey????????????????????//?key?handle?
achClass????????????????//?buffer?for?class?name?
&cchClassName???????????//?size?of?class?string?
NULL???????????????????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-11-27?19:24??SerialPortTest\
?????目錄???????????0??2018-11-27?19:24??SerialPortTest\.vs\
?????目錄???????????0??2018-11-27?19:24??SerialPortTest\.vs\SerialPortTest\
?????目錄???????????0??2018-11-27?19:24??SerialPortTest\.vs\SerialPortTest\v15\
?????文件???????16896??2018-11-27?19:24??SerialPortTest\.vs\SerialPortTest\v15\.suo
?????文件????76922880??2018-11-27?19:24??SerialPortTest\.vs\SerialPortTest\v15\Browse.VC.db
?????目錄???????????0??2018-11-27?19:25??SerialPortTest\Debug\
?????目錄???????????0??2018-11-27?19:24??SerialPortTest\SerialPortTest\
?????目錄???????????0??2018-11-27?19:25??SerialPortTest\SerialPortTest\Debug\
?????文件???????39901??2018-03-05?10:25??SerialPortTest\SerialPortTest\SerialPort.cpp
?????文件???????11634??2018-03-05?10:25??SerialPortTest\SerialPortTest\SerialPort.h
?????文件??????108556??2018-11-27?17:38??SerialPortTest\SerialPortTest\SerialPortTest.aps
?????文件????????6242??2018-11-27?17:29??SerialPortTest\SerialPortTest\SerialPortTest.cpp
?????文件????????1112??2018-11-27?17:29??SerialPortTest\SerialPortTest\SerialPortTest.h
?????文件???????11394??2018-11-27?17:38??SerialPortTest\SerialPortTest\SerialPortTest.rc
?????文件???????10665??2018-11-27?19:24??SerialPortTest\SerialPortTest\SerialPortTest.vcxproj
?????文件????????2455??2018-11-27?18:12??SerialPortTest\SerialPortTest\SerialPortTest.vcxproj.filters
?????文件?????????237??2018-11-27?18:12??SerialPortTest\SerialPortTest\SerialPortTest.vcxproj.user
?????文件???????11874??2018-11-27?19:18??SerialPortTest\SerialPortTest\SerialPortTestDlg.cpp
?????文件????????2362??2018-11-27?19:17??SerialPortTest\SerialPortTest\SerialPortTestDlg.h
?????目錄???????????0??2018-11-27?19:24??SerialPortTest\SerialPortTest\res\
?????文件???????67777??2017-12-02?09:47??SerialPortTest\SerialPortTest\res\SerialPortTest.ico
?????文件?????????812??2018-11-27?17:29??SerialPortTest\SerialPortTest\res\SerialPortTest.rc2
?????文件?????????598??2017-12-02?09:47??SerialPortTest\SerialPortTest\res\Toolbar.bmp
?????文件????????2994??2017-12-02?09:47??SerialPortTest\SerialPortTest\res\Toolbar256.bmp
?????文件????????1466??2018-11-27?18:03??SerialPortTest\SerialPortTest\resource.h
?????文件?????????434??2018-11-27?17:29??SerialPortTest\SerialPortTest\stdafx.cpp
?????文件????????3508??2018-11-27?17:29??SerialPortTest\SerialPortTest\stdafx.h
?????文件?????????630??2018-11-27?17:29??SerialPortTest\SerialPortTest\targetver.h
?????文件????????1456??2018-11-27?17:29??SerialPortTest\SerialPortTest.sln
評論
共有 條評論