Migrated most native APIs to CsWin32

This commit is contained in:
Benjamin Höglinger-Stelzer 2022-09-26 21:44:50 +02:00
parent 8d0a29c0b7
commit 49e2992933
11 changed files with 871 additions and 1029 deletions

View File

@ -1,29 +1,36 @@
using Nefarius.Peripherals.SerialPort.Win32PInvoke;
using Windows.Win32.Devices.Communication;
namespace Nefarius.Peripherals.SerialPort
namespace Nefarius.Peripherals.SerialPort;
/// <summary>
/// Represents the status of the modem control input signals.
/// </summary>
public readonly struct ModemStatus
{
/// <summary>
/// Represents the status of the modem control input signals.
/// </summary>
public struct ModemStatus
{
private readonly uint _status;
internal ModemStatus(uint val) { _status = val; }
internal ModemStatus(uint val)
{
_status = val;
}
/// <summary>
/// Condition of the Clear To Send signal.
/// </summary>
public bool Cts { get { return ((_status & Win32Com.MS_CTS_ON) != 0); } }
public bool Cts => (_status & (uint)MODEM_STATUS_FLAGS.MS_CTS_ON) != 0;
/// <summary>
/// Condition of the Data Set Ready signal.
/// </summary>
public bool Dsr { get { return ((_status & Win32Com.MS_DSR_ON) != 0); } }
public bool Dsr => (_status & (uint)MODEM_STATUS_FLAGS.MS_DSR_ON) != 0;
/// <summary>
/// Condition of the Receive Line Status Detection signal.
/// </summary>
public bool Rlsd { get { return ((_status & Win32Com.MS_RLSD_ON) != 0); } }
public bool Rlsd => (_status & (uint)MODEM_STATUS_FLAGS.MS_RLSD_ON) != 0;
/// <summary>
/// Condition of the Ring Detection signal.
/// </summary>
public bool Ring { get { return ((_status & Win32Com.MS_RING_ON) != 0); } }
}
public bool Ring => (_status & (uint)MODEM_STATUS_FLAGS.MS_RING_ON) != 0;
}

View File

@ -14,3 +14,4 @@ GetCommModemStatus
GetOverlappedResult
ClearCommError
GetCommProperties
WIN32_ERROR

View File

@ -30,6 +30,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2022.1.0" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.63-beta">
<PrivateAssets>all</PrivateAssets>
</PackageReference>

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,23 @@
using Windows.Win32.Devices.Communication;
namespace Nefarius.Peripherals.SerialPort;
internal static class DCBExtensions
{
public static void Init(this DCB dcb, bool parity, bool outCts, bool outDsr, int dtr, bool inDsr, bool txc,
bool xOut,
bool xIn, int rts)
{
dcb.DCBlength = 28;
dcb._bitfield = 0x8001;
if (parity) dcb._bitfield |= 0x0002;
if (outCts) dcb._bitfield |= 0x0004;
if (outDsr) dcb._bitfield |= 0x0008;
dcb._bitfield |= (uint)((dtr & 0x0003) << 4);
if (inDsr) dcb._bitfield |= 0x0040;
if (txc) dcb._bitfield |= 0x0080;
if (xOut) dcb._bitfield |= 0x0100;
if (xIn) dcb._bitfield |= 0x0200;
dcb._bitfield |= (uint)((rts & 0x0003) << 12);
}
}

View File

@ -1,28 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{
[StructLayout(LayoutKind.Sequential)]
internal struct COMMPROP
{
internal UInt16 wPacketLength;
internal UInt16 wPacketVersion;
internal UInt32 dwServiceMask;
internal UInt32 dwReserved1;
internal UInt32 dwMaxTxQueue;
internal UInt32 dwMaxRxQueue;
internal UInt32 dwMaxBaud;
internal UInt32 dwProvSubType;
internal UInt32 dwProvCapabilities;
internal UInt32 dwSettableParams;
internal UInt32 dwSettableBaud;
internal UInt16 wSettableData;
internal UInt16 wSettableStopParity;
internal UInt32 dwCurrentTxQueue;
internal UInt32 dwCurrentRxQueue;
internal UInt32 dwProvSpec1;
internal UInt32 dwProvSpec2;
internal Byte wcProvChar;
}
}

