⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 win32status.cs

📁 常见的串口通信程序
💻 CS
字号:
namespace SerialPorts
{
    using System;
    using System.Runtime.InteropServices;
    using System.Text;

    internal class Win32Status
    {
        // Methods
        protected internal Win32Status()
        {
            this.ready = 0;
            this.cs = new COMMSTAT();
            this.state = new COMMHOLD();
            this.error = new COMMERRS();
        }

        protected internal bool Clear(IntPtr handle)
        {
            if (!Win32Status.ClearCommError(handle, out this.error.status, out this.cs))
            {
                this.SetFault("ClearCommError(Clear)");
                return false;
            }
            this.error.overflowError = (this.error.status & 1) != 0;
            this.error.overrunError = (this.error.status & 2) != 0;
            this.error.parityError = (this.error.status & 4) != 0;
            this.error.framingError = (this.error.status & 8) != 0;
            this.error.breakCondition = (this.error.status & 0x10) != 0;
            this.error.txBufFullError = (this.error.status & 0x100) != 0;
            this.error.parallelTmoutError = (this.error.status & 0x200) != 0;
            this.error.deviceIOError = (this.error.status & 0x400) != 0;
            this.error.notSelectedError = (this.error.status & 0x800) != 0;
            this.error.outOfPaperError = (this.error.status & 0x1000) != 0;
            this.error.modeHandleError = (this.error.status & 0x8000) != 0;
            this.state.ctsHold = (this.cs.bitfield & 1) != 0;
            this.state.dsrHold = (this.cs.bitfield & 2) != 0;
            this.state.rlsdHold = (this.cs.bitfield & 4) != 0;
            this.state.xoffHold = (this.cs.bitfield & 8) != 0;
            this.state.xoffSent = (this.cs.bitfield & 0x10) != 0;
            this.state.eof = (this.cs.bitfield & 0x20) != 0;
            this.state.trim = (this.cs.bitfield & 0x40) != 0;
            this.ready = this.cs.cbInQue;
            return true;
        }

        [DllImport("kernel32.dll")]
        private static extern bool ClearCommError(IntPtr hFile, out uint lpErrors, out COMMSTAT cs);

        [DllImport("kernel32.dll")]
        private static extern bool ClearCommError(IntPtr hFile, out uint lpErrors, IntPtr lpStat);

        internal string QueueString()
        {
            StringBuilder builder1 = new StringBuilder("The RX queue ", 80);
            if (this.cs.cbInQue == 0)
            {
                builder1.Append("is empty.");
            }
            else if (this.cs.cbInQue == 1)
            {
                builder1.Append("contains 1 byte.");
            }
            else
            {
                builder1.Append("contains ");
                builder1.Append(this.cs.cbInQue.ToString());
                builder1.Append(" bytes.");
            }
            builder1.Append("\r\n");
            builder1.Append("The TX queue ");
            if (this.cs.cbOutQue == 0)
            {
                builder1.Append("is empty.");
            }
            else if (this.cs.cbOutQue == 1)
            {
                builder1.Append("contains 1 byte.");
            }
            else
            {
                builder1.Append("contains ");
                builder1.Append(this.cs.cbOutQue.ToString());
                builder1.Append(" bytes.");
            }
            builder1.Append("\r\n");
            builder1.Append("The immediate buffer is ");
            if (this.state.trim)
            {
                builder1.Append("full.");
            }
            else
            {
                builder1.Append("empty.");
            }
            builder1.Append("\r\n");
            return builder1.ToString();
        }

        internal bool Reset(IntPtr handle)
        {
            uint num1;
            if (!Win32Status.ClearCommError(handle, out num1, IntPtr.Zero))
            {
                this.SetFault("ClearCommError(Reset)");
                return false;
            }
            if ((num1 > 0) && (num1 != 0x10))
            {
                this.fault = "COMM Error(s):";
                if ((num1 & 1) != 0)
                {
                    this.fault = this.fault + "\tRX Overflow\r\n";
                }
                if ((num1 & 2) != 0)
                {
                    this.fault = this.fault + "\tOverrun\r\n";
                }
                if ((num1 & 4) != 0)
                {
                    this.fault = this.fault + "\tParity\r\n";
                }
                if ((num1 & 8) != 0)
                {
                    this.fault = this.fault + "\tFraming\r\n";
                }
                if ((num1 & 0x10) != 0)
                {
                    this.fault = this.fault + "\tBreak\r\n";
                }
                if ((num1 & 0x100) != 0)
                {
                    this.fault = this.fault + "\tTX Overflow\r\n";
                }
                if ((num1 & 0x200) != 0)
                {
                    this.fault = this.fault + "\tLPT Tmout\r\n";
                }
                if ((num1 & 0x400) != 0)
                {
                    this.fault = this.fault + "\tIO Error\r\n";
                }
                if ((num1 & 0x800) != 0)
                {
                    this.fault = this.fault + "\tDevice Not Selected\r\n";
                }
                if ((num1 & 0x1000) != 0)
                {
                    this.fault = this.fault + "\tOut-of-Paper\r\n";
                }
                if ((num1 & 0x8000) != 0)
                {
                    this.fault = this.fault + "\tMode/Handle\r\n";
                }
                return false;
            }
            return true;
        }

