Exemplo n.º 1
0
        /// <summary>
        /// 构造方法传入一个要修改的变量和一个报警变量对象集合
        /// </summary>
        /// <param name="var"></param>
        /// <param name="list"></param>
        public FrmModifyVar(Variable_Modbus var, List <VarAlarm_Modbus> list)
            : this()
        {
            this.objVar       = var;
            this.listVarAlarm = list;

            ShowVariable();
        }
Exemplo n.º 2
0
        /// <summary>
        /// FrmNew窗体的委托所绑定的方法,该方法参数是从FrmNew窗体传过来
        /// </summary>
        /// <param name="var"></param>
        /// <param name="listVAlarm"></param>
        private void ReceiveVarFromNew(Variable_Modbus var, List <VarAlarm_Modbus> listVAlarm)
        {
            //将新建变量窗体要新建的变量和报警变量添加到集合中
            this.listVar.Add(var);

            if (listVAlarm != null)
            {
                this.listVarAlarm.AddRange(listVAlarm);
            }

            //更新dgv
            UpdatedgvVar();
            this.dgvVar.CurrentCell = this.dgvVar.Rows[this.dgvVar.Rows.Count - 1].Cells[0];
        }
        /// <summary>
        /// 获取所有的变量对象
        /// </summary>
        /// <param name="xmlpath">存放变量的xml文件路径</param>
        /// <returns></returns>
        public static List <Variable_Modbus> GetLIstVar()
        {
            if (!File.Exists(pathVar))
            {
                return(null);
            }

            List <Variable_Modbus> listVar = new List <Variable_Modbus>();

            //从指定路径加载xml文档
            XmlDocument xdoc = new XmlDocument();

            xdoc.Load(pathVar);

            //获取“Root”节点
            XmlNode noodroot = xdoc.SelectSingleNode("//Root");


            //读取Root节点下所有的变量节点,封装成变量对象,存入集合
            foreach (XmlNode noodtool in noodroot.ChildNodes)
            {
                if (noodtool.Name == "Variable")
                {
                    //封装变量对象
                    Variable_Modbus objVar = new Variable_Modbus();
                    objVar.VarName         = GetValueByAttribute(noodtool, "VarName");
                    objVar.Address         = GetValueByAttribute(noodtool, "Address");
                    objVar.DataType        = GetValueByAttribute(noodtool, "DataType");
                    objVar.StoreType       = GetValueByAttribute(noodtool, "StoreArea");
                    objVar.Note            = GetValueByAttribute(noodtool, "Note");
                    objVar.IsFiling        = GetValueByAttribute(noodtool, "IsFiling");
                    objVar.IsAlarm         = GetValueByAttribute(noodtool, "IsAlarm");
                    objVar.IsReport        = GetValueByAttribute(noodtool, "IsReport");
                    objVar.AbsoluteAddress = GetValueByAttribute(noodtool, "AbsoluteAddress");

                    listVar.Add(objVar);
                }
            }

            return(listVar);
        }
 /// <summary>
 /// 装载XML文件:根据路径返回LIST集合
 /// </summary>
 /// <param name="xmlpath"></param>
 /// <returns></returns>
 public static List <Variable_Modbus> LoadXML(string xmlpath)
 {
     VarModbusList.Clear();
     if (!File.Exists(xmlpath))
     {
         MessageBox.Show("IO配置变量的XML文件不存在!");
     }
     else
     {
         XmlDocument xdoc = new XmlDocument();
         xdoc.Load(xmlpath);
         foreach (XmlNode noodroot in xdoc.ChildNodes)
         {
             if (noodroot.Name == "Root")
             {
                 foreach (XmlNode noodtool in noodroot.ChildNodes)
                 {
                     if (noodtool.Name == "Variable")
                     {
                         Variable_Modbus objVar = new Variable_Modbus();
                         objVar.VarName         = XMLAttributeGetValue(noodtool, "VarName");
                         objVar.Address         = XMLAttributeGetValue(noodtool, "Address");
                         objVar.DataType        = XMLAttributeGetValue(noodtool, "DataType");
                         objVar.StoreArea       = XMLAttributeGetValue(noodtool, "StoreArea");
                         objVar.Note            = XMLAttributeGetValue(noodtool, "Note");
                         objVar.IsFiling        = XMLAttributeGetValue(noodtool, "IsFiling");
                         objVar.IsAlarm         = XMLAttributeGetValue(noodtool, "IsAlarm");
                         objVar.IsReport        = XMLAttributeGetValue(noodtool, "IsReport");
                         objVar.AbsoluteAddress = XMLAttributeGetValue(noodtool, "AbsoluteAddress");
                         VarModbusList.Add(objVar);
                     }
                 }
             }
         }
     }
     return(VarModbusList);
 }
