diff --git a/Nefarius.Peripherals.SerialPort/NativeMethods.txt b/Nefarius.Peripherals.SerialPort/NativeMethods.txt index aa60595..15e5192 100644 --- a/Nefarius.Peripherals.SerialPort/NativeMethods.txt +++ b/Nefarius.Peripherals.SerialPort/NativeMethods.txt @@ -15,4 +15,5 @@ GetCommModemStatus GetOverlappedResult ClearCommError GetCommProperties -WIN32_ERROR \ No newline at end of file +WIN32_ERROR +GetHandleInformation \ No newline at end of file diff --git a/Nefarius.Peripherals.SerialPort/Nefarius.Peripherals.SerialPort.csproj b/Nefarius.Peripherals.SerialPort/Nefarius.Peripherals.SerialPort.csproj index 0a6e29e..91af2d0 100644 --- a/Nefarius.Peripherals.SerialPort/Nefarius.Peripherals.SerialPort.csproj +++ b/Nefarius.Peripherals.SerialPort/Nefarius.Peripherals.SerialPort.csproj @@ -35,5 +35,6 @@ all + \ No newline at end of file diff --git a/Nefarius.Peripherals.SerialPort/SerialPort.cs b/Nefarius.Peripherals.SerialPort/SerialPort.cs index fbe1613..7dde20b 100644 --- a/Nefarius.Peripherals.SerialPort/SerialPort.cs +++ b/Nefarius.Peripherals.SerialPort/SerialPort.cs @@ -257,11 +257,10 @@ public partial class SerialPort : IDisposable CheckResult(); _writeCount = toSend.GetLength(0); - fixed (byte* ptr = toSend) fixed (NativeOverlapped* ptrOl = &_ptrUwo) { uint sent; - if (PInvoke.WriteFile(_hPort, ptr, (uint)_writeCount, &sent, ptrOl)) + if (PInvoke.WriteFile(_hPort, toSend.AsSpan(), &sent, ptrOl)) { _writeCount -= (int)sent; } @@ -460,17 +459,16 @@ public partial class SerialPort : IDisposable private unsafe void ReceiveThread() { - byte* buf = stackalloc byte[1]; - + byte[] buffer = new byte[1]; AutoResetEvent sg = new(false); NativeOverlapped ov = new() { EventHandle = sg.SafeWaitHandle.DangerousGetHandle() }; - COMM_EVENT_MASK eventMask = 0; - try { while (true) { + COMM_EVENT_MASK eventMask = 0; + if (!PInvoke.SetCommMask(_hPort, COMM_EVENT_MASK.EV_RXCHAR | COMM_EVENT_MASK.EV_TXEMPTY | COMM_EVENT_MASK.EV_CTS | COMM_EVENT_MASK.EV_DSR @@ -540,7 +538,7 @@ public partial class SerialPort : IDisposable uint gotBytes; do { - if (!PInvoke.ReadFile(_hPort, buf, 1, &gotBytes, &ov)) + if (!PInvoke.ReadFile(_hPort, buffer, &gotBytes, &ov)) { if (Marshal.GetLastWin32Error() == (int)WIN32_ERROR.ERROR_IO_PENDING) { @@ -555,7 +553,7 @@ public partial class SerialPort : IDisposable if (gotBytes == 1) { - OnRxChar(buf[0]); + OnRxChar(buffer[0]); } } while (gotBytes > 0); } @@ -593,8 +591,7 @@ public partial class SerialPort : IDisposable if (i != 0) { - uint f; - if (!Win32Com.GetCommModemStatus(_hPort.DangerousGetHandle(), out f)) + if (!Win32Com.GetCommModemStatus(_hPort.DangerousGetHandle(), out uint f)) { throw new CommPortException("IO Error [005]"); } @@ -643,4 +640,4 @@ public partial class SerialPort : IDisposable ThrowException("Offline"); return false; } -} +} \ No newline at end of file