提交代码修改

This commit is contained in:
ldh
2026-11-13 08:38:35 +08:00
parent aa5542b99b
commit 53701e1ad0
7 changed files with 182 additions and 46 deletions
+86 -19
View File
@@ -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;
}
+6 -1
View File
@@ -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 };
};
}
+5 -2
View File
@@ -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);
+5 -2
View File
@@ -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; // 写参数后回读校验
+68 -17
View File
@@ -7,6 +7,7 @@
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
@@ -654,34 +655,84 @@ namespace ts660b
bool Tdg7Device::SetFireMode(int value)
{
std::lock_guard<std::mutex> 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;
}