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

📄 serialport.cs

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

    public class SerialPort : SerialComm//声明SerialPort是SerialComm的一个继承
    {
        // Methods 方法
		/// <summary>
		/// 构造函数,形参为类WithEvents的实例
		/// </summary>
		/// <param name="ev"></param>
        public SerialPort(WithEvents ev)
        {
            this.ev = ev;
            this.index = 1;
            this.path = "";
            this.file = "Port";
            this.extn = ".cfg";
            this.SetName();
            this.cnfg = new SerialCnfg(this.index, this.name);
        }
    ///调用父类的销毁函数,用于关毕进程
        public void Close()
        {
            base.Destroy();
        }
    ///调用父类中断函数,来中断串口的通信
        protected override void OnBreak()
        {
            if (this.ev.Break != null)
            {
                this.ev.Break();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="state"></param>
        protected override void OnCTS(bool state)
        {
            if (this.ev.CtsSig != null)
            {
                this.ev.CtsSig(state);
            }
        }

        protected override void OnDSR(bool state)
        {
            if (this.ev.DsrSig != null)
            {
                this.ev.DsrSig(state);
            }
        }

        protected override void OnError(string fault)
        {
            if (this.ev.Error != null)
            {
                this.ev.Error(fault);
            }
        }

        protected override void OnRING(bool state)
        {
            if (this.ev.RingSig != null)
            {
                this.ev.RingSig(state);
            }
        }

        protected override void OnRLSD(bool state)
        {
            if (this.ev.RlsdSig != null)
            {
                this.ev.RlsdSig(state);
            }
        }

        protected override void OnRxChar(byte[] b)
        {
            if (this.ev.RxChar != null)
            {
                this.ev.RxChar(b);
            }
        }

        protected override void OnTxDone()
        {
            if (this.ev.TxDone != null)
            {
                this.ev.TxDone();
            }
        }

        public bool Open(int index)
        {
            if (index != this.index)
            {
                this.index = index;
                this.SetName();
                this.cnfg.Load(this.name);
            }
            return base.Create(this.cnfg);
        }

        private void SetName()
        {
            this.name = this.path + this.file + this.index.ToString() + this.extn;
        }


        // Properties  属性
        public SerialCnfg Cnfg
        {
            get
            {
                return this.cnfg;
            }
        }

        public string ConfigFileName
        {
            get
            {
                return this.name;
            }
        }

        public SerialPort Port
        {
            get
            {
                return this;
            }
        }

        public string SetExtension
        {
            set
            {
                this.extn = value;
                this.SetName();
            }
        }

        public string SetFile
        {
            set
            {
                this.file = value;
                this.SetName();
            }
        }

        public string SetPath
        {
            set
            {
                this.path = value;
                this.SetName();
            }
        }


        // Fields 文件
        private SerialCnfg cnfg;
        private WithEvents ev;
        private string extn;
        private string file;
        private int index;
        private string name;
        private string path;
    }
}

⌨️ 快捷键说明

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