SerialPorts: - dispose

This commit is contained in:
Thomas Wiedebusch 2025-05-14 10:24:08 +02:00
parent 8a3ab91b03
commit 5ba2fd5e65
3 changed files with 21 additions and 13 deletions

View File

@ -14,4 +14,4 @@ using System.Reflection;
//[assembly: AssemblyVersion("1.2.*.0")]
[assembly: AssemblyVersion("2.6.19.*")]
[assembly: AssemblyVersion("2.6.20.*")]

View File

@ -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.
/// </remarks>
/// <remarks date="2025-Mai-12" author="T.Wiedebusch">
/// <remarks date="2025-Mai-12..14" author="T.Wiedebusch">
/// - Dispose procedure changed.
/// </remarks>
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");
}
}

View File

@ -1 +1 @@
7df2ad6be70922b931820861acc675522243ee78
8cb55c312de586b683d72cf58c13e2801d10e14b767777d98338d3e1473a1213