[#74] Weighing: Try to reconnect tcp scales on first error
All checks were successful
Test / Run tests (push) Successful in 3m0s

This commit is contained in:
2026-02-25 10:57:44 +01:00
parent b58dee6d3f
commit 1108427023
3 changed files with 33 additions and 7 deletions

View File

@@ -33,7 +33,9 @@ namespace Elwig.Helpers.Weighing {
protected async Task<string> 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<WeighingResult> Weigh(bool incIdentNr) {
await SendCommand(incIdentNr ? $"RN{InternalScaleNr}" : $"RM{InternalScaleNr}");
string record = await ReceiveResponse();
protected async Task<WeighingResult> 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..];