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

📄 win32modem.cs

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

    internal class Win32Modem
    {
        // Methods
        internal Win32Modem()
        {
            this.signal = new MODSIGS();
        }

        internal bool Get(IntPtr handle)
        {
            if (!Win32Modem.GetCommModemStatus(handle, out this.status))
            {
                int num1 = Marshal.GetLastWin32Error();
                this.fault = "GetCommModemStatus() Failed. System Returned Error Code: " + num1.ToString();
                return false;
            }
            this.signal.CTS = (this.status & 0x10) != 0;
            this.signal.DSR = (this.status & 0x20) != 0;
            this.signal.RING = (this.status & 0x40) != 0;
            this.signal.RLSD = (this.status & 0x80) != 0;
            return true;
        }

        [DllImport("kernel32.dll")]
        private static extern bool GetCommModemStatus(IntPtr hFile, out uint lpModemStat);


        // Properties
        internal bool CtsState
        {
            get
            {
                return this.signal.CTS;
            }
        }

        internal bool DsrState
        {
            get
            {
                return this.signal.DSR;
            }
        }

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

        internal bool RingState
        {
            get
            {
                return this.signal.RING;
            }
        }

        internal bool RlsdState
        {
            get
            {
                return this.signal.RLSD;
            }
        }

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


        // Fields
        private string fault;
        internal const uint MS_CTS_ON = 0x10;
        internal const uint MS_DSR_ON = 0x20;
        internal const uint MS_RING_ON = 0x40;
        internal const uint MS_RLSD_ON = 0x80;
        private MODSIGS signal;
        private uint status;

        // Nested Types
        [StructLayout(LayoutKind.Sequential)]
        internal protected struct MODSIGS
        {
            internal bool CTS;
            internal bool DSR;
            internal bool RLSD;
            internal bool RING;
        }
    }
}

⌨️ 快捷键说明

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