Fixed SetCommState
This commit is contained in:
		@@ -3,9 +3,15 @@ using System.Runtime.InteropServices;
 | 
			
		||||
using System.Text;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using Windows.Win32;
 | 
			
		||||
using Windows.Win32.Devices.Communication;
 | 
			
		||||
using Windows.Win32.Foundation;
 | 
			
		||||
using Windows.Win32.Storage.FileSystem;
 | 
			
		||||
using Microsoft.Win32.SafeHandles;
 | 
			
		||||
using Nefarius.Peripherals.SerialPort.Win32PInvoke;
 | 
			
		||||
using COMMPROP = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMMPROP;
 | 
			
		||||
using COMMTIMEOUTS = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMMTIMEOUTS;
 | 
			
		||||
using COMSTAT = Nefarius.Peripherals.SerialPort.Win32PInvoke.COMSTAT;
 | 
			
		||||
using DCB = Windows.Win32.Devices.Communication.DCB;
 | 
			
		||||
 | 
			
		||||
namespace Nefarius.Peripherals.SerialPort
 | 
			
		||||
{
 | 
			
		||||
@@ -367,20 +373,20 @@ namespace Nefarius.Peripherals.SerialPort
 | 
			
		||||
            commTimeouts.WriteTotalTimeoutMultiplier = SendTimeoutMultiplier;
 | 
			
		||||
            portDcb.Init(Parity is Parity.Odd or Parity.Even, TxFlowCts, TxFlowDsr,
 | 
			
		||||
                (int)UseDtr, RxGateDsr, !TxWhenRxXoff, TxFlowX, RxFlowX, (int)UseRts);
 | 
			
		||||
            portDcb.BaudRate = BaudRate;
 | 
			
		||||
            portDcb.BaudRate = (uint)BaudRate;
 | 
			
		||||
            portDcb.ByteSize = (byte)DataBits;
 | 
			
		||||
            portDcb.Parity = (byte) Parity;
 | 
			
		||||
            portDcb.StopBits = (byte) StopBits;
 | 
			
		||||
            portDcb.XoffChar = (byte) XoffChar;
 | 
			
		||||
            portDcb.XonChar = (byte) XonChar;
 | 
			
		||||
            portDcb.XoffLim = (short) RxHighWater;
 | 
			
		||||
            portDcb.XonLim = (short) RxLowWater;
 | 
			
		||||
            portDcb.Parity = (DCB_PARITY)Parity;
 | 
			
		||||
            portDcb.StopBits = (DCB_STOP_BITS)StopBits;
 | 
			
		||||
            portDcb.XoffChar = (CHAR)(byte)XoffChar;
 | 
			
		||||
            portDcb.XonChar = (CHAR)(byte)XonChar;
 | 
			
		||||
            portDcb.XoffLim = (ushort)RxHighWater;
 | 
			
		||||
            portDcb.XonLim = (ushort)RxLowWater;
 | 
			
		||||
 | 
			
		||||
            if (RxQueue != 0 || TxQueue != 0)
 | 
			
		||||
                if (!Win32Com.SetupComm(_hPort.DangerousGetHandle(), (uint)RxQueue, (uint)TxQueue))
 | 
			
		||||
                    ThrowException("Bad queue settings");
 | 
			
		||||
 | 
			
		||||
            if (!Win32Com.SetCommState(_hPort.DangerousGetHandle(), ref portDcb)) 
 | 
			
		||||
            if (!PInvoke.SetCommState(_hPort, portDcb))
 | 
			
		||||
                ThrowException("Bad com settings");
 | 
			
		||||
 | 
			
		||||
            if (!Win32Com.SetCommTimeouts(_hPort.DangerousGetHandle(), ref commTimeouts))
 | 
			
		||||
@@ -421,7 +427,9 @@ namespace Nefarius.Peripherals.SerialPort
 | 
			
		||||
            // TODO: utilize Task Parallel Library here
 | 
			
		||||
            _rxThread = new Thread(ReceiveThread)
 | 
			
		||||
            {
 | 
			
		||||
                Name = "CommBaseRx", Priority = ThreadPriority.AboveNormal, IsBackground = true
 | 
			
		||||
                Name = "CommBaseRx",
 | 
			
		||||
                Priority = ThreadPriority.AboveNormal,
 | 
			
		||||
                IsBackground = true
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            _rxThread.Start();
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ 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,
 | 
			
		||||
    public static void Init(this ref DCB dcb, bool parity, bool outCts, bool outDsr, int dtr, bool inDsr, bool txc,
 | 
			
		||||
        bool xOut,
 | 
			
		||||
        bool xIn, int rts)
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user