View File

@ -1,15 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{
[StructLayout(LayoutKind.Sequential)]
internal struct COMMTIMEOUTS
{
internal Int32 ReadIntervalTimeout;
internal Int32 ReadTotalTimeoutMultiplier;
internal Int32 ReadTotalTimeoutConstant;
internal Int32 WriteTotalTimeoutMultiplier;
internal Int32 WriteTotalTimeoutConstant;
}
}

View File

@ -1,11 +1,11 @@
using System;
using System.Runtime.InteropServices;
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke;
[StructLayout(LayoutKind.Sequential)]
internal struct COMSTAT
{
[StructLayout(LayoutKind.Sequential)]
internal struct COMSTAT
{
internal const uint fCtsHold = 0x1;
internal const uint fDsrHold = 0x2;
internal const uint fRlsdHold = 0x4;
@ -16,5 +16,4 @@ namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
internal UInt32 Flags;
internal UInt32 cbInQue;
internal UInt32 cbOutQue;
}
}

View File

@ -1,41 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{
[StructLayout(LayoutKind.Sequential)]
internal struct DCB
{
internal Int32 DCBlength;
internal Int32 BaudRate;
internal Int32 PackedValues;
internal Int16 wReserved;
internal Int16 XonLim;
internal Int16 XoffLim;
internal Byte ByteSize;
internal Byte Parity;
internal Byte StopBits;
internal Byte XonChar;
internal Byte XoffChar;
internal Byte ErrorChar;
internal Byte EofChar;
internal Byte EvtChar;
internal Int16 wReserved1;
internal void Init(bool parity, bool outCts, bool outDsr, int dtr, bool inDsr, bool txc, bool xOut,
bool xIn, int rts)
{
DCBlength = 28; PackedValues = 0x8001;
if (parity) PackedValues |= 0x0002;
if (outCts) PackedValues |= 0x0004;
if (outDsr) PackedValues |= 0x0008;
PackedValues |= ((dtr & 0x0003) << 4);
if (inDsr) PackedValues |= 0x0040;
if (txc) PackedValues |= 0x0080;
if (xOut) PackedValues |= 0x0100;
if (xIn) PackedValues |= 0x0200;
PackedValues |= ((rts & 0x0003) << 12);
}
}
}

View File

@ -1,15 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{
[StructLayout(LayoutKind.Sequential)]
internal struct OVERLAPPED
{
internal UIntPtr Internal;
internal UIntPtr InternalHigh;
internal UInt32 Offset;
internal UInt32 OffsetHigh;
internal IntPtr hEvent;
}
}

View File

