From 1108427023c6cf9b82684106e5c780a41022dc55 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Wed, 25 Feb 2026 10:57:44 +0100 Subject: [PATCH] [#74] Weighing: Try to reconnect tcp scales on first error --- Elwig/Helpers/Weighing/GassnerScale.cs | 13 ++++++++++--- Elwig/Helpers/Weighing/Scale.cs | 10 ++++++++++ Elwig/Helpers/Weighing/SysTecITScale.cs | 17 +++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/Elwig/Helpers/Weighing/GassnerScale.cs b/Elwig/Helpers/Weighing/GassnerScale.cs index a6ff07d..32406bb 100644 --- a/Elwig/Helpers/Weighing/GassnerScale.cs +++ b/Elwig/Helpers/Weighing/GassnerScale.cs @@ -53,9 +53,16 @@ namespace Elwig.Helpers.Weighing { return line[1..^1]; } - protected async Task Weigh(bool incIdentNr) { - await SendCommand(incIdentNr ? '\x05' : '?'); - string record = await ReceiveResponse(); + protected async Task Weigh(bool incIdentNr, bool retry = true) { + string record; + try { + await SendCommand(incIdentNr ? '\x05' : '?'); + record = await ReceiveResponse(); + } catch (IOException) { + if (!retry || Tcp == null) throw; + ReconnectTcp(); + return await Weigh(incIdentNr, false); + } if (record.Length != 45) throw new FormatException("Invalid response from scale: Received record has invalid size"); var line = record[2..]; diff --git a/Elwig/Helpers/Weighing/Scale.cs b/Elwig/Helpers/Weighing/Scale.cs index f387cfa..e472613 100644 --- a/Elwig/Helpers/Weighing/Scale.cs +++ b/Elwig/Helpers/Weighing/Scale.cs @@ -10,6 +10,7 @@ namespace Elwig.Helpers.Weighing { protected enum Output { RTS, DTR, OUT1, OUT2 }; + protected readonly string Connection; protected SerialPort? Serial = null; protected SerialPort? ControlSerialEmpty = null, ControlSerialFilling = null; protected TcpClient? Tcp = null; @@ -37,6 +38,7 @@ namespace Elwig.Helpers.Weighing { } protected Scale(string cnx, string? empty, string? filling, int? limit, string? log, bool softFail = false, bool failSilent = false) { + Connection = cnx; if (cnx.StartsWith("serial:")) { try { Serial = Utils.OpenSerialConnection(cnx); @@ -95,6 +97,14 @@ namespace Elwig.Helpers.Weighing { GC.SuppressFinalize(this); } + protected void ReconnectTcp() { + if (Connection.StartsWith("tcp:")) { + Tcp = Utils.OpenTcpConnection(Connection); + Stream = Tcp.GetStream(); + Reader = new(Stream, Encoding.ASCII, false, 512); + } + } + protected static Output? ConvertOutput(string? value) { return value switch { null => null, diff --git a/Elwig/Helpers/Weighing/SysTecITScale.cs b/Elwig/Helpers/Weighing/SysTecITScale.cs index b3ffd4a..7dc7bb3 100644 --- a/Elwig/Helpers/Weighing/SysTecITScale.cs +++ b/Elwig/Helpers/Weighing/SysTecITScale.cs @@ -33,7 +33,9 @@ namespace Elwig.Helpers.Weighing { protected async Task ReceiveResponse() { var line = await Reader.ReadUntilAsync("\r\n"); if (LogPath != null) await File.AppendAllTextAsync(LogPath, line); - if (line == null || line.Length < 4 || !line.StartsWith('<') || !line.EndsWith(">\r\n")) { + if (line == null) { + throw new IOException("Verbindung zu Waage verloren"); + } else if (line.Length < 4 || !line.StartsWith('<') || !line.EndsWith(">\r\n")) { throw new FormatException("Invalid response from scale"); } @@ -72,9 +74,16 @@ namespace Elwig.Helpers.Weighing { return line[1..^3]; } - protected async Task Weigh(bool incIdentNr) { - await SendCommand(incIdentNr ? $"RN{InternalScaleNr}" : $"RM{InternalScaleNr}"); - string record = await ReceiveResponse(); + protected async Task Weigh(bool incIdentNr, bool retry = true) { + string record; + try { + await SendCommand(incIdentNr ? $"RN{InternalScaleNr}" : $"RM{InternalScaleNr}"); + record = await ReceiveResponse(); + } catch (IOException) { + if (!retry || Tcp == null) throw; + ReconnectTcp(); + return await Weigh(incIdentNr, false); + } if (record.Length != 62) throw new FormatException("Invalid response from scale: Received record has invalid size"); var line = record[2..];