Compare commits
2 Commits
v1.1.0-pre
...
v1.1.0-pre
Author | SHA1 | Date | |
---|---|---|---|
1101b71faf | |||
582412f4dc |
@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
|
||||||
namespace Nefarius.Peripherals.SerialPort;
|
namespace Nefarius.Peripherals.SerialPort;
|
||||||
|
|
||||||
@ -22,4 +23,14 @@ public class CommPortException : ApplicationException
|
|||||||
public CommPortException(Exception e) : base("Receive Thread Exception", e)
|
public CommPortException(Exception e) : base("Receive Thread Exception", e)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CommPortException(string message, int error) : base(message)
|
||||||
|
{
|
||||||
|
Win32Error = error;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The Win32 error that caused the exception.
|
||||||
|
/// </summary>
|
||||||
|
public int Win32Error { get; } = Marshal.GetLastWin32Error();
|
||||||
}
|
}
|
@ -30,6 +30,7 @@ public partial class SerialPort : IDisposable
|
|||||||
private Exception _rxException;
|
private Exception _rxException;
|
||||||
private bool _rxExceptionReported;
|
private bool _rxExceptionReported;
|
||||||
private Thread _rxThread;
|
private Thread _rxThread;
|
||||||
|
private CancellationTokenSource _cts;
|
||||||
private int _stateBrk = 2;
|
private int _stateBrk = 2;
|
||||||
private int _stateDtr = 2;
|
private int _stateDtr = 2;
|
||||||
private int _stateRts = 2;
|
private int _stateRts = 2;
|
||||||
@ -70,8 +71,12 @@ public partial class SerialPort : IDisposable
|
|||||||
}
|
}
|
||||||
|
|
||||||
_hPort = PInvoke.CreateFile(PortName,
|
_hPort = PInvoke.CreateFile(PortName,
|
||||||
(uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE), 0,
|
(uint)(FILE_ACCESS_RIGHTS.FILE_GENERIC_READ | FILE_ACCESS_RIGHTS.FILE_GENERIC_WRITE),
|
||||||
null, FILE_CREATION_DISPOSITION.OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES.FILE_FLAG_OVERLAPPED, null);
|
0,
|
||||||
|
null,
|
||||||
|
FILE_CREATION_DISPOSITION.OPEN_EXISTING, FILE_FLAGS_AND_ATTRIBUTES.FILE_FLAG_OVERLAPPED,
|
||||||
|
null
|
||||||
|
);
|
||||||
|
|
||||||
if (_hPort.IsInvalid)
|
if (_hPort.IsInvalid)
|
||||||
{
|
{
|
||||||
@ -80,7 +85,7 @@ public partial class SerialPort : IDisposable
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CommPortException("Port Open Failure");
|
throw new CommPortException("Port Open Failure", Marshal.GetLastWin32Error());
|
||||||
}
|
}
|
||||||
|
|
||||||
_online = true;
|
_online = true;
|
||||||
@ -147,6 +152,7 @@ public partial class SerialPort : IDisposable
|
|||||||
_rxException = null;
|
_rxException = null;
|
||||||
_rxExceptionReported = false;
|
_rxExceptionReported = false;
|
||||||
|
|
||||||
|
_cts = new CancellationTokenSource();
|
||||||
_rxThread = new Thread(ReceiveThread)
|
_rxThread = new Thread(ReceiveThread)
|
||||||
{
|
{
|
||||||
Name = "CommBaseRx", Priority = ThreadPriority.AboveNormal, IsBackground = true
|
Name = "CommBaseRx", Priority = ThreadPriority.AboveNormal, IsBackground = true
|
||||||
@ -182,10 +188,12 @@ public partial class SerialPort : IDisposable
|
|||||||
|
|
||||||
private void InternalClose()
|
private void InternalClose()
|
||||||
{
|
{
|
||||||
|
_cts.Cancel();
|
||||||
PInvoke.CancelIo(_hPort);
|
PInvoke.CancelIo(_hPort);
|
||||||
|
|
||||||
if (_rxThread != null)
|
if (_rxThread != null)
|
||||||
{
|
{
|
||||||
_rxThread.Abort();
|
_rxThread.Join();
|
||||||
_rxThread = null;
|
_rxThread = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -457,7 +465,7 @@ public partial class SerialPort : IDisposable
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while (true)
|
while (!_cts.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
COMM_EVENT_MASK eventMask = 0;
|
COMM_EVENT_MASK eventMask = 0;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user