📄 commbase.cs
字号:
//Constants for dwCreationDisposition:
public const UInt32 OPEN_EXISTING = 3;
//Constants for dwDesiredAccess:
public const UInt32 GENERIC_READ = 0x80000000;
public const UInt32 GENERIC_WRITE = 0x40000000;
[DllImport("kernel32.dll")]
public static extern Boolean CloseHandle(IntPtr hObject);
[DllImport("kernel32.dll")]
public static extern Boolean GetHandleInformation(IntPtr hObject, out UInt32 lpdwFlags);
/// <summary>
/// Manipulating the communications settings.
/// </summary>
[DllImport("kernel32.dll")]
public static extern Boolean GetCommState(IntPtr hFile, ref DCB lpDCB);
[DllImport("kernel32.dll")]
public static extern Boolean GetCommTimeouts(IntPtr hFile, out COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
public static extern Boolean BuildCommDCBAndTimeouts(String lpDef, ref DCB lpDCB, ref COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
public static extern Boolean SetCommState(IntPtr hFile, [In] ref DCB lpDCB);
[DllImport("kernel32.dll")]
public static extern Boolean SetCommTimeouts(IntPtr hFile, [In] ref COMMTIMEOUTS lpCommTimeouts);
[DllImport("kernel32.dll")]
public static extern Boolean SetupComm(IntPtr hFile, UInt32 dwInQueue, UInt32 dwOutQueue);
[DllImport("kernel32.dll")]
public static extern Boolean PurgeComm(IntPtr hFile, UInt32 dwFlags);
//
// PURGE function flags.
//
public const UInt32 PURGE_TXABORT = 0x0001; // Kill the pending/current writes to the comm port.
public const UInt32 PURGE_RXABORT = 0x0002; // Kill the pending/current reads to the comm port.
public const UInt32 PURGE_TXCLEAR = 0x0004; // Kill the transmit queue if there.
public const UInt32 PURGE_RXCLEAR = 0x0008; // Kill the typeahead buffer if there.
[StructLayout( LayoutKind.Sequential )] public struct COMMTIMEOUTS
{
public Int32 ReadIntervalTimeout;
public Int32 ReadTotalTimeoutMultiplier;
public Int32 ReadTotalTimeoutConstant;
public Int32 WriteTotalTimeoutMultiplier;
public Int32 WriteTotalTimeoutConstant;
}
//
// DTR Control Flow Values.
//
public const UInt32 DTR_CONTROL_DISABLE = 0x00;
public const UInt32 DTR_CONTROL_ENABLE = 0x01;
public const UInt32 DTR_CONTROL_HANDSHAKE = 0x02;
//
// RTS Control Flow Values
//
public const UInt32 RTS_CONTROL_DISABLE = 0x00;
public const UInt32 RTS_CONTROL_ENABLE = 0x01;
public const UInt32 RTS_CONTROL_HANDSHAKE = 0x02;
public const UInt32 RTS_CONTROL_TOGGLE = 0x03;
[StructLayout( LayoutKind.Sequential )] public struct DCB
{
private Int32 DCBlength;
public Int32 BaudRate;
private UInt32 PackedValues;
public Int32 wReserved;
public Int32 XonLim;
public Int32 XoffLim;
public Byte ByteSize;
public Byte Parity;
public Byte StopBits;
public Byte XonChar;
public Byte XoffChar;
public Byte ErrorChar;
public Byte EofChar;
public Byte EvtChar;
public Int32 wReserved1;
public void init()
{
DCBlength = 28;
PackedValues = 0x8001;
}
public void init(bool parity, bool outCTS, bool outDSR, int dtr, bool inDSR, bool txc, bool xOut,
bool xIn, int rts)
{
DCBlength = 28; PackedValues = 0x8001;
if (parity) PackedValues |= 0x0002;
if (outCTS) PackedValues |= 0x0004;
if (outDSR) PackedValues |= 0x0008;
PackedValues |= (((UInt32)dtr & 0x0003) << 4);
if (inDSR) PackedValues |= 0x0040;
if (txc) PackedValues |= 0x0080;
if (xOut) PackedValues |= 0x0100;
if (xIn) PackedValues |= 0x0200;
PackedValues |= (((UInt32)rts & 0x0003) << 12);
}
public bool fBinary //: 1; /* Binary Mode (skip EOF check) */
{
get { return IsBitSet(PackedValues,0); }
set { PackedValues = SetBit(PackedValues,0,value); }
}
public bool fParity //: 1; /* Enable parity checking */
{
get { return IsBitSet(PackedValues,1); }
set { PackedValues = SetBit(PackedValues,1,value); }
}
public bool fOutxCtsFlow //:1; /* CTS handshaking on output */
{
get { return IsBitSet(PackedValues,2); }
set { PackedValues = SetBit(PackedValues,2,value); }
}
public bool fOutxDsrFlow //:1; /* DSR handshaking on output */
{
get { return IsBitSet(PackedValues,3); }
set { PackedValues = SetBit(PackedValues,3,value); }
}
public UInt32 fDtrControl //:2; /* DTR Flow control */
{
get
{
unchecked
{
return (UInt32)((PackedValues & 0x00000030) >> 4);
}
}
set
{
PackedValues |= ((value & 0x00000003) << 4);
}
}
public bool fDsrSensitivity //:1; /* DSR Sensitivity */
{
get { return IsBitSet(PackedValues,6); }
set { PackedValues = SetBit(PackedValues,6,value); }
}
public bool fTXContinueOnXoff //: 1; /* Continue TX when Xoff sent */
{
get { return IsBitSet(PackedValues,7); }
set { PackedValues = SetBit(PackedValues,7,value); }
}
public bool fOutX //: 1; /* Enable output X-ON/X-OFF */
{
get { return IsBitSet(PackedValues,8); }
set { PackedValues = SetBit(PackedValues,8,value); }
}
public bool fInX //: 1; /* Enable input X-ON/X-OFF */
{
get { return IsBitSet(PackedValues,9); }
set { PackedValues = SetBit(PackedValues,9,value); }
}
public bool fErrorChar //: 1; /* Enable Err Replacement */
{
get { return IsBitSet(PackedValues,10); }
set { PackedValues = SetBit(PackedValues,10,value); }
}
public bool fNull //: 1; /* Enable Null stripping */
{
get { return IsBitSet(PackedValues,11); }
set { PackedValues = SetBit(PackedValues,11,value); }
}
public UInt32 fRtsControl //:2; /* Rts Flow control */
{
get
{
unchecked
{
return (UInt32)((PackedValues & 0x00003000) >> 12);
}
}
set
{
PackedValues |= ((value & 0x00000003) << 12);
}
}
public bool fAbortOnError //:1; /* Abort all reads and writes on Error */
{
get { return IsBitSet(PackedValues,14); }
set { PackedValues = SetBit(PackedValues,14,value); }
}
public UInt32 fDummy2 //:17; /* Reserved */
{
get
{
unchecked
{
return (UInt32)((PackedValues & 0xffff8000) >> 15);
}
}
set
{
PackedValues |= (value << 15);
}
}
private static bool IsBitSet(UInt32 val,int bit)
{
unchecked
{
return (val & (0x00000001 << bit)) != 0;
}
}
private static UInt32 SetBit(UInt32 val, int bit, bool bSet)
{
unchecked
{
UInt32 bitVal = (UInt32)((0x00000001 << bit));
if(bSet)
{
val |= bitVal;
}
else
{
val &= ~bitVal;
}
return val;
}
}
}
/// <summary>
/// Reading and writing.
/// </summary>
[DllImport("kernel32.dll", SetLastError=true)]
public static extern Boolean WriteFile(IntPtr fFile, Byte[] lpBuffer, UInt32 nNumberOfBytesToWrite,
out UInt32 lpNumberOfBytesWritten, IntPtr lpOverlapped);
[StructLayout( LayoutKind.Sequential )] public struct OVERLAPPED
{
public UIntPtr Internal;
public UIntPtr InternalHigh;
public UInt32 Offset;
public UInt32 OffsetHigh;
public IntPtr hEvent;
}
[DllImport("kernel32.dll")]
public static extern Boolean SetCommMask(IntPtr hFile, UInt32 dwEvtMask);
// Constants for dwEvtMask:
public const UInt32 EV_RXCHAR = 0x0001;
public const UInt32 EV_RXFLAG = 0x0002;
public const UInt32 EV_TXEMPTY = 0x0004;
public const UInt32 EV_CTS = 0x0008;
public const UInt32 EV_DSR = 0x0010;
public const UInt32 EV_RLSD = 0x0020;
public const UInt32 EV_BREAK = 0x0040;
public const UInt32 EV_ERR = 0x0080;
public const UInt32 EV_RING = 0x0100;
public const UInt32 EV_PERR = 0x0200;
public const UInt32 EV_RX80FULL = 0x0400;
public const UInt32 EV_EVENT1 = 0x0800;
public const UInt32 EV_EVENT2 = 0x1000;
[DllImport("kernel32.dll", SetLastError=true)]
public static extern Boolean WaitCommEvent(IntPtr hFile, IntPtr lpEvtMask, IntPtr lpOverlapped);
[DllImport("kernel32.dll")]
public static extern Boolean CancelIo(IntPtr hFile);
[DllImport("kernel32.dll", SetLastError=true)]
public static extern Boolean ReadFile(IntPtr hFile, [Out] Byte[] lpBuffer, UInt32 nNumberOfBytesToRead,
out UInt32 nNumberOfBytesRead, IntPtr lpOverlapped);
[DllImport("kernel32.dll")]
public static extern Boolean TransmitCommChar(IntPtr hFile, Byte cChar);
/// <summary>
/// Control port functions.
/// </summary>
[DllImport("kernel32.dll")]
public static extern Boolean EscapeCommFunction(IntPtr hFile, UInt32 dwFunc);
// Constants for dwFunc:
public const UInt32 SETXOFF = 1;
public const UInt32 SETXON = 2;
public const UInt32 SETRTS = 3;
public const UInt32 CLRRTS = 4;
public const UInt32 SETDTR = 5;
public const UInt32 CLRDTR = 6;
public const UInt32 RESETDEV = 7;
public const UInt32 SETBREAK = 8;
public const UInt32 CLRBREAK = 9;
[DllImport("kernel32.dll")]
public static extern Boolean GetCommModemStatus(IntPtr hFile, out UInt32 lpModemStat);
// Constants for lpModemStat:
public const UInt32 MS_CTS_ON = 0x0010;
public const UInt32 MS_DSR_ON = 0x0020;
public const UInt32 MS_RING_ON = 0x0040;
public const UInt32 MS_RLSD_ON = 0x0080;
/// <summary>
/// Status Functions.
/// </summary>
[DllImport("kernel32.dll", SetLastError=true)]
public static extern Boolean GetOverlappedResult(IntPtr hFile, IntPtr lpOverlapped,
out UInt32 nNumberOfBytesTransferred, Boolean bWait);
[DllImport("kernel32.dll")]
public static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, IntPtr lpStat);
[DllImport("kernel32.dll")]
public static extern Boolean ClearCommError(IntPtr hFile, out UInt32 lpErrors, out COMSTAT cs);
//Constants for lpErrors:
public const UInt32 CE_RXOVER = 0x0001;
public const UInt32 CE_OVERRUN = 0x0002;
public const UInt32 CE_RXPARITY = 0x0004;
public const UInt32 CE_FRAME = 0x0008;
public const UInt32 CE_BREAK = 0x0010;
public const UInt32 CE_TXFULL = 0x0100;
public const UInt32 CE_PTO = 0x0200;
public const UInt32 CE_IOE = 0x0400;
public const UInt32 CE_DNS = 0x0800;
public const UInt32 CE_OOP = 0x1000;
public const UInt32 CE_MODE = 0x8000;
[StructLayout( LayoutKind.Sequential )] public struct COMSTAT
{
public const uint fCtsHold = 0x1;
public const uint fDsrHold = 0x2;
public const uint fRlsdHold = 0x4;
public const uint fXoffHold = 0x8;
public const uint fXoffSent = 0x10;
public const uint fEof = 0x20;
public const uint fTxim = 0x40;
public UInt32 Flags;
public UInt32 cbInQue;
public UInt32 cbOutQue;
}
[DllImport("kernel32.dll")]
public static extern Boolean GetCommProperties(IntPtr hFile, out COMMPROP cp);
[StructLayout( LayoutKind.Sequential )] public struct COMMPROP
{
public UInt16 wPacketLength;
public UInt16 wPacketVersion;
public UInt32 dwServiceMask;
public UInt32 dwReserved1;
public UInt32 dwMaxTxQueue;
public UInt32 dwMaxRxQueue;
public UInt32 dwMaxBaud;
public UInt32 dwProvSubType;
public UInt32 dwProvCapabilities;
public UInt32 dwSettableParams;
public UInt32 dwSettableBaud;
public UInt16 wSettableData;
public UInt16 wSettableStopParity;
public UInt32 dwCurrentTxQueue;
public UInt32 dwCurrentRxQueue;
public UInt32 dwProvSpec1;
public UInt32 dwProvSpec2;
public Byte wcProvChar;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -