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

📄 win32escape.cs

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

    internal class Win32Escape
    {
        // Methods
        internal Win32Escape(IntPtr handle)
        {
            this.handle = handle;
            this.escState = 0;
        }

        [DllImport("kernel32.dll")]
        private static extern bool EscapeCommFunction(IntPtr hFile, uint dwFunc);
		internal bool ExtFunc(int code)
		{
			return ExtFunc((uint)code);
		}

        internal bool ExtFunc(uint code)
        {
            if (((((code != 1) && (code != 2)) || ((this.escState & 3) != 3)) && (((code != 3) && (code != 4)) || ((this.escState & 12) != 12))) && ((((code != 5) && (code != 6)) || ((this.escState & 0x30) != 0x30)) && (((code != 8) && (code != 9)) || ((this.escState & 0xc0) != 0xc0))))
            {
                if (!Win32Escape.EscapeCommFunction(this.handle, code))
                {
                    int num1 = Marshal.GetLastWin32Error();
                    this.fault = "EscapeCommFunction() Failed. System Returned Error Code: " + num1.ToString();
                    return false;
                }
                switch (code)
                {
                    case 1:
                    {
                        this.escState = (byte) ((this.escState & 0xfc) | 1);
                        goto Label_0188;
                    }
                    case 2:
                    {
                        this.escState = (byte) ((this.escState & 0xfc) | 2);
                        goto Label_0188;
                    }
                    case 3:
                    {
                        this.escState = (byte) ((this.escState & 0xf3) | 4);
                        goto Label_0188;
                    }
                    case 4:
                    {
                        this.escState = (byte) ((this.escState & 0xf3) | 8);
                        goto Label_0188;
                    }
                    case 5:
                    {
                        this.escState = (byte) ((this.escState & 0xcf) | 0x10);
                        goto Label_0188;
                    }
                    case 6:
                    {
                        this.escState = (byte) ((this.escState & 0xcf) | 0x20);
                        goto Label_0188;
                    }
                    case 8:
                    {
                        this.escState = (byte) ((this.escState & 0x3f) | 0x40);
                        goto Label_0188;
                    }
                    case 9:
                    {
                        this.escState = (byte) ((this.escState & 0x3f) | 0x80);
                        goto Label_0188;
                    }
                }
            }
            return false;
        Label_0188:
            return true;
        }


        // Properties
        public bool Break
        {
            get
            {
                return ((this.escState & 0xc0) == 0x40);
            }
            set
            {
                this.ExtFunc(value ? 8 : 9);
            }
        }

        public bool BreakAvailable
        {
            get
            {
                return ((this.escState & 0xc0) != 0xc0);
            }
            set
            {
                if (value)
                {
                    this.escState = (byte) (this.escState & 0x3f);
                }
                else
                {
                    this.escState = (byte) (this.escState | 0xc0);
                }
            }
        }

        public bool DTR
        {
            get
            {
                return ((this.escState & 0x30) == 0x10);
            }
            set
            {
                this.ExtFunc(value ? 5 : 6);
            }
        }

        public bool DTRavailable
        {
            get
            {
                return ((this.escState & 0x30) != 0x30);
            }
            set
            {
                if (value)
                {
                    this.escState = (byte) (this.escState & 0xcf);
                }
                else
                {
                    this.escState = (byte) (this.escState | 0x30);
                }
            }
        }

        public byte EscState
        {
            get
            {
                return this.escState;
            }
            set
            {
                this.escState = value;
            }
        }

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

        public bool RTS
        {
            get
            {
                return ((this.escState & 12) == 4);
            }
            set
            {
                this.ExtFunc(value ? 3 : 4);
            }
        }

        public bool RTSavailable
        {
            get
            {
                return ((this.escState & 12) != 12);
            }
            set
            {
                if (value)
                {
                    this.escState = (byte) (this.escState & 0xf3);
                }
                else
                {
                    this.escState = (byte) (this.escState | 12);
                }
            }
        }

        public bool XOFF
        {
            get
            {
                return ((this.escState & 3) == 1);
            }
            set
            {
                this.ExtFunc(value ? 1 : 2);
            }
        }

        public bool XOFFavailable
        {
            get
            {
                return ((this.escState & 3) != 3);
            }
            set
            {
                if (value)
                {
                    this.escState = (byte) (this.escState & 0xfc);
                }
                else
                {
                    this.escState = (byte) (this.escState | 3);
                }
            }
        }


        // Fields
        internal const uint CLRBREAK = 9;
        internal const uint CLRDTR = 6;
        internal const uint CLRRTS = 4;
        private byte escState;
        private string fault;
        private IntPtr handle;
        internal const uint RESETDEV = 7;
        internal const uint SETBREAK = 8;
        internal const uint SETDTR = 5;
        internal const uint SETRTS = 3;
        internal const uint SETXOFF = 1;
        internal const uint SETXON = 2;
    }
}

⌨️ 快捷键说明

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