win32dcb.cs
来自「常见的串口通信程序」· CS 代码 · 共 301 行
CS
301 行
namespace SerialPorts
{
using System;
using System.Runtime.InteropServices;
internal class Win32DCB
{
// Methods
internal Win32DCB(SerialCnfg cfg)
{
this.dcb = new DCB();
this.dcb.dcbLength = Marshal.SizeOf(this.dcb);
this.dcb.baudRate = (int) cfg.BaudRate;
this.dcb.bitfield = 0x8001;
if ((cfg.Parity == Parity.Odd) || (cfg.Parity == Parity.Even))
{
this.dcb.bitfield |= 2;
}
if (cfg.TxFlowCTS)
{
this.dcb.bitfield |= 4;
}
if (cfg.TxFlowDSR)
{
this.dcb.bitfield |= 8;
}
this.dcb.bitfield |= (((byte) (cfg.DtrControl & PinState.Toggle)) << 4);
if (cfg.RxDSRsense)
{
this.dcb.bitfield |= 0x40;
}
if (cfg.TxContinue)
{
this.dcb.bitfield |= 0x80;
}
if (cfg.TxFlowXoff)
{
this.dcb.bitfield |= 0x100;
}
if (cfg.RxFlowXoff)
{
this.dcb.bitfield |= 0x200;
}
this.dcb.bitfield |= (((byte) (cfg.RtsControl & PinState.Toggle)) << 12);
this.dcb.byteSize = (byte) cfg.DataBits;
this.dcb.prtyByte = (byte) cfg.Parity;
this.dcb.stopBits = (byte) cfg.StopBits;
this.dcb.xoffChar = cfg.XoffChar;
this.dcb.xonChar = cfg.XonChar;
}
internal bool Get(IntPtr handle)
{
if (!Win32DCB.GetCommState(handle, ref this.dcb))
{
this.SetFault("GetCommState()");
return false;
}
return true;
}
[DllImport("kernel32.dll")]
private static extern bool GetCommState(IntPtr hFile, ref DCB lpDCB);
internal void Limits(SerialCnfg cfg, uint rxQueLen)
{
if (cfg.XonLimit == 0)
{
this.dcb.xonLim = (rxQueLen > 0) ? ((short) (rxQueLen / 10)) : ((short) 8);
}
else
{
this.dcb.xonLim = cfg.XonLimit;
}
if (cfg.XoffLimit == 0)
{
this.dcb.xoffLim = (rxQueLen > 0) ? ((short) (rxQueLen / 10)) : ((short) 8);
}
else
{
this.dcb.xoffLim = cfg.XoffLimit;
}
}
internal bool Set(IntPtr handle)
{
if (!Win32DCB.SetCommState(handle, ref this.dcb))
{
this.SetFault("SetCommState()");
return false;
}
return true;
}
[DllImport("kernel32.dll")]
private static extern bool SetCommState(IntPtr hFile, [In] ref DCB lpDCB);
private void SetFault(string who)
{
int num1 = Marshal.GetLastWin32Error();
this.fault = who + "Failed. System Returned Error Code: " + num1.ToString();
}
// Properties
internal int BaudRate
{
get
{
return this.dcb.baudRate;
}
set
{
this.dcb.baudRate = value;
}
}
internal int Bitfield
{
get
{
return this.dcb.bitfield;
}
set
{
this.dcb.bitfield = value;
}
}
internal byte ByteSize
{
get
{
return this.dcb.byteSize;
}
set
{
this.dcb.byteSize = value;
}
}
internal int DcbLength
{
get
{
return this.dcb.dcbLength;
}
}
internal byte EofChar
{
get
{
return this.dcb.eofChar;
}
set
{
this.dcb.eofChar = value;
}
}
internal byte ErrorChar
{
get
{
return this.dcb.errorChar;
}
set
{
this.dcb.errorChar = value;
}
}
internal byte EventChar
{
get
{
return this.dcb.evtChar;
}
set
{
this.dcb.evtChar = value;
}
}
internal string Fault
{
get
{
return this.fault;
}
}
internal byte ParityBits
{
get
{
return this.dcb.prtyByte;
}
set
{
this.dcb.prtyByte = value;
}
}
internal byte StopBits
{
get
{
return this.dcb.stopBits;
}
set
{
this.dcb.stopBits = value;
}
}
internal byte XoffChar
{
get
{
return this.dcb.xoffChar;
}
set
{
this.dcb.xoffChar = value;
}
}
internal short XoffLimit
{
get
{
return this.dcb.xoffLim;
}
set
{
this.dcb.xoffLim = value;
}
}
internal byte XonChar
{
get
{
return this.dcb.xonChar;
}
set
{
this.dcb.xonChar = value;
}
}
internal short XonLimit
{
get
{
return this.dcb.xonLim;
}
set
{
this.dcb.xonLim = value;
}
}
// Fields
private DCB dcb;
internal const uint DTR_CONTROL_DISABLE = 0;
internal const uint DTR_CONTROL_ENABLE = 1;
internal const uint DTR_CONTROL_HANDSHAKE = 2;
private string fault;
internal const uint RTS_CONTROL_DISABLE = 0;
internal const uint RTS_CONTROL_ENABLE = 1;
internal const uint RTS_CONTROL_HANDSHAKE = 2;
internal const uint RTS_CONTROL_TOGGLE = 3;
// Nested Types
[StructLayout(LayoutKind.Sequential)]
internal struct DCB
{
internal int dcbLength;
internal int baudRate;
internal int bitfield;
internal short wReserved;
internal short xonLim;
internal short xoffLim;
internal byte byteSize;
internal byte prtyByte;
internal byte stopBits;
internal byte xonChar;
internal byte xoffChar;
internal byte errorChar;
internal byte eofChar;
internal byte evtChar;
internal short wReserved1;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?