初始化版本控制:TS660 DLL 源码、构建脚本与 SDK(Release) 产物
This commit is contained in:
+241
@@ -0,0 +1,241 @@
|
||||
// SerialPort.cpp: 串口通信封装实现
|
||||
#include "pch.h"
|
||||
#include "SerialPort.h"
|
||||
|
||||
SerialPort::SerialPort()
|
||||
: m_hCom(INVALID_HANDLE_VALUE)
|
||||
{
|
||||
ZeroMemory(&m_readOl, sizeof(m_readOl));
|
||||
ZeroMemory(&m_writeOl, sizeof(m_writeOl));
|
||||
}
|
||||
|
||||
SerialPort::~SerialPort()
|
||||
{
|
||||
Close();
|
||||
DestroyOverlappedEvents();
|
||||
}
|
||||
|
||||
bool SerialPort::CreateOverlappedEvents()
|
||||
{
|
||||
m_readOl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
if (m_readOl.hEvent == NULL) {
|
||||
return false;
|
||||
}
|
||||
m_writeOl.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
if (m_writeOl.hEvent == NULL) {
|
||||
CloseHandle(m_readOl.hEvent);
|
||||
m_readOl.hEvent = NULL;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void SerialPort::DestroyOverlappedEvents()
|
||||
{
|
||||
if (m_readOl.hEvent != NULL) {
|
||||
CloseHandle(m_readOl.hEvent);
|
||||
m_readOl.hEvent = NULL;
|
||||
}
|
||||
if (m_writeOl.hEvent != NULL) {
|
||||
CloseHandle(m_writeOl.hEvent);
|
||||
m_writeOl.hEvent = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
bool SerialPort::Open(const wchar_t* comPort,
|
||||
int baudRate,
|
||||
int dataBits,
|
||||
StopBits stopBits,
|
||||
Parity parity)
|
||||
{
|
||||
if (IsOpen()) {
|
||||
Close();
|
||||
}
|
||||
|
||||
if (comPort == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!CreateOverlappedEvents()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// FILE_FLAG_OVERLAPPED 必须设置,用于异步 I/O
|
||||
m_hCom = CreateFileW(comPort,
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
0, // 独占
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED,
|
||||
NULL);
|
||||
if (m_hCom == INVALID_HANDLE_VALUE) {
|
||||
DestroyOverlappedEvents();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 配置 DCB
|
||||
DCB dcb = { 0 };
|
||||
dcb.DCBlength = sizeof(DCB);
|
||||
if (!GetCommState(m_hCom, &dcb)) {
|
||||
Close();
|
||||
return false;
|
||||
}
|
||||
|
||||
dcb.BaudRate = static_cast<DWORD>(baudRate);
|
||||
dcb.ByteSize = static_cast<BYTE>(dataBits);
|
||||
|
||||
switch (parity) {
|
||||
case Parity::None: dcb.Parity = NOPARITY; break;
|
||||
case Parity::Odd: dcb.Parity = ODDPARITY; break;
|
||||
case Parity::Even: dcb.Parity = EVENPARITY; break;
|
||||
case Parity::Mark: dcb.Parity = MARKPARITY; break;
|
||||
case Parity::Space: dcb.Parity = SPACEPARITY; break;
|
||||
default: dcb.Parity = NOPARITY; break;
|
||||
}
|
||||
|
||||
switch (stopBits) {
|
||||
case StopBits::One: dcb.StopBits = ONESTOPBIT; break;
|
||||
case StopBits::OnePointFive: dcb.StopBits = ONE5STOPBITS; break;
|
||||
case StopBits::Two: dcb.StopBits = TWOSTOPBITS; break;
|
||||
default: dcb.StopBits = ONESTOPBIT; break;
|
||||
}
|
||||
|
||||
// 关闭硬件流控(可按需修改)
|
||||
dcb.fBinary = TRUE;
|
||||
dcb.fParity = (parity != Parity::None) ? TRUE : FALSE;
|
||||
dcb.fOutxCtsFlow = FALSE;
|
||||
dcb.fOutxDsrFlow = FALSE;
|
||||
dcb.fDtrControl = DTR_CONTROL_ENABLE;
|
||||
dcb.fRtsControl = RTS_CONTROL_ENABLE;
|
||||
dcb.fOutX = FALSE;
|
||||
dcb.fInX = FALSE;
|
||||
|
||||
if (!SetCommState(m_hCom, &dcb)) {
|
||||
Close();
|
||||
return false;
|
||||
}
|
||||
|
||||
// 清空缓冲区
|
||||
PurgeComm(m_hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
|
||||
|
||||
// 默认超时: 无超时(由 Read/Write 内部 WaitForSingleObject 控制)
|
||||
COMMTIMEOUTS to = { 0 };
|
||||
to.ReadIntervalTimeout = MAXDWORD;
|
||||
to.ReadTotalTimeoutMultiplier = 0;
|
||||
to.ReadTotalTimeoutConstant = 0;
|
||||
to.WriteTotalTimeoutMultiplier = 0;
|
||||
to.WriteTotalTimeoutConstant = 0;
|
||||
SetCommTimeouts(m_hCom, &to);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SerialPort::Close()
|
||||
{
|
||||
if (m_hCom != INVALID_HANDLE_VALUE) {
|
||||
// 取消未完成的异步 I/O
|
||||
CancelIo(m_hCom);
|
||||
CloseHandle(m_hCom);
|
||||
m_hCom = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
DestroyOverlappedEvents();
|
||||
}
|
||||
|
||||
bool SerialPort::SetTimeouts(const COMMTIMEOUTS& timeouts)
|
||||
{
|
||||
if (!IsOpen()) return false;
|
||||
COMMTIMEOUTS to = timeouts;
|
||||
return SetCommTimeouts(m_hCom, &to) != 0;
|
||||
}
|
||||
|
||||
bool SerialPort::SetTimeout(int readTimeoutMs, int writeTimeoutMs)
|
||||
{
|
||||
if (!IsOpen()) return false;
|
||||
|
||||
COMMTIMEOUTS to = { 0 };
|
||||
// MAXDWORD 表示无驱动级总间隔限制, ReadFile 立即返回由 overlapped 控制
|
||||
to.ReadIntervalTimeout = MAXDWORD;
|
||||
to.ReadTotalTimeoutMultiplier = 0;
|
||||
to.ReadTotalTimeoutConstant = 0;
|
||||
to.WriteTotalTimeoutMultiplier = 0;
|
||||
to.WriteTotalTimeoutConstant = static_cast<DWORD>(writeTimeoutMs);
|
||||
// 注: readTimeoutMs 由 Read() 中 WaitForSingleObject 的 timeoutMs 控制, 不写入 COMMTIMEOUTS
|
||||
(void)readTimeoutMs;
|
||||
return SetCommTimeouts(m_hCom, &to) != 0;
|
||||
}
|
||||
|
||||
int SerialPort::Read(unsigned char* buf, int size, int timeoutMs)
|
||||
{
|
||||
if (!IsOpen() || buf == nullptr || size <= 0) return -1;
|
||||
|
||||
if (m_readOl.hEvent == NULL) {
|
||||
if (!CreateOverlappedEvents()) return -1;
|
||||
}
|
||||
|
||||
ResetEvent(m_readOl.hEvent);
|
||||
m_readOl.Offset = 0;
|
||||
m_readOl.OffsetHigh = 0;
|
||||
|
||||
DWORD bytesRead = 0;
|
||||
BOOL ok = ReadFile(m_hCom, buf, static_cast<DWORD>(size),
|
||||
&bytesRead, &m_readOl);
|
||||
if (!ok) {
|
||||
DWORD err = GetLastError();
|
||||
if (err != ERROR_IO_PENDING) {
|
||||
return -1;
|
||||
}
|
||||
// 异步等待
|
||||
DWORD wait = WaitForSingleObject(m_readOl.hEvent, timeoutMs < 0 ? INFINITE : static_cast<DWORD>(timeoutMs));
|
||||
if (wait == WAIT_TIMEOUT) {
|
||||
// 取消未完成的 I/O, 仍可获取已读字节
|
||||
CancelIo(m_hCom);
|
||||
GetOverlappedResult(m_hCom, &m_readOl, &bytesRead, FALSE);
|
||||
return static_cast<int>(bytesRead);
|
||||
} else if (wait != WAIT_OBJECT_0) {
|
||||
return -1;
|
||||
}
|
||||
if (!GetOverlappedResult(m_hCom, &m_readOl, &bytesRead, FALSE)) {
|
||||
DWORD e = GetLastError();
|
||||
if (e == ERROR_HANDLE_EOF || e == ERROR_IO_INCOMPLETE) {
|
||||
return static_cast<int>(bytesRead);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return static_cast<int>(bytesRead);
|
||||
}
|
||||
|
||||
int SerialPort::Write(const unsigned char* buf, int size, int timeoutMs)
|
||||
{
|
||||
if (!IsOpen() || buf == nullptr || size <= 0) return -1;
|
||||
|
||||
if (m_writeOl.hEvent == NULL) {
|
||||
if (!CreateOverlappedEvents()) return -1;
|
||||
}
|
||||
|
||||
ResetEvent(m_writeOl.hEvent);
|
||||
m_writeOl.Offset = 0;
|
||||
m_writeOl.OffsetHigh = 0;
|
||||
|
||||
DWORD bytesWritten = 0;
|
||||
BOOL ok = WriteFile(m_hCom, buf, static_cast<DWORD>(size),
|
||||
&bytesWritten, &m_writeOl);
|
||||
if (!ok) {
|
||||
DWORD err = GetLastError();
|
||||
if (err != ERROR_IO_PENDING) {
|
||||
return -1;
|
||||
}
|
||||
DWORD wait = WaitForSingleObject(m_writeOl.hEvent, timeoutMs < 0 ? INFINITE : static_cast<DWORD>(timeoutMs));
|
||||
if (wait == WAIT_TIMEOUT) {
|
||||
CancelIo(m_hCom);
|
||||
GetOverlappedResult(m_hCom, &m_writeOl, &bytesWritten, FALSE);
|
||||
return static_cast<int>(bytesWritten);
|
||||
} else if (wait != WAIT_OBJECT_0) {
|
||||
return -1;
|
||||
}
|
||||
if (!GetOverlappedResult(m_hCom, &m_writeOl, &bytesWritten, FALSE)) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return static_cast<int>(bytesWritten);
|
||||
}
|
||||
Reference in New Issue
Block a user