@ -1,155 +0,0 @@
using System;
using System.Runtime.InteropServices;
namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{
internal class Win32Com
{
/// <summary>
/// Opening Testing and Closing the Port Handle.
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile);
//Constants for errors:
internal const UInt32 ERROR_FILE_NOT_FOUND = 2;
internal const UInt32 ERROR_INVALID_NAME = 123;
internal const UInt32 ERROR_ACCESS_DENIED = 5;
internal const UInt32 ERROR_IO_PENDING = 997;
//Constants for return value:
internal const Int32 INVALID_HANDLE_VALUE = -1;
//Constants for dwFlagsAndAttributes:
internal const UInt32 FILE_FLAG_OVERLAPPED = 0x40000000;
//Constants for dwCreationDisposition:
internal const UInt32 OPEN_EXISTING = 3;
//Constants for dwDesiredAccess:
internal const UInt32 GENERIC_READ = 0x80000000;
internal const UInt32 GENERIC_WRITE = 0x40000000;
[DllImport("kernel32.dll")]
internal static extern Boolean CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
internal static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags);
/// <summary>
/// Manipulating the communications settings.
/// </summary>
[DllImport("kernel32.dll")]
internal static extern Boolean GetCommState(IntPtr hFile, ref DCB lpDCB);
[DllImport("kernel32.dll")]
internal static extern Boolean GetCommTimeouts(IntPtr hFile, out COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
internal static extern Boolean BuildCommDCBAndTimeouts(String lpDef, ref DCB lpDCB, ref COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
internal static extern Boolean SetCommState(IntPtr hFile, [In] ref DCB lpDCB);
[DllImport("kernel32.dll")]
internal static extern Boolean SetCommTimeouts(IntPtr hFile, [In] ref COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
internal static extern Boolean SetupComm(IntPtr hFile, UInt32 dwInQueue, UInt32 dwOutQueue);
/// <summary>
/// Reading and writing.
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
[DllImport("kernel32.dll")]
internal static extern Boolean SetCommMask(IntPtr hFile, UInt32 dwEvtMask);
// Constants for dwEvtMask:
internal const UInt32 EV_RXCHAR = 0x0001;
internal const UInt32 EV_RXFLAG = 0x0002;
internal const UInt32 EV_TXEMPTY = 0x0004;
internal const UInt32 EV_CTS = 0x0008;
internal const UInt32 EV_DSR = 0x0010;
internal const UInt32 EV_RLSD = 0x0020;
internal const UInt32 EV_BREAK = 0x0040;
internal const UInt32 EV_ERR = 0x0080;
internal const UInt32 EV_RING = 0x0100;
internal const UInt32 EV_PERR = 0x0200;
internal const UInt32 EV_RX80FULL = 0x0400;
internal const UInt32 EV_EVENT1 = 0x0800;
internal const UInt32 EV_EVENT2 = 0x1000;
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern Boolean WaitCommEvent(IntPtr hFile, IntPtr lpEvtMask, IntPtr lpOverlapped);
[DllImport("kernel32.dll")]
internal static extern Boolean CancelIo(IntPtr hFile);
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern Boolean ReadFile(IntPtr hFile, [Out] Byte[] lpBuffer, UInt32 nNumberOfBytesToRead,
out UInt32 nNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll")]
internal static extern Boolean TransmitCommChar(IntPtr hFile, Byte cChar);
/// <summary>
/// Control port functions.
/// </summary>
[DllImport("kernel32.dll")]
internal static extern Boolean EscapeCommFunction(IntPtr hFile, UInt32 dwFunc);
// Constants for dwFunc:
internal const UInt32 SETXOFF = 1;
internal const UInt32 SETXON = 2;
internal const UInt32 SETRTS = 3;
internal const UInt32 CLRRTS = 4;
internal const UInt32 SETDTR = 5;
internal const UInt32 CLRDTR = 6;
internal const UInt32 RESETDEV = 7;
internal const UInt32 SETBREAK = 8;
internal const UInt32 CLRBREAK = 9;
[DllImport("kernel32.dll")]
internal static extern Boolean GetCommModemStatus(IntPtr hFile, out UInt32 lpModemStat);
// Constants for lpModemStat:
internal const UInt32 MS_CTS_ON = 0x0010;
internal const UInt32 MS_DSR_ON = 0x0020;
internal const UInt32 MS_RING_ON = 0x0040;
internal const UInt32 MS_RLSD_ON = 0x0080;
/// <summary>
/// Status Functions.
/// </summary>
[DllImport("kernel32.dll", SetLastError = true)]
internal static extern Boolean GetOverlappedResult(IntPtr hFile, IntPtr lpOverlapped,
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:
internal const UInt32 CE_RXOVER = 0x0001;
internal const UInt32 CE_OVERRUN = 0x0002;
internal const UInt32 CE_RXPARITY = 0x0004;
internal const UInt32 CE_FRAME = 0x0008;
internal const UInt32 CE_BREAK = 0x0010;
internal const UInt32 CE_TXFULL = 0x0100;
internal const UInt32 CE_PTO = 0x0200;
internal const UInt32 CE_IOE = 0x0400;
internal const UInt32 CE_DNS = 0x0800;
internal const UInt32 CE_OOP = 0x1000;
internal const UInt32 CE_MODE = 0x8000;
[DllImport("kernel32.dll")]
internal static extern Boolean GetCommProperties(IntPtr hFile, out COMMPROP cp);
}
}