VC2017串口通讯


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.vsSerialPortTest
     目录           0  2018-11-27 19:24  SerialPortTest.vsSerialPortTestv15
     文件       16896  2018-11-27 19:24  SerialPortTest.vsSerialPortTestv15.suo
     文件    76922880  2018-11-27 19:24  SerialPortTest.vsSerialPortTestv15Browse.VC.db
     目录           0  2018-11-27 19:25  SerialPortTestDebug
     目录           0  2018-11-27 19:24  SerialPortTestSerialPortTest
     目录           0  2018-11-27 19:25  SerialPortTestSerialPortTestDebug
     文件       39901  2018-03-05 10:25  SerialPortTestSerialPortTestSerialPort.cpp
     文件       11634  2018-03-05 10:25  SerialPortTestSerialPortTestSerialPort.h
     文件      108556  2018-11-27 17:38  SerialPortTestSerialPortTestSerialPortTest.aps
     文件        6242  2018-11-27 17:29  SerialPortTestSerialPortTestSerialPortTest.cpp
     文件        1112  2018-11-27 17:29  SerialPortTestSerialPortTestSerialPortTest.h
     文件       11394  2018-11-27 17:38  SerialPortTestSerialPortTestSerialPortTest.rc
     文件       10665  2018-11-27 19:24  SerialPortTestSerialPortTestSerialPortTest.vcxproj
     文件        2455  2018-11-27 18:12  SerialPortTestSerialPortTestSerialPortTest.vcxproj.filters
     文件         237  2018-11-27 18:12  SerialPortTestSerialPortTestSerialPortTest.vcxproj.user
     文件       11874  2018-11-27 19:18  SerialPortTestSerialPortTestSerialPortTestDlg.cpp
     文件        2362  2018-11-27 19:17  SerialPortTestSerialPortTestSerialPortTestDlg.h
     目录           0  2018-11-27 19:24  SerialPortTestSerialPortTest
es
     文件       67777  2017-12-02 09:47  SerialPortTestSerialPortTest
esSerialPortTest.ico
     文件         812  2018-11-27 17:29  SerialPortTestSerialPortTest
esSerialPortTest.rc2
     文件         598  2017-12-02 09:47  SerialPortTestSerialPortTest
esToolbar.bmp
     文件        2994  2017-12-02 09:47  SerialPortTestSerialPortTest
esToolbar256.bmp
     文件        1466  2018-11-27 18:03  SerialPortTestSerialPortTest
esource.h
     文件         434  2018-11-27 17:29  SerialPortTestSerialPortTeststdafx.cpp
     文件        3508  2018-11-27 17:29  SerialPortTestSerialPortTeststdafx.h
     文件         630  2018-11-27 17:29  SerialPortTestSerialPortTest argetver.h
     文件        1456  2018-11-27 17:29  SerialPortTestSerialPortTest.sln

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)