From 5ba2fd5e65d29166c16632f830d982bb7d8f5744 Mon Sep 17 00:00:00 2001 From: Thomas Wiedebusch Date: Wed, 14 May 2025 10:24:08 +0200 Subject: [PATCH] SerialPorts: - dispose --- Common/.shared/SharedAssemblyInfo.cs | 2 +- .../Ports/SerialPorts/BaseSerialPort.cs | 30 ++++++++++++------- ...erRegisters.csproj.CoreCompileInputs.cache | 2 +- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Common/.shared/SharedAssemblyInfo.cs b/Common/.shared/SharedAssemblyInfo.cs index bb17108a..8b771807 100644 --- a/Common/.shared/SharedAssemblyInfo.cs +++ b/Common/.shared/SharedAssemblyInfo.cs @@ -14,4 +14,4 @@ using System.Reflection; //[assembly: AssemblyVersion("1.2.*.0")] -[assembly: AssemblyVersion("2.6.19.*")] +[assembly: AssemblyVersion("2.6.20.*")] diff --git a/Common/Hardware/Interfaces/Ports/SerialPorts/BaseSerialPort.cs b/Common/Hardware/Interfaces/Ports/SerialPorts/BaseSerialPort.cs index 9db4a064..61dd9b13 100644 --- a/Common/Hardware/Interfaces/Ports/SerialPorts/BaseSerialPort.cs +++ b/Common/Hardware/Interfaces/Ports/SerialPorts/BaseSerialPort.cs @@ -419,7 +419,7 @@ namespace Xylem.Common.Hardware.Interfaces.Ports.SerialPorts /// The IrDA sniffer showed these on the communication line, but they are incompletely received. /// Assuming the DiscardInBuffer may be delayed, so the incoming data will be scrapped. /// - /// + /// /// - Dispose procedure changed. /// private void ReadingThreadLoop() @@ -451,8 +451,9 @@ namespace Xylem.Common.Hardware.Interfaces.Ports.SerialPorts if (SyncMarkRecord.SkipDecoding != dataSyncMarkThisRun) { OnRawRecordReceived?.Invoke(this, new StringPortDataEventArgs(rxStringRecord, - _receiveTimeStampPc, dataSyncMarkThisRun)); + _receiveTimeStampPc, dataSyncMarkThisRun)); } + if (RecordStreamingRawData) { AsciiDataLogger.Trace($"{rxStringRecord}"); @@ -506,12 +507,14 @@ namespace Xylem.Common.Hardware.Interfaces.Ports.SerialPorts if (PortSettingsForTransmitProtocol.DoubleSyncByte) { if (rxByte.Equals(PortSettingsForTransmitProtocol.ProtSyncByte) && - rxByteRecord[rxByteRecord.Count - 1].Equals(PortSettingsForTransmitProtocol.ProtSyncByte)) + rxByteRecord[rxByteRecord.Count - 1] + .Equals(PortSettingsForTransmitProtocol.ProtSyncByte)) { //skips this byte and read the next one rxByte = (Byte)_serialPort.ReadByte(); } } + //add byte to receive result buffer rxByteRecord.Add(rxByte); @@ -555,7 +558,9 @@ namespace Xylem.Common.Hardware.Interfaces.Ports.SerialPorts } } catch (ThreadAbortException) - { throw; } + { + throw; + } catch (TimeoutException) { //ATTENTION: This "_serialPort.DiscardInBuffer()" caused a lot of trouble as it flushes a few incoming @@ -578,12 +583,12 @@ namespace Xylem.Common.Hardware.Interfaces.Ports.SerialPorts if (_receiveThread.ThreadState == ThreadState.Aborted || _receiveThread.ThreadState == ThreadState.AbortRequested) { - _byteDataLogger.Warn(ex, $"{ Ident} ThreadState is Aborted or AbortRequested but an " + - "error occurred while reading records from serial port"); + _byteDataLogger.Warn(ex, $"{Ident} ThreadState is Aborted or AbortRequested but an " + + "error occurred while reading records from serial port"); } else { - _byteDataLogger.Error(ex, $"{ Ident} Error while reading records from serial port"); + _byteDataLogger.Error(ex, $"{Ident} Error while reading records from serial port"); } } finally @@ -601,10 +606,12 @@ namespace Xylem.Common.Hardware.Interfaces.Ports.SerialPorts } } } - }//while (!_tokenReadData.IsCancellationRequested) - }// over hole reading thread loop + } //while (!_tokenReadData.IsCancellationRequested) + } // over hole reading thread loop catch (ThreadAbortException) - { } + { + _byteDataLogger.Info($"{Ident} Thread abort exception fired!"); + } catch (Exception ex) { _byteDataLogger.Error($"{Ident} {ex.Message}"); @@ -626,7 +633,8 @@ namespace Xylem.Common.Hardware.Interfaces.Ports.SerialPorts _serialPort.DiscardInBuffer(); _serialPort.DiscardOutBuffer(); _serialPort.Close(); - + Thread.Sleep(500); + _serialPort.Dispose(); _byteDataLogger.Info($"{Ident} Port is closed"); } } diff --git a/Common/Hardware/WaterMeter/WaterMeterCore/WaterMeterRegisters/obj/Debug/WaterMeterRegisters.csproj.CoreCompileInputs.cache b/Common/Hardware/WaterMeter/WaterMeterCore/WaterMeterRegisters/obj/Debug/WaterMeterRegisters.csproj.CoreCompileInputs.cache index 9389613c..4163759b 100644 --- a/Common/Hardware/WaterMeter/WaterMeterCore/WaterMeterRegisters/obj/Debug/WaterMeterRegisters.csproj.CoreCompileInputs.cache +++ b/Common/Hardware/WaterMeter/WaterMeterCore/WaterMeterRegisters/obj/Debug/WaterMeterRegisters.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -7df2ad6be70922b931820861acc675522243ee78 +8cb55c312de586b683d72cf58c13e2801d10e14b767777d98338d3e1473a1213