winform空压机控制流程(学习笔记)

1.自定义控件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MS_UI
{
    /// <summary>
    /// 冷却泵
    /// </summary>
    public partial class LQB : UserControl
    {
        public LQB()
        {
            InitializeComponent();
            mytimer = new Timer();
            mytimer.Interval = 10;
            mytimer.Tick += Mytimer_Tick;
            mytimer.Enabled = true;
        }

        private void Mytimer_Tick(object sender, EventArgs e)
        {
            if (this.DeviceStateName != null)
            {
                if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(DeviceStateName))
                {
                    Start = CommonMethods.CurrentValue[DeviceStateName] == "1" ? true : false;
                }
            }


            if (Start)
            {
                if (this.index < this.indexMax)
                {
                    this.index++;
                }
                else
                {
                    this.index = 0;
                }
            }
            this.change();
        }

        private void change()
        {
            switch (this.index)
            {
                case 0: this.MainPic.Image = Properties.Resources._1; break;
                case 1: this.MainPic.Image = Properties.Resources._2; break;
                case 2: this.MainPic.Image = Properties.Resources._3; break;
                case 3: this.MainPic.Image = Properties.Resources._4; break;
                case 4: this.MainPic.Image = Properties.Resources._5; break;
                case 5: this.MainPic.Image = Properties.Resources._6; break;
                case 6: this.MainPic.Image = Properties.Resources._7; break;
                case 7: this.MainPic.Image = Properties.Resources._8; break;
                case 8: this.MainPic.Image = Properties.Resources._9; break;
                case 9: this.MainPic.Image = Properties.Resources._10; break;
                case 10: this.MainPic.Image = Properties.Resources._11; break;
                default: this.MainPic.Image = Properties.Resources._1; break;
            }

        }

        //当前选择的图片序号
        private int index;

        //定义一个定时器
        private Timer mytimer;

        //定义定时器的时间间隔
        private int interval = 10;

        /// <summary>
        /// 定义设备状态变量
        /// </summary>
        public string DeviceStateName { get; set; }

        /// <summary>
        /// 定义设备启动变量
        /// </summary>
        public string DeviceStartName { get; set; }

        /// <summary>
        /// 定义设备停止变量
        /// </summary>
        public string DeviceStopName { get; set; }

        /// <summary>
        /// 定义设备名称
        /// </summary>
        public string DeviceName { get; set; }


        public int Interval
        {

            get { return mytimer.Interval; }
            set
            {

                if (value != mytimer.Interval)
                {
                    mytimer.Interval = value;
                }
            }
        }

        //定义控件的转动属性
        private bool start = false;
        public bool Start
        {
            get { return start; }
            set { start = value; }
        }

        //最大图片数目
        private int indexMax = 11;
        public int IndexMax
        {
            get { return indexMax; }
            set { indexMax = value; }
        }

        public delegate void PumpClickDelegate(object sender, EventArgs e);

        public event PumpClickDelegate UserControlClick;
//自定义控件的双击事件 private void MainPic_DoubleClick(object sender, EventArgs e) { if (UserControlClick != null) { UserControlClick(this, new EventArgs());//把自身传入 } } } }

 2,主程序运行的时候读取配置信息,把这两个通过键值对的方式储存

        private void InitialTxt()
        {
            //第一步:通讯接口信息
            CommonMethods.objModbusEntity = CommonMethods.GetConfigInfo(FilePath + "Modbus.ini");
            //第二步:变量集合信息
            CommonMethods.VarModbusList = CommonMethods.LoadXML(FilePath + "Variable_Modbus.xml");
            //第三部:存储区域集合
            CommonMethods.StoreAreaList = CommonMethods.LoadStoreAreaXML(FilePath + "StoreArea.xml");
            //第四步:报警变量集合
            CommonMethods.VarAlarmModbusList = CommonMethods.LoadAlarmXML(FilePath + "VarAlarm_Modbus.xml");
            //第五步:其他初始化
            foreach (Variable_Modbus item in CommonMethods.VarModbusList)
            {
                if (!CommonMethods.CurrentValue.ContainsKey(item.VarName))//如果不包含
                {
                    CommonMethods.CurrentValue.Add(item.VarName, "");//新加一个键值对
                    CommonMethods.CurrentVarAddress.Add(item.VarName, item.Address);
                }

                if (item.StoreArea == "01 Coil Status(0x)")
                {
                    objComm.List_0x.Add(item);
                }

                if (item.StoreArea == "02 Input Status(1x)")
                {
                    objComm.List_1x.Add(item);
                }

                if (item.StoreArea == "03 Holding Register(4x)")
                {
                    objComm.List_4x.Add(item);
                }

                if (item.StoreArea == "04 Input Register(3x)")
                {
                    objComm.List_3x.Add(item);
                }

                if (item.IsFiling == "1")//如果要归档
                {
                    CommonMethods.FileVarModbusList.Add(item);
                    CommonMethods.CurrentVarNote.Add(item.VarName, item.Note);
                    CommonMethods.FileVarNameList.Add(item.Note);
                }
                if (item.IsReport == "1")//如果要参与报表
                {
                    CommonMethods.ReportVarModbusList.Add(item);
                }
            }
        }

  

winform空压机控制流程(学习笔记)

 

 

 

3,在控制的窗口添加自定义控件,自定义的控件  开始和停止 的名称和上面 变量的名称一致

 

winform空压机控制流程(学习笔记)

 

 3.1 在控制的窗口代码添加方法

        private void Device_DoubleClick(object sender, EventArgs e)
        {
            string DeviceName = string.Empty;
            string StartAddress = string.Empty;
            string StopAddress = string.Empty;
            string DeviceStartName = string.Empty;
            string DeviceStopName = string.Empty;

            //设备判断
            if (sender is LQB)//如果是冷却泵
            {
                LQB item = (LQB)sender;
                DeviceName = item.DeviceName;//获取 冷却泵 控件的属性值
                DeviceStartName = item.DeviceStartName;
                DeviceStopName = item.DeviceStopName;

            }
            else if ( sender is Tap)//如果是冷却泵
            {
                Tap item = (Tap)sender;
                DeviceName = item.DeviceName;
                DeviceStartName = item.DeviceStartName;
                DeviceStopName = item.DeviceStopName;
            }

            if (!string.IsNullOrEmpty(DeviceName) && !string.IsNullOrEmpty(DeviceStartName) &&
                !string.IsNullOrEmpty(DeviceStopName))
            {
                //采集参数(获取开始地址和停止地址)
                if (CommonMethods.CurrentVarAddress.ContainsKey(DeviceStartName))//CommonMethods.CurrentVarAddress在主窗体加载的时候传值的
                {
                    StartAddress = CommonMethods.CurrentVarAddress[DeviceStartName];
                }
                if (CommonMethods.CurrentVarAddress.ContainsKey(DeviceStopName))
                {
                    StopAddress = CommonMethods.CurrentVarAddress[DeviceStopName];
                }
            }
            else
            {
                MessageBox.Show("该控件未绑定需要的属性值");
            }



            //调用  设备控制的 窗体
            Frm_DeviceControl objFrm = new Frm_DeviceControl(DeviceName,StartAddress,StopAddress);
            objFrm.ShowDialog();
        }

  自定义空间的事件 绑定 控制窗体中写的方法

winform空压机控制流程(学习笔记)

 

 分析上面的代码

winform空压机控制流程(学习笔记)

 

 4,控制窗体接收上面传过来的值,Modbus写入

winform空压机控制流程(学习笔记)

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MS_UI
{
    public partial class Frm_DeviceControl : Form
    {
        public Frm_DeviceControl()
        {
            InitializeComponent();
        }

        string StartAddress = string.Empty;
        string StopAddress = string.Empty;

        /// <summary>
        /// 构造方法(通过构造方法传参)
        /// </summary>
        /// <param name="DeviceName"></param>
        /// <param name="StartAddress">开始地址</param>
        /// <param name="StopAddress">停止地址</param>
        public Frm_DeviceControl(string DeviceName, string StartAddress, string StopAddress)
        {
            InitializeComponent();
            this.StartAddress = StartAddress;
            this.StopAddress = StopAddress;
            this.lbl_StartName.Text = "开启" + DeviceName;
            this.lbl_StopName.Text = "关闭" + DeviceName;
        }

        //开启按钮
        private void btn_Start_Click(object sender, EventArgs e)
        {
            CommonMethods.IsWriting = true;//表示我现在要写数据了
            Thread.Sleep(500);//防止我这边要写了,你那边还在干点什么,停止一会,让那边有足够的时间干完
            bool res1 = CommonMethods.objMod.PreSetKeepReg(CommonMethods.Address, int.Parse(this.StartAddress), 256);//预值寄存器
            bool res2 = CommonMethods.objMod.PreSetKeepReg(CommonMethods.Address, int.Parse(this.StartAddress), 0);//预值寄存器
            if (res1 && res2)
            {
                MessageBox.Show(this.lbl_StartName.Text + "成功!", "设备开启");
            }
            else
            {
                MessageBox.Show(this.lbl_StartName.Text + "失败!", "设备开启");
            }
            CommonMethods.IsWriting = false;//写完数据之后,要设置为false,表示我没有在写数据了,不然那边就读取不了数据
        }

        //停止按钮
        private void btn_Stop_Click(object sender, EventArgs e)
        {
            CommonMethods.IsWriting = true;
            Thread.Sleep(500);
            bool res1 = CommonMethods.objMod.PreSetKeepReg(CommonMethods.Address, int.Parse(this.StopAddress), 256);
            bool res2 = CommonMethods.objMod.PreSetKeepReg(CommonMethods.Address, int.Parse(this.StopAddress), 0);
            if (res1 && res2)
            {
                MessageBox.Show(this.lbl_StopName.Text + "成功!", "设备关闭");
            }
            else
            {
                MessageBox.Show(this.lbl_StopName.Text + "失败!", "设备关闭");
            }
            CommonMethods.IsWriting = false;
        }
    }
}

  

 

winform空压机控制流程(学习笔记)

上一篇:Qt 如何使窗体初始最大化


下一篇:Windows中 php -v 与phpinfo 查询的版本不一致