From cbb57f70392767b88c38a229e0be44b20ecc8e5f Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Sat, 12 Aug 2023 00:25:48 +0200 Subject: [PATCH] Control serial port has not to be equal with scale serial port any more --- Elwig/Helpers/Weighing/SystecScale.cs | 52 +++++++++++++++++++-------- Setup/Files/config.ini | 8 +++-- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/Elwig/Helpers/Weighing/SystecScale.cs b/Elwig/Helpers/Weighing/SystecScale.cs index c5e0101..17b66b0 100644 --- a/Elwig/Helpers/Weighing/SystecScale.cs +++ b/Elwig/Helpers/Weighing/SystecScale.cs @@ -11,6 +11,7 @@ namespace Elwig.Helpers.Weighing { protected enum Output { RTS, DTR, OUT1, OUT2 }; protected SerialPort? Serial = null; + protected SerialPort? ControlSerialEmpty = null, ControlSerialFilling = null; protected TcpClient? Tcp = null; protected Stream Stream; @@ -27,7 +28,7 @@ namespace Elwig.Helpers.Weighing { public int? WeightLimit { get; private set; } public string? LogPath { get; private set; } - public SystecScale(string id, string model, string connection, string? empty = null, string? fill = null, int? limit = null, string? log = null) { + public SystecScale(string id, string model, string connection, string? empty = null, string? filling = null, int? limit = null, string? log = null) { ScaleId = id; Model = model; IsReady = true; @@ -46,11 +47,28 @@ namespace Elwig.Helpers.Weighing { if (empty != null) { var parts = empty.Split(':'); - EmptyMode = ConvertOutput(parts[0]); - EmptyDelay = int.Parse(parts[1]); + if (parts.Length == 3) { + if (parts[0] != Serial?.PortName) + ControlSerialEmpty = Utils.OpenSerialConnection($"serial://{parts[0]}:9600"); + } else if (parts.Length != 2) { + throw new ArgumentException("Invalid value for 'empty'"); + } + EmptyMode = ConvertOutput(parts[^2]); + EmptyDelay = int.Parse(parts[^1]); } - FillingClearanceMode = ConvertOutput(fill); + WeightLimit = limit; + if (filling != null) { + var parts = filling.Split(':'); + if (parts.Length == 2) { + if (parts[0] != Serial?.PortName) + ControlSerialFilling = parts[0] != ControlSerialEmpty?.PortName ? Utils.OpenSerialConnection($"serial://{parts[0]}:9600") : ControlSerialEmpty; + } else if (parts.Length != 1) { + throw new ArgumentException("Invalid value for 'filling'"); + } + FillingClearanceMode = ConvertOutput(parts[^1]); + } + if (FillingClearanceMode != null && WeightLimit == null) throw new ArgumentException("Weight limit has to be set, if filling clearance supervision is enabled"); } @@ -58,6 +76,8 @@ namespace Elwig.Helpers.Weighing { public void Dispose() { Stream.Close(); Serial?.Close(); + ControlSerialEmpty?.Close(); + ControlSerialFilling?.Close(); Tcp?.Close(); GC.SuppressFinalize(this); } @@ -117,7 +137,7 @@ namespace Elwig.Helpers.Weighing { throw new IOException($"Invalid response from scale (error code {error})"); } - return line[1..(line.Length - 1)]; + return line[1..^1]; } protected async Task Weigh(bool incIdentNr) { @@ -164,14 +184,15 @@ namespace Elwig.Helpers.Weighing { } public async Task Empty() { - if (EmptyMode == Output.RTS && Serial != null) { - Serial.RtsEnable = true; + SerialPort? p = ControlSerialEmpty ?? Serial; + if (EmptyMode == Output.RTS && p != null) { + p.RtsEnable = true; await Task.Delay(EmptyDelay); - Serial.RtsEnable = false; - } else if (EmptyMode == Output.DTR && Serial != null) { - Serial.DtrEnable = true; + p.RtsEnable = false; + } else if (EmptyMode == Output.DTR && p != null) { + p.DtrEnable = true; await Task.Delay(EmptyDelay); - Serial.DtrEnable = false; + p.DtrEnable = false; } else if (EmptyMode == Output.OUT1 || EmptyMode == Output.OUT2) { int output = EmptyMode == Output.OUT1 ? 1 : 2; await SendCommand($"OS{output:02i}"); @@ -183,10 +204,11 @@ namespace Elwig.Helpers.Weighing { } protected async Task SetFillingClearance(bool status) { - if (FillingClearanceMode == Output.RTS && Serial != null) { - Serial.RtsEnable = status; - } else if (FillingClearanceMode == Output.DTR && Serial != null) { - Serial.DtrEnable = status; + SerialPort? p = ControlSerialFilling ?? Serial; + if (FillingClearanceMode == Output.RTS && p != null) { + p.RtsEnable = status; + } else if (FillingClearanceMode == Output.DTR && p != null) { + p.DtrEnable = status; } else if (FillingClearanceMode == Output.OUT1 || FillingClearanceMode == Output.OUT2) { string cmd = status ? "OS" : "OC"; int output = FillingClearanceMode == Output.OUT1 ? 1 : 2; diff --git a/Setup/Files/config.ini b/Setup/Files/config.ini index 5e99e26..3f20b5a 100644 --- a/Setup/Files/config.ini +++ b/Setup/Files/config.ini @@ -13,8 +13,11 @@ file = database.sqlite3 ;type = systec ;model = IT3000A ;connection = serial://COM1:9600,8,N,1 -;empty = RTS:1000 -;limit = 4000 +;empty = COM2:RTS:1000 +;filling = DTR +; Limit on when the filling clearance should be removed +;limit = 3500 +; Enables scale logging ;log = waage.log ;[scale.B] @@ -23,4 +26,5 @@ file = database.sqlite3 ;connection = tcp://10.0.0.1:1234 ;empty = OUT1:3000 ;filling = OUT2 +; Limit on when the filling clearance should be removed ;limit = 3000