From e5ed2dc688b7479816915644c374493236e0c615 Mon Sep 17 00:00:00 2001 From: ETWen Date: Thu, 4 Jun 2026 14:17:45 +0800 Subject: [PATCH] feat(scripting): wait settles until device is idle before matching After finding the keyword, wait requires the receive buffer to stay quiet for ~300ms (no new bytes) and consumes up to the LAST occurrence. This rejects matches on echoed command prompts (e.g. 'SVOS> help') mid-output, preventing the script from racing ahead. Applies to all wait-based loops without per-command markers. --- docs/ttl-script-reference.md | 2 +- src/ETTerms/Scripting/TTLInterpreter.cs | 45 ++++++++++++++++++------- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/docs/ttl-script-reference.md b/docs/ttl-script-reference.md index 6bcf48a..29fef7d 100644 --- a/docs/ttl-script-reference.md +++ b/docs/ttl-script-reference.md @@ -25,7 +25,7 @@ |------|------|------| | `send` | `send '文字'` | 送出文字(不加換行)到 active session。 | | `sendln` | `sendln '文字'` | 送出文字並附加 `\r\n`。 | -| `wait` | `wait '字串'` | **一直等到**接收緩衝出現指定字串才往下;預設無限等待(可按 Stop 取消)。命中後 `result=1` 並清掉緩衝中該字串(含)之前的內容。 | +| `wait` | `wait '字串'` | **一直等到**接收緩衝出現指定字串才往下;預設無限等待(可按 Stop 取消)。命中後會再確認裝置「安靜」約 300ms(無新資料)才接受,以排除夾在輸出中途的回顯(如 `SVOS> help`),並消費到該字串**最後一次**出現(含)之前的內容,`result=1`。 | | `pause` | `pause 秒數` | 暫停指定秒數(可被 Stop 中止)。 | | `timeout` | `timeout = 秒數` | 設定 `wait` 的逾時秒數。預設 `0`=無限等待;若設為 `N>0`,`wait` 超過 N 秒仍未命中會**中止整個腳本並報錯**(不會略過 `wait` 往下做)。 | | `flushrecv` | `flushrecv` | 清空接收緩衝區。 | diff --git a/src/ETTerms/Scripting/TTLInterpreter.cs b/src/ETTerms/Scripting/TTLInterpreter.cs index c6a1e8c..1e54bf3 100644 --- a/src/ETTerms/Scripting/TTLInterpreter.cs +++ b/src/ETTerms/Scripting/TTLInterpreter.cs @@ -21,6 +21,8 @@ public sealed class TTLInterpreter : IDisposable private readonly StringBuilder _recv = new(); private readonly CancellationTokenSource _cts = new(); + private const int SettleMs = 300; // wait 命中關鍵字後,需連續安靜這麼久(無新資料)才接受,避免比對到輸出中途的回顯 + private GroupSyncContext? _groupSync; private string _groupMemberLabel = ""; private readonly Dictionary _pdus = new(); @@ -351,18 +353,10 @@ public sealed class TTLInterpreter : IDisposable while (true) { ThrowIfCancelled(); - lock (_recv) - { - string buf = _recv.ToString(); - int idx = buf.IndexOf(text, StringComparison.Ordinal); - if (idx >= 0) - { - _result = 1; - _recv.Clear(); - _recv.Append(buf.Substring(idx + text.Length)); - return; - } - } + bool found; + lock (_recv) found = _recv.ToString().IndexOf(text, StringComparison.Ordinal) >= 0; + if (found && SettleAndConsume(text)) return; + // 只有在明確設定 timeout(>0) 且超時才中止腳本;否則一直等到關鍵字出現或被取消。 if (_timeout > 0 && elapsed >= _timeout) { @@ -375,6 +369,33 @@ public sealed class TTLInterpreter : IDisposable } } + /// + /// 找到關鍵字後,等待裝置「安靜」( 內無新資料進來)才接受, + /// 並消費到「最後一次」出現處。若 settle 期間又有資料進來,視為輸出尚未結束 + /// (命中的可能是夾在輸出中的指令回顯,如 SVOS> help),回傳 false 讓外層續等到真正的就緒提示。 + /// + private bool SettleAndConsume(string text) + { + int len; + lock (_recv) len = _recv.Length; + for (int waited = 0; waited < SettleMs; waited += 50) + { + ThrowIfCancelled(); + Thread.Sleep(50); + lock (_recv) if (_recv.Length != len) return false; // 又有新資料 → 還沒安靜 + } + lock (_recv) + { + string buf = _recv.ToString(); + int idx = buf.LastIndexOf(text, StringComparison.Ordinal); + if (idx < 0) return false; + _result = 1; + _recv.Clear(); + _recv.Append(buf.Substring(idx + text.Length)); + } + return true; + } + private void FlushReceive() { lock (_recv) _recv.Clear();