Exemplo n.º 5
0
        /// <summary>
        /// 修改变量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.dgvVar.SelectedRows.Count != 1 || this.dgvVar.CurrentRow == null)
            {
                return;
            }

            #region 封装要修改的变量和其对应的报警对象集合

            //将dgv中选中列对应的变量对象从集合中读取出来
            int             index  = this.dgvVar.CurrentRow.Index;
            Variable_Modbus objVar = this.listVar[index];

            //变量对象对应的报警对象集合
            List <VarAlarm_Modbus> listAlarm = null;
            if (objVar.IsAlarm == "1")
            {
                listAlarm = new List <VarAlarm_Modbus>();
                //如果变量是设置为报警的,则从报警变量对象集合中取出其对应的报警变量,封装到集合中
                foreach (VarAlarm_Modbus item in this.listVarAlarm)
                {
                    if (item.VarName == objVar.VarName)
                    {
                        listAlarm.Add(item);
                    }
                }
            }
            #endregion

            //创建修改变量窗体,将要修改的变量和其对应的变量对象集合传递进去
            FrmModifyVar objFrm = new FrmModifyVar(objVar, listAlarm);
            DialogResult dr     = objFrm.ShowDialog();

            //修改变量窗体的变量修改成功后
            if (dr == DialogResult.OK)
            {
                //集合中替换新修改的变量
                this.listVar[index] = objFrm.objVar;


                //更新新修改变量对应的报警变量
                if (objFrm.objVar.IsAlarm == "1" && objFrm.listVarAlarm.Count > 0)
                {
                    //该变量是报警变量:则在报警变量对象集合中先清除原来的再添加新修改后的

                    for (int i = 0; i < listVarAlarm.Count; i++)
                    {
                        if (listVarAlarm[i].VarName == objFrm.objVar.VarName)
                        {
                            listVarAlarm.RemoveAt(i);
                            i--;
                        }
                    }

                    this.listVarAlarm.AddRange(objFrm.listVarAlarm);
                }
                else
                {
                    //该变量不是报警变量:则在报警变量集合中删除其关联的报警变量
                    for (int i = 0; i < listVarAlarm.Count; i++)
                    {
                        if (listVarAlarm[i].VarName == objFrm.objVar.VarName)
                        {
                            listVarAlarm.RemoveAt(i);
                            i--;
                        }
                    }
                }

                UpdatedgvVar();
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// 对变量集合进行分析,返回第一个数值为该变量类型的起始地址,第二个数值为长度
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private List <int> AnalyseVar(List <Variable_Modbus> list)
        {
            List <int> listResult  = new List <int>(2);
            List <int> listAddress = new List <int>();

            int    start    = 0;
            int    end      = 0;
            int    length   = 0;
            string dataType = string.Empty;

            //遍历所有的变量
            foreach (Variable_Modbus item in list)
            {
                //将变量的地址转成int型数据,存入地址集合中
                listAddress.Add(int.Parse(item.Address));
            }

            //获取起始地址,最小值
            start = listAddress.Min();
            //获取结束地址,最大值
            end = listAddress.Max();

            //查找出地址为最小值的变量,获取其数据类型
            Variable_Modbus lin = (from n in list where n.Address == end.ToString() select n).FirstOrDefault();

            dataType = lin.DataType;

            //判断其数据类型,获取存储区的总长度length
            switch (dataType)
            {
            case "Bool":
                length = end - start + 1;
                break;

            case "Float":
                length = end - start + 2;
                break;

            case "Float Inverse":
                length = end - start + 2;
                break;

            case "Long":
                length = end - start + 2;
                break;

            case "Long Inverse":
                length = end - start + 2;
                break;

            case "Double":
                length = end - start + 4;
                break;

            case "Double Inverse":
                length = end - start + 4;
                break;

            default:
                length = end - start + 2;
                break;
            }
            listResult.Add(start);
            listResult.Add(length);
            return(listResult);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 按下确认按钮时,获取窗口数据,封装对象
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                #region 封装变量对象
                //封装变量对象
                this.objVar      = new Variable_Modbus();
                objVar.VarName   = this.txtVarName.Text.Trim();
                objVar.StoreType = this.cboStoreArea.Text.Trim();
                objVar.DataType  = this.cboDataType.Text.Trim();
                objVar.Address   = this.txtAddress.Text.Trim();
                objVar.IsFiling  = this.chkIsFilling.Checked ? "1" : "0";
                objVar.IsAlarm   = this.chkIsAlarm.Checked ? "1" : "0";
                objVar.IsReport  = this.chkIsReport.Checked ? "1" : "0";
                objVar.Note      = this.txtNote.Text.Trim();
                #endregion

                #region 封装报警变量集合
                if (objVar.IsAlarm == "1")
                {
                    this.listVarAlarm = new List <VarAlarm_Modbus>();
                    //封装报警变量集合
                    if (this.chkIsAlarm.Checked)
                    {
                        //封装修改后新的集合
                        if (chk_High.Checked)
                        {
                            listVarAlarm.Add(new VarAlarm_Modbus()
                            {
                                VarName    = this.txtVarName.Text.Trim(),
                                Priority   = Convert.ToInt16(this.txt_Priority_High.Text.Trim()),
                                AlarmType  = "High",
                                AlarmValue = float.Parse(this.txt_Alarm_High.Text.Trim()),
                                Note       = this.txt_Note_High.Text.Trim()
                            });
                        }
                        if (chk_HiHi.Checked)
                        {
                            listVarAlarm.Add(new VarAlarm_Modbus()
                            {
                                VarName    = this.txtVarName.Text.Trim(),
                                Priority   = Convert.ToInt16(this.txt_Priority_HiHi.Text.Trim()),
                                AlarmType  = "HiHi",
                                AlarmValue = float.Parse(this.txt_Alarm_HiHi.Text.Trim()),
                                Note       = this.txt_Note_HiHi.Text.Trim()
                            });
                        }
                        if (chk_Low.Checked)
                        {
                            listVarAlarm.Add(new VarAlarm_Modbus()
                            {
                                VarName    = this.txtVarName.Text.Trim(),
                                Priority   = Convert.ToInt16(this.txt_Priority_Low.Text.Trim()),
                                AlarmType  = "Low",
                                AlarmValue = float.Parse(this.txt_Alarm_Low.Text.Trim()),
                                Note       = this.txt_Note_Low.Text.Trim()
                            });
                        }
                        if (chk_LoLo.Checked)
                        {
                            listVarAlarm.Add(new VarAlarm_Modbus()
                            {
                                VarName    = this.txtVarName.Text.Trim(),
                                Priority   = Convert.ToInt16(this.txt_Priority_LoLo.Text.Trim()),
                                AlarmType  = "LoLo",
                                AlarmValue = float.Parse(this.txt_Alarm_LoLo.Text.Trim()),
                                Note       = this.txt_Note_LoLo.Text.Trim()
                            });
                        }
                    }
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show("变量数据输入错误:" + ex.Message);
                return;
            }

            //调用其委托
            actionTrans(objVar, listVarAlarm);
        }