Files
TS660_DLL/TS660_Debug.cpp

132 lines
4.3 KiB
C++

// TS660_Debug.cpp: 串口通信调试程序 - 发送命令并显示原始响应字节
#include "pch.h"
#include "SerialPort.h"
#include "TS660DDG.h"
#include <cstdio>
#include <vector>
#include <string>
#include <windows.h>
static std::wstring A2W(const std::string& s) {
if (s.empty()) return std::wstring();
int n = MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, nullptr, 0);
std::wstring w(n ? n - 1 : 0, L'\0');
if (n) MultiByteToWideChar(CP_ACP, 0, s.c_str(), -1, w.data(), n);
return w;
}
static std::wstring NormalizeCom(const std::wstring& port) {
if (port.size() >= 4 && port.substr(0, 4) == L"\\\\.\\") return port;
if (port.size() >= 3) {
std::wstring prefix = port.substr(0, 3);
for (auto& c : prefix) c = static_cast<wchar_t>(towupper(c));
if (prefix == L"COM") {
std::wstring numStr = port.substr(3);
if (!numStr.empty()) {
bool allDigit = true;
for (auto c : numStr) if (!isdigit(c)) { allDigit = false; break; }
if (allDigit) {
int n = std::stoi(numStr);
if (n > 9) return L"\\\\.\\" + port;
}
}
}
}
return port;
}
static void DumpHex(const unsigned char* data, int len, const char* tag) {
std::printf("%s [%d bytes]: ", tag, len);
for (int i = 0; i < len; ++i) {
std::printf("%02X ", data[i]);
if ((i & 15) == 15) std::printf("\n ");
}
std::printf("\n");
}
int main(int argc, char** argv) {
std::string portName = (argc >= 2) ? argv[1] : "COM10";
int baudRate = (argc >= 3) ? std::atoi(argv[2]) : 9600;
std::printf("=== TS660 Serial Debug ===\n");
std::printf("Port=%s, Baud=%d\n\n", portName.c_str(), baudRate);
SerialPort serial;
std::wstring port = NormalizeCom(A2W(portName));
if (!serial.Open(port.c_str(), baudRate, 8, StopBits::One, Parity::None)) {
std::printf("[FAIL] Cannot open %s\n", portName.c_str());
Sleep(3000);
return -1;
}
std::printf("[OK] Serial opened: %S\n\n", port.c_str());
// 测试 1: 读取温度
{
std::vector<uint8_t> frame;
TS660DDG::BuildFrame(TS660_RD | 0x13, 0x0000, nullptr, 0, frame);
DumpHex(frame.data(), static_cast<int>(frame.size()), "SEND READ_TEMP");
PurgeComm(serial.GetHandle(), PURGE_RXCLEAR | PURGE_RXABORT);
int n = serial.Write(frame.data(), static_cast<int>(frame.size()), 1000);
std::printf("Write returned: %d\n\n", n);
// 读取响应 - 循环读取 2 秒
std::printf("Reading response (2s window)...\n");
unsigned char buf[512];
int totalRead = 0;
DWORD startTick = GetTickCount();
while (GetTickCount() - startTick < 2000) {
int r = serial.Read(buf + totalRead, sizeof(buf) - totalRead, 200);
if (r > 0) {
totalRead += r;
std::printf(" Read %d bytes (total=%d)\n", r, totalRead);
}
}
if (totalRead > 0) {
DumpHex(buf, totalRead, "RECV RAW");
} else {
std::printf("[FAIL] No response within 2s\n");
}
}
std::printf("\n");
Sleep(500);
// 测试 2: 软件触发
{
std::vector<uint8_t> frame;
TS660DDG::BuildFrame(TS660_WR | 0x14, 0x0000, nullptr, 0, frame);
DumpHex(frame.data(), static_cast<int>(frame.size()), "SEND SOFTWARE_TRIG");
PurgeComm(serial.GetHandle(), PURGE_RXCLEAR | PURGE_RXABORT);
int n = serial.Write(frame.data(), static_cast<int>(frame.size()), 1000);
std::printf("Write returned: %d\n\n", n);
std::printf("Reading response (2s window)...\n");
unsigned char buf[512];
int totalRead = 0;
DWORD startTick = GetTickCount();
while (GetTickCount() - startTick < 2000) {
int r = serial.Read(buf + totalRead, sizeof(buf) - totalRead, 200);
if (r > 0) {
totalRead += r;
std::printf(" Read %d bytes (total=%d)\n", r, totalRead);
}
}
if (totalRead > 0) {
DumpHex(buf, totalRead, "RECV RAW");
} else {
std::printf("[FAIL] No response within 2s\n");
}
}
serial.Close();
std::printf("\n=== Done ===\n");
Sleep(5000);
return 0;
}