private bool SetDCBState(IntPtr hComDevice) { if ( hComDevice.ToInt32() <= 0) { return false; } try { DCB lpDCB = new DCB(); NativeMethods.GetCommState(hComDevice, ref lpDCB); if (m_BaudRate != SerialBaudRate.brCustom) { lpDCB.BaudRate = (int)m_BaudRate; } else { lpDCB.BaudRate = m_CustomBaudRate; } lpDCB.ByteSize = (byte)m_DataWidth; lpDCB.StopBits = (byte)m_StopBits; lpDCB.Parity = (byte)m_ParityBits; lpDCB.Flags = 1; if (m_EnableDTROnOpen) { lpDCB.Flags |= 0x10; } if (m_EnableRTSOnOpen) { lpDCB.Flags |= 0x1000; } switch (m_HwFlowControl) { case SerialHardwareFlowControl.hfRTS: lpDCB.Flags |= 0x1000; break; case SerialHardwareFlowControl.hfRTSCTS: lpDCB.Flags = (lpDCB.Flags | 4) | 0x2000; break; } switch (m_SwFlowControl) { case SerialSoftwareFlowControl.sfXONXOFF: lpDCB.Flags = (lpDCB.Flags | 0x100) | 0x200; break; } lpDCB.XonChar = '\x0011'; lpDCB.XoffChar = '\x0013'; return (NativeMethods.SetCommState(hComDevice, ref lpDCB) != 0); } catch { return false; } }
internal static extern int SetCommState(IntPtr hCommDev, ref DCB lpDCB);