Migrated to CsWin32 #1

Merged
nefarius merged 14 commits from nefarius/bugfix/refactoring into master 2024-07-13 15:32:14 +02:00
3 changed files with 13 additions and 25 deletions
Showing only changes of commit 5e30d62651 - Show all commits

View File

@ -18,3 +18,5 @@ WIN32_ERROR
WaitCommEvent WaitCommEvent
WriteFile WriteFile
CreateFile CreateFile
ESCAPE_COMM_FUNCTION
EscapeCommFunction

View File

@ -1,4 +1,7 @@
using Nefarius.Peripherals.SerialPort.Win32PInvoke; using Windows.Win32;
using Windows.Win32.Devices.Communication;
using Nefarius.Peripherals.SerialPort.Win32PInvoke;
namespace Nefarius.Peripherals.SerialPort; namespace Nefarius.Peripherals.SerialPort;
@ -141,14 +144,14 @@ public partial class SerialPort
CheckOnline(); CheckOnline();
if (value) if (value)
{ {
if (Win32Com.EscapeCommFunction(_hPort.DangerousGetHandle(), Win32Com.SETRTS)) if (PInvoke.EscapeCommFunction(_hPort, ESCAPE_COMM_FUNCTION.SETRTS))
_stateRts = 1; _stateRts = 1;
else else
ThrowException("Unexpected Failure"); ThrowException("Unexpected Failure");
} }
else else
{ {
if (Win32Com.EscapeCommFunction(_hPort.DangerousGetHandle(), Win32Com.CLRRTS)) if (PInvoke.EscapeCommFunction(_hPort, ESCAPE_COMM_FUNCTION.CLRRTS))
_stateRts = 1; _stateRts = 1;
else else
ThrowException("Unexpected Failure"); ThrowException("Unexpected Failure");
@ -173,14 +176,14 @@ public partial class SerialPort
CheckOnline(); CheckOnline();
if (value) if (value)
{ {
if (Win32Com.EscapeCommFunction(_hPort.DangerousGetHandle(), Win32Com.SETDTR)) if (PInvoke.EscapeCommFunction(_hPort, ESCAPE_COMM_FUNCTION.SETDTR))
_stateDtr = 1; _stateDtr = 1;
else else
ThrowException("Unexpected Failure"); ThrowException("Unexpected Failure");
} }
else else
{ {
if (Win32Com.EscapeCommFunction(_hPort.DangerousGetHandle(), Win32Com.CLRDTR)) if (PInvoke.EscapeCommFunction(_hPort, ESCAPE_COMM_FUNCTION.CLRDTR))
_stateDtr = 0; _stateDtr = 0;
else else
ThrowException("Unexpected Failure"); ThrowException("Unexpected Failure");
@ -200,14 +203,14 @@ public partial class SerialPort
CheckOnline(); CheckOnline();
if (value) if (value)
{ {
if (Win32Com.EscapeCommFunction(_hPort.DangerousGetHandle(), Win32Com.SETBREAK)) if (PInvoke.EscapeCommFunction(_hPort, ESCAPE_COMM_FUNCTION.SETBREAK))
_stateBrk = 0; _stateBrk = 0;
else else
ThrowException("Unexpected Failure"); ThrowException("Unexpected Failure");
} }
else else
{ {
if (Win32Com.EscapeCommFunction(_hPort.DangerousGetHandle(), Win32Com.CLRBREAK)) if (PInvoke.EscapeCommFunction(_hPort, ESCAPE_COMM_FUNCTION.CLRBREAK))
_stateBrk = 0; _stateBrk = 0;
else else
ThrowException("Unexpected Failure"); ThrowException("Unexpected Failure");

View File

@ -5,17 +5,6 @@ namespace Nefarius.Peripherals.SerialPort.Win32PInvoke;
internal class Win32Com internal class Win32Com
{ {
// 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;
//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;
@ -42,12 +31,6 @@ internal class Win32Com
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
internal static extern Boolean TransmitCommChar(IntPtr hFile, Byte cChar); 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);
[DllImport("kernel32.dll")] [DllImport("kernel32.dll")]
internal static extern Boolean GetCommModemStatus(IntPtr hFile, out UInt32 lpModemStat); internal static extern Boolean GetCommModemStatus(IntPtr hFile, out UInt32 lpModemStat);
} }