Ported more code

This commit is contained in:
Benjamin Höglinger-Stelzer 2022-10-03 19:03:33 +02:00
parent 4617c69206
commit 2a23da6db5
3 changed files with 4 additions and 49 deletions

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 COMMTIMEOUTS = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMMTIMEOUTS;
using COMSTAT = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMSTAT; using COMSTAT = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMSTAT;
using DCB = Windows.Win32.Devices.Communication.DCB; using DCB = Windows.Win32.Devices.Communication.DCB;
@ -369,8 +368,8 @@ namespace Nefarius.Peripherals.SerialPort
commTimeouts.ReadIntervalTimeout = 0; commTimeouts.ReadIntervalTimeout = 0;
commTimeouts.ReadTotalTimeoutConstant = 0; commTimeouts.ReadTotalTimeoutConstant = 0;
commTimeouts.ReadTotalTimeoutMultiplier = 0; commTimeouts.ReadTotalTimeoutMultiplier = 0;
commTimeouts.WriteTotalTimeoutConstant = SendTimeoutConstant; commTimeouts.WriteTotalTimeoutConstant = (uint)SendTimeoutConstant;
commTimeouts.WriteTotalTimeoutMultiplier = SendTimeoutMultiplier; commTimeouts.WriteTotalTimeoutMultiplier = (uint)SendTimeoutMultiplier;
portDcb.Init(Parity is Parity.Odd or Parity.Even, TxFlowCts, TxFlowDsr, portDcb.Init(Parity is Parity.Odd or Parity.Even, TxFlowCts, TxFlowDsr,
(int)UseDtr, RxGateDsr, !TxWhenRxXoff, TxFlowX, RxFlowX, (int)UseRts); (int)UseDtr, RxGateDsr, !TxWhenRxXoff, TxFlowX, RxFlowX, (int)UseRts);
portDcb.BaudRate = (uint)BaudRate; portDcb.BaudRate = (uint)BaudRate;
@ -383,13 +382,13 @@ namespace Nefarius.Peripherals.SerialPort
portDcb.XonLim = (ushort)RxLowWater; portDcb.XonLim = (ushort)RxLowWater;
if (RxQueue != 0 || TxQueue != 0) if (RxQueue != 0 || TxQueue != 0)
if (!Win32Com.SetupComm(_hPort.DangerousGetHandle(), (uint)RxQueue, (uint)TxQueue)) if (!PInvoke.SetupComm(_hPort, (uint)RxQueue, (uint)TxQueue))
ThrowException("Bad queue settings"); ThrowException("Bad queue settings");
if (!PInvoke.SetCommState(_hPort, portDcb)) if (!PInvoke.SetCommState(_hPort, portDcb))
ThrowException("Bad com settings"); ThrowException("Bad com settings");
if (!Win32Com.SetCommTimeouts(_hPort.DangerousGetHandle(), ref commTimeouts)) if (!PInvoke.SetCommTimeouts(_hPort, commTimeouts))
ThrowException("Bad timeout settings"); ThrowException("Bad timeout settings");
_stateBrk = 0; _stateBrk = 0;

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

@ -5,33 +5,10 @@ namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
{ {
internal class Win32Com 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: //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_ACCESS_DENIED = 5;
internal const UInt32 ERROR_IO_PENDING = 997; 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")] [DllImport("kernel32.dll")]
internal static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags); internal static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags);
@ -40,12 +17,6 @@ namespace Nefarius.Peripherals.SerialPort.Win32PInvoke
/// Manipulating the communications settings. /// Manipulating the communications settings.
/// </summary> /// </summary>
[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> /// <summary>
/// Reading and writing. /// Reading and writing.
/// </summary> /// </summary>