159 lines
4.5 KiB
C++
159 lines
4.5 KiB
C++
#include "win32_link.h"
|
|
|
|
#include <string>
|
|
|
|
namespace ts660b
|
|
{
|
|
namespace
|
|
{
|
|
std::wstring NormalizePortName(const wchar_t* portName)
|
|
{
|
|
std::wstring s = portName ? portName : L"";
|
|
|
|
// 去掉首尾空白和引号
|
|
size_t begin = 0;
|
|
while (begin < s.size() && (s[begin] == L' ' || s[begin] == L'\t' || s[begin] == L'"'))
|
|
++begin;
|
|
size_t end = s.size();
|
|
while (end > begin && (s[end - 1] == L' ' || s[end - 1] == L'\t' || s[end - 1] == L'"'))
|
|
--end;
|
|
s = s.substr(begin, end - begin);
|
|
|
|
if (s.empty())
|
|
return s;
|
|
|
|
// 已经是 \\.\COMx 形式
|
|
if (s.size() >= 4 && s[0] == L'\\' && s[1] == L'\\' && s[2] == L'.' && s[3] == L'\\')
|
|
return s;
|
|
|
|
return L"\\\\.\\" + s;
|
|
}
|
|
}
|
|
|
|
bool Win32ByteLink::Open(const wchar_t* portName, int baudRate)
|
|
{
|
|
Close();
|
|
|
|
const std::wstring path = NormalizePortName(portName);
|
|
if (path.empty())
|
|
return false;
|
|
|
|
handle_ = ::CreateFileW(path.c_str(),
|
|
GENERIC_READ | GENERIC_WRITE,
|
|
0,
|
|
nullptr,
|
|
OPEN_EXISTING,
|
|
0,
|
|
nullptr);
|
|
if (handle_ == INVALID_HANDLE_VALUE)
|
|
return false;
|
|
|
|
::SetupComm(handle_, 4096, 4096);
|
|
|
|
DCB dcb = {};
|
|
dcb.DCBlength = sizeof(DCB);
|
|
if (!::GetCommState(handle_, &dcb))
|
|
{
|
|
Close();
|
|
return false;
|
|
}
|
|
|
|
dcb.BaudRate = (baudRate > 0) ? (DWORD)baudRate : 9600;
|
|
dcb.ByteSize = 8;
|
|
dcb.Parity = NOPARITY;
|
|
dcb.StopBits = ONESTOPBIT;
|
|
dcb.fBinary = TRUE;
|
|
dcb.fParity = FALSE;
|
|
dcb.fOutxCtsFlow = FALSE;
|
|
dcb.fOutxDsrFlow = FALSE;
|
|
dcb.fDsrSensitivity = FALSE;
|
|
dcb.fDtrControl = DTR_CONTROL_DISABLE;
|
|
dcb.fRtsControl = RTS_CONTROL_DISABLE;
|
|
dcb.fOutX = FALSE;
|
|
dcb.fInX = FALSE;
|
|
dcb.fErrorChar = FALSE;
|
|
dcb.fNull = FALSE;
|
|
dcb.fAbortOnError = FALSE;
|
|
|
|
if (!::SetCommState(handle_, &dcb))
|
|
{
|
|
Close();
|
|
return false;
|
|
}
|
|
|
|
COMMTIMEOUTS timeouts = {};
|
|
timeouts.ReadIntervalTimeout = 20;
|
|
timeouts.ReadTotalTimeoutMultiplier = 0;
|
|
timeouts.ReadTotalTimeoutConstant = 50;
|
|
timeouts.WriteTotalTimeoutMultiplier = 0;
|
|
timeouts.WriteTotalTimeoutConstant = 500;
|
|
if (!::SetCommTimeouts(handle_, &timeouts))
|
|
{
|
|
Close();
|
|
return false;
|
|
}
|
|
|
|
Purge();
|
|
return true;
|
|
}
|
|
|
|
void Win32ByteLink::Close()
|
|
{
|
|
if (handle_ != INVALID_HANDLE_VALUE)
|
|
{
|
|
::CloseHandle(handle_);
|
|
handle_ = INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|
|
|
|
bool Win32ByteLink::Write(const char* data, int length)
|
|
{
|
|
if (handle_ == INVALID_HANDLE_VALUE || data == nullptr || length <= 0)
|
|
return false;
|
|
|
|
int offset = 0;
|
|
while (offset < length)
|
|
{
|
|
DWORD written = 0;
|
|
if (!::WriteFile(handle_, data + offset, (DWORD)(length - offset), &written, nullptr))
|
|
return false;
|
|
if (written == 0)
|
|
return false;
|
|
offset += (int)written;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int Win32ByteLink::Read(char* buffer, int capacity, int timeoutMs)
|
|
{
|
|
if (handle_ == INVALID_HANDLE_VALUE || buffer == nullptr || capacity <= 0)
|
|
return -1;
|
|
|
|
COMMTIMEOUTS timeouts = {};
|
|
timeouts.ReadIntervalTimeout = 20;
|
|
timeouts.ReadTotalTimeoutMultiplier = 0;
|
|
timeouts.ReadTotalTimeoutConstant = (timeoutMs > 0) ? (DWORD)timeoutMs : 50;
|
|
::SetCommTimeouts(handle_, &timeouts);
|
|
|
|
DWORD read = 0;
|
|
if (!::ReadFile(handle_, buffer, (DWORD)capacity, &read, nullptr))
|
|
{
|
|
const DWORD error = ::GetLastError();
|
|
if (error == ERROR_OPERATION_ABORTED)
|
|
return 0;
|
|
return -1;
|
|
}
|
|
return (int)read;
|
|
}
|
|
|
|
void Win32ByteLink::Purge()
|
|
{
|
|
if (handle_ == INVALID_HANDLE_VALUE)
|
|
return;
|
|
|
|
// 只清接收缓冲。清发送缓冲(PURGE_TXCLEAR/PURGE_TXABORT)会把刚写入、
|
|
// 尚未发送完的命令截断——9600bps 下 9 个字节要 ~9.4ms 才发完。
|
|
::PurgeComm(handle_, PURGE_RXCLEAR);
|
|
}
|
|
}
|