diff --git a/README.md b/README.md index f56cd39..5ec9d60 100644 --- a/README.md +++ b/README.md @@ -65,13 +65,16 @@ Enable=1 | 想改什么 | 改哪里 | | --- | --- | -| A→B 通道映射(默认 A1→B4、A2→B5、A3→B1、A4→B2) | `src/bridge_config.h` 的 `map[5]`,或 ini 的 `[ChannelMap]` | +| A→B 通道映射(默认 A1→B7、A2→B5、A3→B1、A4→B6) | `src/bridge_config.h` 的 `map[5]`,或 ini 的 `[ChannelMap]` | | A 触发模式 → B `TrigSel/FireMode` | `src/bridge_config.h` 的 `trigSel` / `fireMode`,或 ini 的 `[Trigger]` | -| 通道打开/关闭时写 B 的 `Ch{n}Out` 值 | `src/bridge_config.h` 的 `onMode` / `offMode`,或 ini 的 `[Output]` | +| 通道打开/关闭时写 B 的 `Ch{n}Out` 值、B7 总开关 | `src/bridge_config.h` 的 `onMode` / `offMode` / `masterBChannel`,或 ini 的 `[Output]` | | 串口超时、写后延时、回读校验 | `src/bridge_config.h`,或 ini 的 `[Bridge]` | | B 协议命令格式 / 应答解析 | `src/tdg7_device.cpp` | | 导出函数签名 | `src/exports.cpp` + `src/TS660_DLL.def`(两处都要加) | +> B7 默认是总开关:打开映射到 B7 的 A 通道(当前 A1)时 B1~B7 全部置为正脉冲,关闭时全部置 GND。 +> 不需要该行为时,在 ini 的 `[Output]` 里设 `MasterChannel=0`。 + > 源码是 **UTF-8 with BOM**,工程已经设置 `/utf-8`。新增/另存源文件时保持 UTF-8, > 不要存成 GBK,否则中文日志字符串会编译报错。 diff --git a/TS660_Bridge.ini.sample b/TS660_Bridge.ini.sample index 6794244..ecfdbed 100644 --- a/TS660_Bridge.ini.sample +++ b/TS660_Bridge.ini.sample @@ -21,17 +21,18 @@ SilenceOnOpen=1 [ChannelMap] ; A 设备通道 -> B 设备通道(1..7,不能重复) -A1=B4 +A1=B7 A2=B5 A3=B1 -A4=B2 +A4=B6 [Trigger] ; A 设备触发模式 -> B 设备 (TrigSel, FireMode) ; TrigSel: 0=内触发 1=外触发上升沿 2=外触发下降沿 ; FireMode: 0=连续 1=单次 ; A 主程序只使用 0(内部触发)与 2(软件触发)。 -; 若 B 固件在 TrigSel=1 下 Fire 不产生脉冲,可把 Mode2TrigSel 改成 0 试试。 +; 软件触发 = 外触发上升沿 + 单次,每次 Fire 前 DLL 会重新置一次单次模式; +; 如果现场确实接外部触发源,反向极性可用 2(下降沿)。 Mode0TrigSel=0 Mode0FireMode=0 Mode1TrigSel=1 @@ -48,6 +49,9 @@ Mode4FireMode=1 ; 0=正脉冲 1=负脉冲 2=常高+5V 3=常低GND OnMode=0 OffMode=3 +; 总开关通道:默认 B7。映射到 B7 的 A 通道打开时,B1~B7 全部置为 OnMode; +; 关闭时 B1~B7 全部置为 OffMode。填 0 表示关闭该行为(各通道独立控制)。 +MasterChannel=7 [Temperature] ; B 设备没有温度传感器,ts660_read_temperature 固定返回该值。 diff --git a/TS660_DLL/src/bridge.cpp b/TS660_DLL/src/bridge.cpp index 589fb02..8b65d9d 100644 --- a/TS660_DLL/src/bridge.cpp +++ b/TS660_DLL/src/bridge.cpp @@ -71,10 +71,12 @@ namespace ts660b device_.SetFrequencyDivider(b, 1); // 上电先把所有通道拉到关闭电平,避免残留输出 + for (int b = 0; b < 8; ++b) + lastMode_[b] = -1; if (config_.silenceAllChannelsOnOpen) { for (int b = 1; b <= 7; ++b) - device_.SetOutputMode(b, config_.offMode); + SetBChannelMode(b, config_.offMode); } globalOutput_ = false; @@ -109,10 +111,79 @@ namespace ts660b return config_.map[aChannel]; } - bool Bridge::ApplyChannelOutput(int aChannel, int bChannel) + int Bridge::FindMasterAChannel() const { - const bool on = globalOutput_ && channels_[aChannel].enabled; - return device_.SetOutputMode(bChannel, on ? config_.onMode : config_.offMode); + if (config_.masterBChannel < 1 || config_.masterBChannel > 7) + return -1; + for (int a = 1; a <= 4; ++a) + { + if (config_.map[a] == config_.masterBChannel) + return a; + } + return -1; + } + + int Bridge::AChannelForB(int bChannel) const + { + for (int a = 1; a <= 4; ++a) + { + if (config_.map[a] == bChannel) + return a; + } + return -1; + } + + bool Bridge::SetBChannelMode(int bChannel, int mode) + { + if (bChannel < 1 || bChannel > 7) + return false; + if (lastMode_[bChannel] == mode) + return true; + if (!device_.SetOutputMode(bChannel, mode)) + return false; + lastMode_[bChannel] = mode; + return true; + } + + // 统一计算并下发 7 个通道的输出电平。 + // 规则:存在“总开关通道”(默认 B7)时,它开则 B1~B7 全开、它关则全关; + // 没有总开关通道时,退回按 A 各通道自己的开关分别控制。 + bool Bridge::ApplyAllOutputs() + { + const int master = FindMasterAChannel(); + bool ok = true; + + for (int b = 1; b <= 7; ++b) + { + bool on = false; + if (globalOutput_) + { + if (master > 0) + { + on = channels_[master].enabled; + } + else + { + const int a = AChannelForB(b); + on = (a > 0) && channels_[a].enabled; + } + } + + if (!SetBChannelMode(b, on ? config_.onMode : config_.offMode)) + ok = false; + } + return ok; + } + + // 主程序是“先设触发模式、后写频率/通道参数”,而 B 设备在后续参数写入后会丢掉 + // 单次位(原厂软件是最后才设触发模式)。所以每次发射前重新置一次单次模式。 + bool Bridge::ArmTrigger() + { + if (lastTriggerMode_ < 0 || lastTriggerMode_ > 4) + return true; + if (config_.fireMode[lastTriggerMode_] != 1) + return true; + return device_.SetTrigger(config_.trigSel[lastTriggerMode_], 1); } int Bridge::ReadTemperature(float* outCelsius) @@ -181,7 +252,7 @@ namespace ts660b return -1; channels_[aChannel].enabled = (enable != 0); - return ApplyChannelOutput(aChannel, bChannel) ? 0 : -1; + return ApplyAllOutputs() ? 0 : -1; } int Bridge::SetDelay(int aChannel, long long delayNs) @@ -210,14 +281,7 @@ namespace ts660b globalOutput_ = (enable != 0); explicitStop_ = !globalOutput_; - - int result = 0; - for (int a = 1; a <= 4; ++a) - { - if (!ApplyChannelOutput(a, config_.map[a])) - result = -1; - } - return result; + return ApplyAllOutputs() ? 0 : -1; } int Bridge::SoftwareTrigger() @@ -235,13 +299,10 @@ namespace ts660b { Logger::Instance().Write("WARN 发射前输出未使能,自动开启(软件触发模式)"); globalOutput_ = true; - for (int a = 1; a <= 4; ++a) + if (!ApplyAllOutputs()) { - if (!ApplyChannelOutput(a, config_.map[a])) - { - Logger::Instance().Write("ERR 自动开启输出失败:A%d -> B%d", a, config_.map[a]); - return -1; - } + Logger::Instance().Write("ERR 自动开启输出失败"); + return -1; } } else @@ -251,6 +312,12 @@ namespace ts660b } } + if (!ArmTrigger()) + { + Logger::Instance().Write("ERR 发射前重设单次触发失败"); + return -1; + } + Logger::Instance().Write("FIRE global=%d A-mode=%d", globalOutput_ ? 1 : 0, lastTriggerMode_); return device_.Fire(1) ? 0 : -1; } diff --git a/TS660_DLL/src/bridge.h b/TS660_DLL/src/bridge.h index f90055b..21c281e 100644 --- a/TS660_DLL/src/bridge.h +++ b/TS660_DLL/src/bridge.h @@ -38,7 +38,11 @@ namespace ts660b private: int ToHardwareChannel(int aChannel); - bool ApplyChannelOutput(int aChannel, int bChannel); + int FindMasterAChannel() const; + int AChannelForB(int bChannel) const; + bool ApplyAllOutputs(); + bool SetBChannelMode(int bChannel, int mode); + bool ArmTrigger(); struct ChannelState { @@ -52,5 +56,6 @@ namespace ts660b bool globalOutput_ = false; bool explicitStop_ = false; int lastTriggerMode_ = -1; + int lastMode_[8] = { -1, -1, -1, -1, -1, -1, -1, -1 }; }; } diff --git a/TS660_DLL/src/bridge_config.cpp b/TS660_DLL/src/bridge_config.cpp index 11bb75c..5deaf1e 100644 --- a/TS660_DLL/src/bridge_config.cpp +++ b/TS660_DLL/src/bridge_config.cpp @@ -18,7 +18,6 @@ namespace ts660b return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY) == 0; } - std::wstring ReadString(const std::wstring& ini, const wchar_t* section, const wchar_t* key, const wchar_t* defaultValue) { wchar_t buffer[512] = {}; @@ -115,7 +114,7 @@ namespace ts660b // ---- [ChannelMap] ---- const wchar_t* mapKeys[4] = { L"A1", L"A2", L"A3", L"A4" }; - const wchar_t* mapDefaults[4] = { L"B4", L"B5", L"B1", L"B2" }; + const wchar_t* mapDefaults[4] = { L"B7", L"B5", L"B1", L"B6" }; int parsed[5] = { 0, config.map[1], config.map[2], config.map[3], config.map[4] }; bool mapOk = true; for (int a = 1; a <= 4; ++a) @@ -169,6 +168,10 @@ namespace ts660b if (offMode >= 0 && offMode <= 3) config.offMode = offMode; + const int master = ReadInt(iniPath, L"Output", L"MasterChannel", config.masterBChannel); + if (master >= 0 && master <= 7) + config.masterBChannel = master; + // ---- [Temperature] ---- config.temperatureCelsius = ReadFloat(iniPath, L"Temperature", L"Celsius", config.temperatureCelsius); diff --git a/TS660_DLL/src/bridge_config.h b/TS660_DLL/src/bridge_config.h index e8db0d0..a9e56dc 100644 --- a/TS660_DLL/src/bridge_config.h +++ b/TS660_DLL/src/bridge_config.h @@ -15,18 +15,21 @@ namespace ts660b int writeSettleMs = 10; // 写命令后等待仪器处理的固定延时 // ---- A 通道 -> B 通道 ---- - // map[A] = B;A1->B4, A2->B5, A3->B1, A4->B2 - int map[5] = { 0, 4, 5, 1, 2 }; + // map[A] = B;A1->B7, A2->B5, A3->B1, A4->B6 + int map[5] = { 0, 7, 5, 1, 6 }; // ---- A 触发模式(0..4) -> B (TrigSel, FireMode) ---- // A: 0=内部触发, 2=软件触发(Aurora 只用这两档) // B: TrigSel 0=内触发 1=外触发上升沿 2=外触发下降沿;FireMode 0=连续 1=单次 + // 软件触发 = 外触发上升沿 + 单次,每次 ts660_software_trigger 前会重新置一次 + // (主程序先设触发、后写频率/通道,单次位会被后续命令冲掉,原厂软件是最后设触发)。 int trigSel[5] = { 0, 0, 1, 2, 2 }; int fireMode[5] = { 0, 0, 1, 0, 1 }; // ---- 通道输出电平(B 的 Ch{n}Out 取值)---- int onMode = 0; // 0=正脉冲 int offMode = 3; // 3=常低 GND(关闭输出) + int masterBChannel = 7; // B7 总开关:打开映射到它的 A 通道时 B1~B7 全开;0=禁用 bool silenceAllChannelsOnOpen = true; // 打开串口时先把 7 个通道全部置为 offMode bool verifyReadback = true; // 写参数后回读校验 diff --git a/TS660_DLL/src/tdg7_device.cpp b/TS660_DLL/src/tdg7_device.cpp index 9f71f97..c73a20a 100644 --- a/TS660_DLL/src/tdg7_device.cpp +++ b/TS660_DLL/src/tdg7_device.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -654,34 +655,84 @@ namespace ts660b bool Tdg7Device::SetFireMode(int value) { + std::lock_guard guard(ioMutex_); + if (!link_ || !link_->IsOpen()) + { + SetError("串口未打开"); + return false; + } + char command[64] = {}; std::snprintf(command, sizeof(command) - 1, "FireMode=%d", value); + link_->Purge(); + if (!Write(command)) + return false; + + // 原厂驱动的做法:设备会把当前 FireMode 以“纯数字”回一行,不需要再发 ?FireMode。 std::string response; - // 部分固件不回显 FireMode(或只回非数值应答),此时按写入成功处理 - if (!Exchange("FireMode", command, "?FireMode", response, true)) + const DWORD start = ::GetTickCount(); + char buffer[128] = {}; + + while ((DWORD)(::GetTickCount() - start) < 300) { - Logger::Instance().Write("WARN FireMode 无有效应答,按写入成功处理"); - return true; - } - if (!verify_) - return true; - if (response.empty()) - { - Logger::Instance().Write("WARN FireMode 无回读,无法确认单次模式已生效"); - return true; + const int n = link_->Read(buffer, (int)sizeof(buffer) - 1, 30); + if (n > 0) + { + response.append(buffer, (size_t)n); + bool hasValue = false; + for (char c : response) + { + if (!std::isspace((unsigned char)c)) + { + hasValue = true; + break; + } + } + if (hasValue) + break; + } + else if (n < 0) + { + break; + } } - double back = 0; + if (!response.empty()) + Logger::Instance().Write("RX %s", response.c_str()); + + int parsed = -1; + double keyed = 0; std::string unit; - if (!ExtractValue(response, "FireMode", back, unit)) - return true; // 有应答但不含可解析值,按可接受处理 - - if ((int)std::llround(back) != value) + if (ExtractValue(response, "FireMode", keyed, unit)) { - SetError("设置 FireMode 失败"); + parsed = (int)std::llround(keyed); + } + else + { + std::string trimmed; + for (char c : response) + { + if (!std::isspace((unsigned char)c)) + trimmed.push_back(c); + } + if (!trimmed.empty()) + { + char* end = nullptr; + const long parsedLong = std::strtol(trimmed.c_str(), &end, 10); + if (end != trimmed.c_str()) + parsed = (int)parsedLong; + } + } + + if (parsed >= 0 && parsed != value) + { + SetError("设置 FireMode 失败:回读 " + std::to_string(parsed)); return false; } + if (parsed < 0) + Logger::Instance().Write("WARN FireMode 无有效回读,按写入成功处理"); + return true; }