private void InitializeDcb() { var dcb = new Dcb(); GetCommStateNative(ref dcb); dcb.Flags &= ~(1u << DcbFlagAbortOnError); SetCommStateNative(ref dcb); }
public void Open(string portName, uint baudrate) { // Check if port can be found bool isValid = SerialPort.GetPortNames() .Any(x => String.Compare(x, portName, StringComparison.OrdinalIgnoreCase) == 0); if (!isValid) { throw new IOException(string.Format("{0} port was not found", portName)); } string port = @"\\.\" + portName; Handle = CreateFile(port, GenericRead | GenericWrite, 0, IntPtr.Zero, OpenExisting, 0, IntPtr.Zero); if (Handle.IsInvalid) { throw new IOException(string.Format("{0} port is already open", portName)); } var dcb = new Dcb(); // first get the current dcb structure setup if (GetCommState(Handle, ref dcb) == false) { throw new IOException(string.Format("GetCommState error {0}", portName)); } dcb.BaudRate = baudrate; dcb.ByteSize = 8; dcb.Flags = 129; dcb.XoffChar = 0; dcb.XonChar = 0; /* Apply the settings */ if (SetCommState(Handle, ref dcb) == false) { throw new IOException(string.Format("SetCommState error {0}", portName)); } /* Set DTR, some boards needs a DTR = 1 level */ if (EscapeCommFunction(Handle, Setdtr) == false) { throw new IOException(string.Format("EscapeCommFunction error {0}", portName)); } // Write default timeouts var cto = new Commtimeouts { ReadTotalTimeoutConstant = 500, ReadTotalTimeoutMultiplier = 0, ReadIntervalTimeout = 10, WriteTotalTimeoutConstant = WriteTimeout, WriteTotalTimeoutMultiplier = 0 }; if (SetCommTimeouts(Handle, ref cto) == false) { throw new IOException(string.Format("SetCommTimeouts error {0}", portName)); } // Create filestream _fileStream = new FileStream(Handle, FileAccess.ReadWrite, 32, false); }
private void InitializeDcb() { Dcb dcb = new Dcb(); GetCommStateNative(ref dcb); log.Info("before dcb flags: "+dcb.Flags); dcb.Flags &= ~(1u << DcbFlagAbortOnError); log.Info("after dcb flags: " + dcb.Flags); SetCommStateNative(ref dcb); }
private void InitializeDcb(uint baudrate) { Dcb dcb = new Dcb(); GetCommStateNative(ref dcb); dcb.Flags = 1; // only binary mode dcb.BaudRate = baudrate; dcb.Parity = 0; dcb.StopBits = 0; dcb.ByteSize = 8; SetCommStateNative(ref dcb); }
private void SetCommStateNative(ref Dcb lpDcb) { int commErrors = 0; Comstat comStat = new Comstat(); for (int i = 0; i < CommStateRetries; i++) { if (!NativeMethods.ClearCommError(m_Handle, ref commErrors, ref comStat)) { WinIoError(); } if (NativeMethods.SetCommState(m_Handle, ref lpDcb)) { break; } if (i == CommStateRetries - 1) { WinIoError(); } } }
private void refreshPortConfig() { Dcb dcb = new Dcb(); dcb.DCBLength = (uint)Marshal.SizeOf(typeof(Dcb)); bool OK; OK = GetCommState(handle, ref dcb); if (!OK) { throw new IOException("Win32 API GetCommState() failed", Marshal.GetLastWin32Error()); } dcb.BaudRate = (uint)baudRate; dcb.ByteSize = dataBits; dcb.Parity = parity; dcb.StopBits = stopBits; dcb.CheckParity = false; dcb.OutxCtsFlow = false; dcb.OutxDsrFlow = false; dcb.DtrControl = 0x00; dcb.DsrSensitivity = false; dcb.OutX = false; dcb.InX = false; dcb.ReplaceErrorChar = true; dcb.Null = false; dcb.RtsControl = 0x00; dcb.AbortOnError = false; OK = SetCommState(handle, ref dcb); if (!OK) { throw new IOException("Win32 API SetCommState() failed", Marshal.GetLastWin32Error()); } OK = SetupComm(handle, driverInputBufferSize_bytes, driverOutputBufferSize_bytes); if (!OK) { throw new IOException("Win32 API SetupComm() failed", Marshal.GetLastWin32Error()); } }
private void SetCommStateNative(ref Dcb lpDcb) { var commErrors = 0; var comStat = new Comstat(); for (var i = 0; i < CommStateRetries; i++) { if (!ClearCommError(m_Handle, ref commErrors, ref comStat)) { WinIoError(); } if (SetCommState(m_Handle, ref lpDcb)) { break; } if (i == CommStateRetries - 1) { WinIoError(); } } }
private void GetCommStateNative(ref Dcb lpDcb) { int commErrors = 0; Comstat comStat = new Comstat(); for (int i = 0; i < CommStateRetries; i++) { if (!ClearCommError(_handle, ref commErrors, ref comStat)) { WinIoError(); } if (GetCommState(_handle, ref lpDcb)) { break; } if (i == CommStateRetries - 1) { WinIoError(); } } }
public static void Initialize(string portName) { const int dwFlagsAndAttributes = 0x40000000; const int dwAccess = unchecked ((int)0xC0000000); if ((portName == null) || !portName.StartsWith("COM", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(Messages.InvalidSerialPort, nameof(portName)); } SafeFileHandle fileHandle = CreateFile(@"\\.\" + portName, dwAccess, 0, IntPtr.Zero, 3, dwFlagsAndAttributes, IntPtr.Zero); if (fileHandle.IsInvalid) { ThrowIoException(); } try { int fileType = GetFileType(fileHandle); if ((fileType != 2) && (fileType != 0)) { throw new ArgumentException(Messages.InvalidSerialPort, nameof(portName)); } var dcb = new Dcb(); MarshalCommState(fileHandle, () => GetCommState(fileHandle, ref dcb)); dcb.Flags &= ~(1u << DcbFlagAbortOnError); MarshalCommState(fileHandle, () => SetCommState(fileHandle, ref dcb)); } finally { fileHandle.Close(); } }
internal static extern bool SetCommState(SafeFileHandle hFile, ref Dcb lpDcb);
private void InitializeDcb() { Dcb dcb = new Dcb(); GetCommStateNative(ref dcb); dcb.Flags &= ~(1u << DcbFlagAbortOnError); SetCommStateNative(ref dcb); }
/// <summary> /// Configures the serial device based on the connection parameters pased in by the user. /// </summary> /// <returns>Whether or not the operation succeeded</returns> private bool ConfigureSerialPort() { Dcb serialConfig = new Dcb(); if (GetCommState(_pHandle, ref serialConfig)) { // setup the DCB struct with the serial settings we need serialConfig.BaudRate = (uint)_iBaudRate; serialConfig.ByteSize = _byteSize; serialConfig.fBinary = 1; // must be true serialConfig.fDtrControl = 1; // DTR_CONTROL_ENABLE "Enables the DTR line when the device is opened and leaves it on." serialConfig.fAbortOnError = 0; // false serialConfig.fTXContinueOnXoff = 0; // false serialConfig.fParity = 1; // true so that the Parity member is looked at switch (_parity) { case Parity.Even: serialConfig.Parity = 2; break; case Parity.Mark: serialConfig.Parity = 3; break; case Parity.Odd: serialConfig.Parity = 1; break; case Parity.Space: serialConfig.Parity = 4; break; // ReSharper disable once RedundantCaseLabel case Parity.None: default: serialConfig.Parity = 0; break; } switch (_stopBits) { case StopBits.One: serialConfig.StopBits = 0; break; case StopBits.OnePointFive: serialConfig.StopBits = 1; break; case StopBits.Two: serialConfig.StopBits = 2; break; // ReSharper disable once RedundantCaseLabel case StopBits.None: default: throw new ArgumentException("stopBits cannot be StopBits.None"); } if (SetCommState(_pHandle, ref serialConfig)) { // set the serial connection timeouts CommTimeout timeouts = new CommTimeout(); timeouts.ReadIntervalTimeout = 1; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.ReadTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; if (SetCommTimeouts(_pHandle, ref timeouts)) { return(true); } return(false); } return(false); } return(false); }
private static extern bool SetCommState(SafeFileHandle hFile, ref Dcb lpDcb);
static extern bool GetCommState(IntPtr hFile, ref Dcb lpDcb);
private void Initialize() { _ReadSafeFileHandle = ApiService.CreateReadConnection(DeviceId, FileAccessRights.GenericRead | FileAccessRights.GenericWrite); if (_ReadSafeFileHandle.IsInvalid) { return; } var dcb = new Dcb(); var isSuccess = ApiService.AGetCommState(_ReadSafeFileHandle, ref dcb); WindowsDeviceBase.HandleError(isSuccess, Messages.ErrorCouldNotGetCommState); dcb.ByteSize = _ByteSize; dcb.fDtrControl = 1; dcb.BaudRate = (uint)_BaudRate; dcb.fBinary = 1; dcb.fTXContinueOnXoff = 0; dcb.fAbortOnError = 0; dcb.fParity = 1; switch (_Parity) { case Parity.Even: dcb.Parity = 2; break; case Parity.Mark: dcb.Parity = 3; break; case Parity.Odd: dcb.Parity = 1; break; case Parity.Space: dcb.Parity = 4; break; default: dcb.Parity = 0; break; } switch (_StopBits) { case StopBits.One: dcb.StopBits = 0; break; case StopBits.OnePointFive: dcb.StopBits = 1; break; case StopBits.Two: dcb.StopBits = 2; break; default: throw new ArgumentException(Messages.ErrorMessageStopBitsMustBeSpecified); } isSuccess = ApiService.ASetCommState(_ReadSafeFileHandle, ref dcb); WindowsDeviceBase.HandleError(isSuccess, Messages.ErrorCouldNotSetCommState); var timeouts = new CommTimeouts { WriteTotalTimeoutConstant = 0, ReadIntervalTimeout = 1, WriteTotalTimeoutMultiplier = 0, ReadTotalTimeoutMultiplier = 0, ReadTotalTimeoutConstant = 0 }; isSuccess = ApiService.ASetCommTimeouts(_ReadSafeFileHandle, ref timeouts); WindowsDeviceBase.HandleError(isSuccess, Messages.ErrorCouldNotSetCommTimeout); }
private static extern bool GetCommState(SafeFileHandle hFile, ref Dcb lpDcb);
private static extern bool SetCommState( SafeFileHandle hFile, // handle to communications device ref Dcb lpDcb // device-control block );
internal static extern bool GetCommState(SafeFileHandle hFile, ref Dcb lpDcb);
private static extern bool SetCommState(SafeFileHandle hFile, [In] ref Dcb lpDCB);
static extern bool SetCommState(IntPtr hFile, [In] ref Dcb lpDcb);