Added regions

Refactoring
This commit is contained in:
Benjamin Höglinger-Stelzer 2018-11-25 14:34:11 +01:00
parent dd7cbfddb3
commit 9b21c21a8e

View File

@ -12,6 +12,8 @@ namespace PInvokeSerialPort
/// </summary> /// </summary>
public class SerialPort : IDisposable public class SerialPort : IDisposable
{ {
#region Private fields
private readonly ManualResetEvent _writeEvent = new ManualResetEvent(false); private readonly ManualResetEvent _writeEvent = new ManualResetEvent(false);
private bool _auto; private bool _auto;
private bool _checkSends = true; private bool _checkSends = true;
@ -28,11 +30,9 @@ namespace PInvokeSerialPort
private int _stateRts = 2; private int _stateRts = 2;
private int _writeCount; private int _writeCount;
/// <summary> #endregion
/// If true, the port will automatically re-open on next send if it was previously closed due
/// to an error (default: false) #region Public properties
/// </summary>
public bool AutoReopen;
/// <summary> /// <summary>
/// Class constructor /// Class constructor
@ -51,6 +51,12 @@ namespace PInvokeSerialPort
BaudRate = baudRate; BaudRate = baudRate;
} }
/// <summary>
/// If true, the port will automatically re-open on next send if it was previously closed due
/// to an error (default: false)
/// </summary>
public bool AutoReopen { get; set; }
/// <summary> /// <summary>
/// Baud Rate (default: 115200) /// Baud Rate (default: 115200)
/// </summary> /// </summary>
@ -317,6 +323,8 @@ namespace PInvokeSerialPort
} }
} }
#endregion
/// <inheritdoc /> /// <inheritdoc />
/// <summary> /// <summary>
/// For IDisposable /// For IDisposable
@ -370,10 +378,25 @@ namespace PInvokeSerialPort
if (!Win32Com.SetCommTimeouts(_hPort, ref commTimeouts)) ThrowException("Bad timeout settings"); if (!Win32Com.SetCommTimeouts(_hPort, ref commTimeouts)) ThrowException("Bad timeout settings");
_stateBrk = 0; _stateBrk = 0;
if (UseDtr == HsOutput.None) _stateDtr = 0; switch (UseDtr)
if (UseDtr == HsOutput.Online) _stateDtr = 1; {
if (UseRts == HsOutput.None) _stateRts = 0; case HsOutput.None:
if (UseRts == HsOutput.Online) _stateRts = 1; _stateDtr = 0;
break;
case HsOutput.Online:
_stateDtr = 1;
break;
}
switch (UseRts)
{
case HsOutput.None:
_stateRts = 0;
break;
case HsOutput.Online:
_stateRts = 1;
break;
}
_checkSends = CheckAllSends; _checkSends = CheckAllSends;
wo.Offset = 0; wo.Offset = 0;
@ -385,13 +408,13 @@ namespace PInvokeSerialPort
_rxException = null; _rxException = null;
_rxExceptionReported = false; _rxExceptionReported = false;
// TODO: utilize Task Parallel Library here
_rxThread = new Thread(ReceiveThread) _rxThread = new Thread(ReceiveThread)
{ {
Name = "CommBaseRx", Name = "CommBaseRx", Priority = ThreadPriority.AboveNormal, IsBackground = true
Priority = ThreadPriority.AboveNormal
}; };
//If not set to true, my application process will not exit completely after UI closed
_rxThread.IsBackground = true;
_rxThread.Start(); _rxThread.Start();
Thread.Sleep(1); //Give rx thread time to start. By documentation, 0 should work, but it does not! Thread.Sleep(1); //Give rx thread time to start. By documentation, 0 should work, but it does not!