CSerialPort 类,头文件: SerialPort.h
#ifndef _SERIAL_H
#define _SERIAL_H#include <string>
#include <Windows.h>
#include <cstddef>
#include <cstdlib>
#include <cassert>// 定义串口类型
#ifdef _UNICODE
typedef CString porttype;
#else
typedef std::string porttype;
#endif // _UNICODEtypedef unsigned long ulong;
typedef unsigned char uchar;class CSerialPort
{
public:CSerialPort(const porttype & portNum, // 串口号DWORD baudRate = 9600, // 波特率BYTE byteSize = 8, // 数据位BYTE parityBit = NOPARITY, // 检验位BYTE stopBit = ONESTOPBIT // 停止位);~CSerialPort();public:bool openComm(); // 打开串口void closeComm(); // 关闭串口bool writeToComm(BYTE data[], DWORD dwLegnth); // 发送数据bool readFromComm(char buffer[], DWORD dwLength); // 读取数据private:HANDLE m_hComm; // 通信设备porttype m_portNum; // 串口号DWORD m_dwBaudRate; // 波特率BYTE m_byteSize; // 数据位BYTE m_parityBit; // 校验位BYTE m_stopBit; // 停止位bool m_bOpen; // 串口开关标志
private:enum BufferSize{MIN_BUFFER_SIZE = 256,BUFFER_SIZE = 512,MAX_BUFFER_SIZE = 1024};// 设置串口号void setPortNum(const porttype &portNum){this->m_portNum = portNum;}// 设置波特率void setBaudRate(const ulong baudRate){this->m_dwBaudRate = baudRate;}// 设置数据位void setByteSize(const uchar byteSize){this->m_byteSize = byteSize;}// 设置检验位void setParityBit(const uchar parityBit){this->m_parityBit = parityBit;}// 设置停止位void setStopBit(const uchar stopBit){this->m_stopBit = stopBit;}// 获取串口号porttype getPortNum() { return m_portNum; }// 获取波特率ulong getBaudRate() { return m_dwBaudRate; }// 获取数据位uchar getByteSize() { return m_byteSize; }// 获取检验位uchar getParityBit() { return m_parityBit; }// 获取停止位uchar getStopBit() { return m_stopBit; }
};#endif // _SERIAL_H
SerialPort 类实现文件 .cpp:
#include "stdafx.h"
#include "Serial.h"CSerialPort::CSerialPort(const porttype & portNum,DWORD baudRate /* = 9600 */,BYTE byteSize /* = 8 */,BYTE parityBit /* = NOPARITY */,BYTE stopBit /* = ONESTOPBIT */) : m_portNum(portNum),m_dwBaudRate(baudRate),m_byteSize(byteSize),m_parityBit(parityBit),m_stopBit(stopBit),m_bOpen(false)
{}CSerialPort::~CSerialPort()
{}// 打开串口成功,返回 truebool CSerialPort::openComm()
{
#ifdef _UNICODEm_hComm = CreateFile(m_portNum,GENERIC_READ | GENERIC_WRITE, //允许读和写0, //独占方式NULL, OPEN_EXISTING, //打开而不是创建0, //同步方式NULL);
#elsem_hComm = CreateFileA(m_portNum.c_str(),GENERIC_READ | GENERIC_WRITE, //允许读和写0, //独占方式NULL, OPEN_EXISTING, //打开而不是创建0, //同步方式NULL);
#endifif (m_hComm == INVALID_HANDLE_VALUE){
#ifdef _UNICODETCHAR szBuf[1024] = { 0 };wsprintf(szBuf, L"打开 %s 失败,代码: %d", m_portNum, GetLastError());MessageBox(NULL, szBuf, L"Warnning", MB_OK);
#elsechar szBuf[1024] = { 0 };sprintf_s(szBuf, "打开 %s 失败,代码: %d", m_portNum, GetLastError());MessageBox(NULL, szBuf, "Warnning", MB_OK);
#endif // _UNICODEreturn false;}else{DCB dcb;SetupComm(m_hComm, MAX_BUFFER_SIZE, MAX_BUFFER_SIZE); // 设置读写缓冲区大小GetCommState(m_hComm, &dcb);dcb.BaudRate = m_dwBaudRate;dcb.ByteSize = m_byteSize;dcb.Parity = m_parityBit;dcb.StopBits = m_stopBit;if (!SetCommState(m_hComm, &dcb)){
#ifdef _UNICODETCHAR szBuf[1024] = { 0 };wsprintf(szBuf, L"串口设置失败,错误代码: %d", GetLastError());MessageBox(NULL, szBuf, TEXT("ERROR"), MB_OK);
#elsechar szBuf[1024] = { 0 };wsprintf(szBuf, "串口设置失败,错误代码: %d", GetLastError());MessageBox(NULL, szBuf, "ERROR", MB_OK);
#endifreturn false;}}//在读写串口前,用 PurgeComm 函数清空缓冲区PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_TXABORT);m_bOpen = true;return true;}// 关闭串口void CSerialPort::closeComm()
{CloseHandle(m_hComm);
}// 向串口发送数据
bool CSerialPort::writeToComm(BYTE data[], DWORD dwLength)
{
#ifdef _DEBUGassert(m_bOpen == true || dwLength > 0);return false;
#endif // _DEBUGDWORD dwError = 0;if (ClearCommError(m_hComm, &dwError, NULL) && dwError > 0){PurgeComm(m_hComm, PURGE_TXABORT | PURGE_TXCLEAR);}DWORD dwTx = 0;BOOL ret = FALSE;ret = WriteFile(m_hComm, data, dwLength, &dwTx, NULL);if (ret == FALSE){
#ifdef _UNICODETCHAR szBuf[1024] = { 0 };wsprintf(szBuf, _T("读取数据失败,错误代码: %d"), GetLastError());MessageBox(NULL, szBuf, L"ERROR", MB_OK);
#elsechar szBuf[1024] = { 0 };sprintf_s(szBuf, "读取数据失败, 错误代码: %d", GetLastError());MessageBox(NULL, szBuf, "Error", MB_OK);
#endif // _UNICODEreturn false;}return true;}// 从串口中读取数据bool CSerialPort::readFromComm(char buffer[], DWORD dwLength)
{
#ifdef _DEBUGassert(m_bOpen == true || dwLength > 0);return false;
#endif // _DEBUGCOMSTAT comStat;DWORD dwError = 0;if (ClearCommError(m_hComm, &dwError, &comStat) && dwError > 0){PurgeComm(m_hComm, PURGE_RXABORT | PURGE_RXCLEAR);}DWORD dwRx = 0; // 读入的字节数BOOL ret = FALSE;BYTE* byReadData = new BYTE[dwLength];char szTmp[4] = { 0 };int sizeOfBytes = sizeof(szTmp);ret = ReadFile(m_hComm, byReadData, dwLength, &dwRx, NULL); // 读入数据if (ret == TRUE){for (int i = 0; i < dwRx; ++i){sprintf_s(szTmp, "%02x", byReadData[i]);strcat_s(buffer, sizeOfBytes*dwLength, szTmp);}// 释放内存delete byReadData;return true;}else{
#ifdef _UNICODETCHAR szBuf[1024] = { 0 };wsprintf(szBuf, _T("数据读取失败,错误代码: %d"), GetLastError());MessageBox(NULL, szBuf, L"Error", MB_OK);
#elsechar szBuf[1024] = { 0 };wsprintf(szBuf, "数据读取失败,错误代码: %d", GetLastError());MessageBox(NULL, szBuf, "Error", MB_OK);
#endif // _UNICODEreturn false;}return true;
}
#include <iostream>
#include <cassert>
#include "Serial.h"
using namespace std;int main()
{CSerialPort serialPort("COM3");BYTE sendData = 0x01;char recvBuf[1024] = { 0 };serialPort.openComm();serialPort.writeToComm(&sendData, 1);serialPort.readFromComm(recvBuf, 1);serialPort.closeComm();system("pause");return 0;
}