winform Air Compressor Control Process (study notes)

1. Custom Controls

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;
                . 6 Case: 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; 
            } 

        } 

        // currently selected picture number 
        Private int index; 

        // define a timer 
        Private the timer myTimer; 

        // timer interval defined 
        Private interval the int = 10; 

        /// <Summary> 
        /// apparatus defined state variables 
        /// </ summary> 
        public String DeviceStateName {GET; SET;} 

        /// <Summary> 
        /// defined variable device starts
        /// </ Summary> 
        public String DeviceStartName {GET; SET;} 

        /// <Summary> 
        /// stop device defined variable 
        /// </ Summary> 
        public String DeviceStopName {GET; SET;} 

        /// <Summary > 
        /// defined device name 
        /// </ Summary> 
        public String {DeviceName GET; SET;} 


        public int the Interval 
        { 

            GET mytimer.Interval {return;} 
            SET 
            { 

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

        rotational control defined properties //
        Start to false BOOL = Private; 
        public BOOL the Start 
        { 
            GET Start {return;} 
            SET {Start = value;} 
        } 

        // maximum number of images 
        Private indexMax = int. 11; 
        public int IndexMax 
        { 
            GET indexMax {return;} 
            SET = {value indexMax ;} 
        } 

        public void PumpClickDelegate the delegate (SENDER Object, EventArgs E); 

        public event PumpClickDelegate UserControlClick; 
// custom control the double-click event Private void MainPic_DoubleClick (SENDER Object, EventArgs E) { IF (! UserControlClick = null) { UserControlClick (the this, new new EventArgs ()); // put their incoming } } } }

 2, when the main program is running to read configuration information, by way of these two stored key-value pairs

        void InitialTxt Private () 
        { 
            // Step: Communication interface information 
            CommonMethods.objModbusEntity = CommonMethods.GetConfigInfo (FilePath + "Modbus.ini"); 
            // Step: Variable set information 
            CommonMethods.VarModbusList = CommonMethods.LoadXML (FilePath + "Variable_Modbus.xml"); 
            // third unit: a storage area set 
            CommonMethods.StoreAreaList = CommonMethods.LoadStoreAreaXML (FilePath + "StoreArea.xml"); 
            // fourth step: alarm variable set 
            CommonMethods.VarAlarmModbusList = CommonMethods.LoadAlarmXML (+ FilePath "VarAlarm_Modbus.xml"); 
            // step 5: additional initialize 
            the 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);
                }
            }
        }

  

 

 

 

3, add a custom control in the control window, the same name of the custom controls start and stop of the above names and variables

 

 

 3.1 The method of adding the code control window

        void Device_DoubleClick Private (SENDER Object, EventArgs E) 
        { 
            String DeviceName = string.Empty; 
            String StartAddress = string.Empty; 
            String StopAddress = string.Empty; 
            String DeviceStartName = string.Empty; 
            String DeviceStopName = string.Empty; 

            // device Analyzing 
            if (sender is LQB) // if the cooling pump 
            { 
                LQB Item = (LQB) SENDER; 
                DeviceName = item.DeviceName; // Get cooling pump control attribute value 
                DeviceStartName = item.DeviceStartName; 
                DeviceStopName = item.DeviceStopName; 

            }
            else if (sender is Tap) // if the cooling pump is 
            { 
                the Tap Item = (the Tap) SENDER; 
                DeviceName = item.DeviceName; 
                DeviceStartName = item.DeviceStartName; 
                DeviceStopName = item.DeviceStopName; 
            } 

            ! IF (String.IsNullOrEmpty (DeviceName) &&! String.IsNullOrEmpty (DeviceStartName) && 
                ! String.IsNullOrEmpty (DeviceStopName)) 
            { 
                // acquisition parameters (acquisition start address and stop address) 
                IF (CommonMethods.CurrentVarAddress.ContainsKey (DeviceStartName)) // main form CommonMethods.CurrentVarAddress when the loaded mass values 
                {
                    = CommonMethods.CurrentVarAddress StartAddress [DeviceStartName];  
                }
                IF (CommonMethods.CurrentVarAddress.ContainsKey (DeviceStopName)) 
                { 
                    StopAddress = CommonMethods.CurrentVarAddress [DeviceStopName]; 
                } 
            } 
            the else 
            { 
                MessageBox.Show ( "attribute value of the control required unbound") ; 
            } 



            // call control apparatus form 
            Frm_DeviceControl objFrm = new new Frm_DeviceControl (DeviceName, StartAddress, StopAddress); 
            objFrm.ShowDialog (); 
        }

  Write control method in the form of self-defined event space bound

 

 Analysis of the above code

 

 4, the reception window control value transmitted over the above, Modbus write

 

 

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,E EventArgs) 
            CommonMethods.IsWriting = to true; // I represents the data write
        {
            Thread.Sleep (500); // prevent me write here, and you still do something over there, stop for a while, let the other side have enough time to finish 
            bool res1 = CommonMethods.objMod.PreSetKeepReg (CommonMethods. Address, int.Parse (this.StartAddress), 256 ); // pre-register value 
            bool res2 = CommonMethods.objMod.PreSetKeepReg (CommonMethods.Address, int.Parse (this.StartAddress), 0); // pre-register value 
            if (&& RES1 RES2) 
            { 
                MessageBox.Show (this.lbl_StartName.Text + "! success", "apparatus open"); 
            } 
            the else 
            { 
                MessageBox.Show (this.lbl_StartName.Text + "failed!", "apparatus open") ; 
            } 
            CommonMethods.IsWriting = to false;After // written data, to be set to false, indicating I did not write the data, or else there would not read data 
        } 

        // stop button 
        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;
        }
    }
}

  

 

Guess you like

Origin www.cnblogs.com/baozi789654/p/12597597.html