        private void SetFault(string who)
        {
            int num1 = Marshal.GetLastWin32Error();
            this.fault = who + "Failed. System Returned Error Code: " + num1.ToString();
        }

        internal string StateString()
        {
            if (this.cs.cbOutQue <= 0)
            {
                return "";
            }
            StringBuilder builder1 = new StringBuilder("The TX queue is ", 80);
            if ((this.state.ctsHold || this.state.dsrHold) || ((this.state.rlsdHold || this.state.xoffHold) || this.state.xoffSent))
            {
                builder1.Append("holding on ");
                if (this.state.ctsHold)
                {
                    builder1.Append("CTS ");
                }
                if (this.state.dsrHold)
                {
                    builder1.Append("DSR ");
                }
                if (this.state.rlsdHold)
                {
                    builder1.Append("RLSD ");
                }
                if (this.state.xoffHold)
                {
                    builder1.Append("Rx XOff ");
                }
                if (this.state.xoffSent)
                {
                    builder1.Append("Tx XOff ");
                }
            }
            else
            {
                builder1.Append("sending data.");
            }
            builder1.Append("\r\n");
            return builder1.ToString();
        }


        // Properties
        internal bool CtsHold
        {
            get
            {
                return this.state.ctsHold;
            }
        }

        internal bool DsrHold
        {
            get
            {
                return this.state.dsrHold;
            }
        }

        internal string Fault
        {
            get
            {
                return this.fault;
            }
        }

        internal bool ImmediateWaiting
        {
            get
            {
                return this.state.trim;
            }
        }

        internal uint Ready
        {
            get
            {
                return this.ready;
            }
        }

        internal bool RlsdHold
        {
            get
            {
                return this.state.rlsdHold;
            }
        }

        internal uint RxQueCount
        {
            get
            {
                return this.cs.cbInQue;
            }
        }

        internal uint Status
        {
            get
            {
                return this.error.status;
            }
        }

        internal uint TxQueCount
        {
            get
            {
                return this.cs.cbOutQue;
            }
        }

        internal bool XoffHold
        {
            get
            {
                return this.state.xoffHold;
            }
        }

        internal bool XoffSent
        {
            get
            {
                return this.state.xoffSent;
            }
        }


        // Fields
        internal const uint CE_BREAK = 0x10;
        internal const uint CE_DNS = 0x800;
        internal const uint CE_FRAME = 8;
        internal const uint CE_IOE = 0x400;
        internal const uint CE_MODE = 0x8000;
        internal const uint CE_OOP = 0x1000;
        internal const uint CE_OVERRUN = 2;
        internal const uint CE_PTO = 0x200;
        internal const uint CE_RXOVER = 1;
        internal const uint CE_RXPARITY = 4;
        internal const uint CE_TXFULL = 0x100;
        private COMMSTAT cs;
        internal const uint CTS_HOLD_BIT = 1;
        internal const uint DSR_HOLD_BIT = 2;
        internal const uint EOF_BIT = 0x20;
        private COMMERRS error;
        private string fault;
        private uint ready;
        internal const uint RLSD_HOLD_BIT = 4;
        private COMMHOLD state;
        internal const uint TRIM_BIT = 0x40;
        internal const uint XOFF_HOLD_BIT = 8;
        internal const uint XOFF_SENT_BIT = 0x10;

        // Nested Types
        [StructLayout(LayoutKind.Sequential)]
        internal protected struct COMMERRS
        {
            internal bool overflowError;
            internal bool overrunError;
            internal bool parityError;
            internal bool framingError;
            internal bool breakCondition;
            internal bool txBufFullError;
            internal bool parallelTmoutError;
            internal bool deviceIOError;
            internal bool notSelectedError;
            internal bool outOfPaperError;
            internal bool modeHandleError;
            internal uint status;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal protected struct COMMHOLD
        {
            internal bool ctsHold;
            internal bool dsrHold;
            internal bool rlsdHold;
            internal bool xoffHold;
            internal bool xoffSent;
            internal bool eof;
            internal bool trim;
        }

        [StructLayout(LayoutKind.Sequential)]
        internal protected struct COMMSTAT
        {
            internal uint bitfield;
            internal uint cbInQue;
            internal uint cbOutQue;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -