Migrated more types

This commit is contained in:
Benjamin Höglinger-Stelzer 2022-10-03 19:16:35 +02:00
parent b9ac62bbfd
commit aabf3897fd
4 changed files with 32 additions and 46 deletions

View File

@ -23,35 +23,43 @@ namespace Nefarius.Peripherals.SerialPort
_outQueueSize = outQs; _outQueueSize = outQs;
} }
internal const uint fCtsHold = 0x1;
internal const uint fDsrHold = 0x2;
internal const uint fRlsdHold = 0x4;
internal const uint fXoffHold = 0x8;
internal const uint fXoffSent = 0x10;
internal const uint fEof = 0x20;
internal const uint fTxim = 0x40;
/// <summary> /// <summary>
/// Output is blocked by CTS handshaking. /// Output is blocked by CTS handshaking.
/// </summary> /// </summary>
public bool CtsHold => (_status & COMSTAT.fCtsHold) != 0; public bool CtsHold => (_status & fCtsHold) != 0;
/// <summary> /// <summary>
/// Output is blocked by DRS handshaking. /// Output is blocked by DRS handshaking.
/// </summary> /// </summary>
public bool DsrHold => (_status & COMSTAT.fDsrHold) != 0; public bool DsrHold => (_status & fDsrHold) != 0;
/// <summary> /// <summary>
/// Output is blocked by RLSD handshaking. /// Output is blocked by RLSD handshaking.
/// </summary> /// </summary>
public bool RlsdHold => (_status & COMSTAT.fRlsdHold) != 0; public bool RlsdHold => (_status & fRlsdHold) != 0;
/// <summary> /// <summary>
/// Output is blocked because software handshaking is enabled and XOFF was received. /// Output is blocked because software handshaking is enabled and XOFF was received.
/// </summary> /// </summary>
public bool XoffHold => (_status & COMSTAT.fXoffHold) != 0; public bool XoffHold => (_status & fXoffHold) != 0;
/// <summary> /// <summary>
/// Output was blocked because XOFF was sent and this station is not yet ready to receive. /// Output was blocked because XOFF was sent and this station is not yet ready to receive.
/// </summary> /// </summary>
public bool XoffSent => (_status & COMSTAT.fXoffSent) != 0; public bool XoffSent => (_status & fXoffSent) != 0;
/// <summary> /// <summary>
/// There is a character waiting for transmission in the immediate buffer. /// There is a character waiting for transmission in the immediate buffer.
/// </summary> /// </summary>
public bool ImmediateWaiting => (_status & COMSTAT.fTxim) != 0; public bool ImmediateWaiting => (_status & fTxim) != 0;
/// <summary> /// <summary>
/// Number of bytes waiting in the input queue. /// Number of bytes waiting in the input queue.

View File

@ -9,7 +9,6 @@ using Windows.Win32.Storage.FileSystem;
using Microsoft.Win32.SafeHandles; using Microsoft.Win32.SafeHandles;
using Nefarius.Peripherals.SerialPort.Win32PInvoke; using Nefarius.Peripherals.SerialPort.Win32PInvoke;
using COMMPROP = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMMPROP; using COMMPROP = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMMPROP;
using COMSTAT = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMSTAT;
namespace Nefarius.Peripherals.SerialPort; namespace Nefarius.Peripherals.SerialPort;
@ -298,16 +297,20 @@ public class SerialPort : IDisposable
/// Get the status of the queues /// Get the status of the queues
/// </summary> /// </summary>
/// <returns>Queue status object</returns> /// <returns>Queue status object</returns>
protected QueueStatus GetQueueStatus() protected unsafe QueueStatus GetQueueStatus()
{ {
COMSTAT cs; COMSTAT cs;
COMMPROP cp; COMMPROP cp;
uint er; CLEAR_COMM_ERROR_FLAGS er;
CheckOnline(); CheckOnline();
if (!Win32Com.ClearCommError(_hPort.DangerousGetHandle(), out er, out cs)) ThrowException("Unexpected failure"); if (!PInvoke.ClearCommError(_hPort, &er, &cs))
if (!Win32Com.GetCommProperties(_hPort.DangerousGetHandle(), out cp)) ThrowException("Unexpected failure"); ThrowException("Unexpected failure");
return new QueueStatus(cs.Flags, cs.cbInQue, cs.cbOutQue, cp.dwCurrentRxQueue, cp.dwCurrentTxQueue);
if (!Win32Com.GetCommProperties(_hPort.DangerousGetHandle(), out cp))
ThrowException("Unexpected failure");
return new QueueStatus(cs._bitfield, cs.cbInQue, cs.cbOutQue, cp.dwCurrentRxQueue, cp.dwCurrentTxQueue);
} }
/// <summary> /// <summary>
@ -378,7 +381,7 @@ public class SerialPort : IDisposable
{ {
} }
private void ReceiveThread() private unsafe void ReceiveThread()
{ {
var buf = new byte[1]; var buf = new byte[1];
@ -413,16 +416,16 @@ public class SerialPort : IDisposable
eventMask = (uint)Marshal.ReadInt32(uMask); eventMask = (uint)Marshal.ReadInt32(uMask);
if ((eventMask & Win32Com.EV_ERR) != 0) if ((eventMask & Win32Com.EV_ERR) != 0)
{ {
uint errs; CLEAR_COMM_ERROR_FLAGS errs;
if (Win32Com.ClearCommError(_hPort.DangerousGetHandle(), out errs, IntPtr.Zero)) if (PInvoke.ClearCommError(_hPort, &errs, null))
{ {
var s = new StringBuilder("UART Error: ", 40); var s = new StringBuilder("UART Error: ", 40);
if ((errs & Win32Com.CE_FRAME) != 0) s = s.Append("Framing,"); if (((uint)errs & Win32Com.CE_FRAME) != 0) s = s.Append("Framing,");
if ((errs & Win32Com.CE_IOE) != 0) s = s.Append("IO,"); if (((uint)errs & Win32Com.CE_IOE) != 0) s = s.Append("IO,");
if ((errs & Win32Com.CE_OVERRUN) != 0) s = s.Append("Overrun,"); if (((uint)errs & Win32Com.CE_OVERRUN) != 0) s = s.Append("Overrun,");
if ((errs & Win32Com.CE_RXOVER) != 0) s = s.Append("Receive Overflow,"); if (((uint)errs & Win32Com.CE_RXOVER) != 0) s = s.Append("Receive Overflow,");
if ((errs & Win32Com.CE_RXPARITY) != 0) s = s.Append("Parity,"); if (((uint)errs & Win32Com.CE_RXPARITY) != 0) s = s.Append("Parity,");
if ((errs & Win32Com.CE_TXFULL) != 0) s = s.Append("Transmit Overflow,"); if (((uint)errs & Win32Com.CE_TXFULL) != 0) s = s.Append("Transmit Overflow,");
s.Length = s.Length - 1; s.Length = s.Length - 1;
throw new CommPortException(s.ToString()); throw new CommPortException(s.ToString());
} }

View File

@ -1,20 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{
[StructLayout(LayoutKind.Sequential)]
internal struct COMSTAT
{
internal const uint fCtsHold = 0x1;
internal const uint fDsrHold = 0x2;
internal const uint fRlsdHold = 0x4;
internal const uint fXoffHold = 0x8;
internal const uint fXoffSent = 0x10;
internal const uint fEof = 0x20;
internal const uint fTxim = 0x40;
internal UInt32 Flags;
internal UInt32 cbInQue;
internal UInt32 cbOutQue;
}
}

View File

@ -88,11 +88,6 @@ namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
internal static extern Boolean GetOverlappedResult(IntPtr hFile, IntPtr lpOverlapped, internal static extern Boolean GetOverlappedResult(IntPtr hFile, IntPtr lpOverlapped,
out UInt32 nNumberOfBytesTransferred, Boolean bWait); out UInt32 nNumberOfBytesTransferred, Boolean bWait);
[DllImport("kernel32.dll")]
internal static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, IntPtr lpStat);
[DllImport("kernel32.dll")]
internal static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, out COMSTAT cs);
//Constants for lpErrors: //Constants for lpErrors:
internal const UInt32 CE_RXOVER = 0x0001; internal const UInt32 CE_RXOVER = 0x0001;
internal const UInt32 CE_OVERRUN = 0x0002; internal const UInt32 CE_OVERRUN = 0x0002;