Files

152 lines
4.5 KiB
C++

// TS660API.cpp: TS660 C 接口实现,桥接到 TS660DDG 类
#include "pch.h"
#include "TS660API.h"
#include "TS660DDG.h"
#include <string>
#include <cwctype>
// ASCII -> wchar_t
static std::wstring AnsiToWide(const char* s)
{
if (s == nullptr) return std::wstring();
int len = MultiByteToWideChar(CP_ACP, 0, s, -1, nullptr, 0);
if (len <= 0) return std::wstring();
std::wstring out(len, L'\0');
MultiByteToWideChar(CP_ACP, 0, s, -1, &out[0], len);
if (out.size() && out.back() == L'\0') out.pop_back();
return out;
}
// 规范化 COM 端口名: COM10+ 必须用 \\.\COMxx 格式, 否则 CreateFileW 失败
// 已带 \\.\ 或 \\?\ 前缀的直接返回, 不重复处理
static std::wstring NormalizeComPort(const std::wstring& port)
{
if (port.size() >= 4 && port.substr(0, 4) == L"\\\\.\\") return port;
if (port.size() >= 4 && port.substr(0, 4) == L"\\\\?\\") return port;
// 大小写不敏感地匹配 "COM" 前缀 + 纯数字后缀
if (port.size() > 3) {
if (std::towupper(port[0]) == L'C' &&
std::towupper(port[1]) == L'O' &&
std::towupper(port[2]) == L'M') {
const std::wstring numStr = port.substr(3);
bool allDigit = !numStr.empty();
for (wchar_t c : numStr) {
if (!std::iswdigit(c)) { allDigit = false; break; }
}
if (allDigit) {
int num = std::stoi(numStr);
if (num > 9) {
return L"\\\\.\\" + port;
}
}
}
}
return port;
}
static TS660DDG* ToPtr(void* h)
{
return static_cast<TS660DDG*>(h);
}
TS660_DLL_API void* ts660_init(const char* com_port, int baud_rate)
{
if (com_port == nullptr) return nullptr;
std::wstring port = NormalizeComPort(AnsiToWide(com_port));
if (port.empty()) return nullptr;
TS660DDG* ddg = new (std::nothrow) TS660DDG();
if (ddg == nullptr) return nullptr;
int baud = (baud_rate > 0) ? baud_rate : 115200;
if (!ddg->Open(port.c_str(), baud)) {
delete ddg;
return nullptr;
}
return ddg;
}
TS660_DLL_API void ts660_close(void* handle)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return;
p->Close();
delete p;
}
TS660_DLL_API int ts660_read_timing_config(void* handle, int channel,
long long* out_delay_ns,
long long* out_width_ns,
int* out_trigger_mode,
double* out_trig_freq_hz)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
TS660TimingConfig cfg = {};
if (!p->ReadTimingConfig(channel, &cfg)) return -2;
if (out_delay_ns) *out_delay_ns = cfg.delayNs;
if (out_width_ns) *out_width_ns = cfg.widthNs;
if (out_trigger_mode) *out_trigger_mode = cfg.triggerMode;
if (out_trig_freq_hz) *out_trig_freq_hz = cfg.trigFreqHz;
return 0;
}
TS660_DLL_API int ts660_read_temperature(void* handle, float* out_temp)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->ReadTemperature(out_temp) ? 0 : -2;
}
TS660_DLL_API int ts660_set_trigger(void* handle, int mode)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->SetTrigger(static_cast<TS660TriggerMode>(mode)) ? 0 : -2;
}
TS660_DLL_API int ts660_set_trigger_freq(void* handle, double freq_hz)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->SetInternalTriggerFreq(freq_hz) ? 0 : -2;
}
TS660_DLL_API int ts660_set_output_channel(void* handle, int channel, int enable)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->SetOutputChannel(channel, enable != 0) ? 0 : -2;
}
TS660_DLL_API int ts660_set_delay(void* handle, int channel, long long delay_ns)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->SetDelay(channel, static_cast<int64_t>(delay_ns)) ? 0 : -2;
}
TS660_DLL_API int ts660_set_pulse_width(void* handle, int channel, long long width_ns)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->SetPulseWidth(channel, static_cast<int64_t>(width_ns)) ? 0 : -2;
}
TS660_DLL_API int ts660_set_global_output(void* handle, int enable)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->SetGlobalOutput(enable != 0) ? 0 : -2;
}
TS660_DLL_API int ts660_software_trigger(void* handle)
{
TS660DDG* p = ToPtr(handle);
if (p == nullptr) return -1;
return p->SoftwareTrigger() ? 0 : -2;
}