示例#1
0
        public void SyncRead(object sender, EventArgs e)
        {
            tTimer.Stop();

            try
            {
                object ItemValues; object Quantities; object TimeStamps;//同步读取临时变量 值 质量 时间戳

                for (int i = 0; i < ModelList.Count; i++)
                {
                    opcItem[i].Read(1, out ItemValues, out Quantities, out TimeStamps);

                    if (Quantities.ToString() == "192")
                    {
                        Address            = ModelList[i].KepAddress;
                        myDcValue[Address] = ItemValues.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                tTimer.Start();
            }
            tTimer.Start();
        }
示例#2
0
        public void SyncRead(object sender, EventArgs e)
        {
            tTimer.Stop();
            string LabelName = string.Empty;

            try
            {
                object ItemValues; object Quantities; object TimeStamps;//同步读取临时变量 值 质量 时间戳

                for (int i = 0; i < paramClass.ParamList.Count; i++)
                {
                    opcItem[i].Read(1, out ItemValues, out Quantities, out TimeStamps);
                    if (Quantities.ToString() == "192")
                    {
                        LabelName = paramClass.ParamList[i].LabelName;

                        Type type = paramClass.GetType();                                          //获取类型
                        System.Reflection.PropertyInfo propertyInfo = type.GetProperty(LabelName); //获取指定名称的属性
                        //int value_Old = (int)propertyInfo.GetValue(paramClass, null); //获取属性值
                        if (propertyInfo != null)
                        {
                            propertyInfo.SetValue(paramClass, ItemValues.ToString(), null);                       //给对应属性赋值
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                tTimer.Start();
            }
            tTimer.Start();
        }
示例#3
0
        private void InitValue()
        {
            //同步读
            object ItemValues, Qualities, TimeStamps;

            textBoxResult.Text = "";
            for (int i = opcItem.Count() - 1; i >= 0; i--)
            {
                try
                {
                    opcItem[i].Read(1, out ItemValues, out Qualities, out TimeStamps);

                    ItemList[i].value = ItemValues.ToString();
                    labelState.Text   = "Item: " + ItemList[i].name + " value:" + ItemList[i].value;
                }
                catch (Exception ex)
                {
                    textBoxResult.Text += "Item: " + ItemList[i].name + " error: " + ex.Message + "\r\n";
                    //  labelState.Text = ex.Message;
                }
            }
        }
示例#4
0
        private void buttonCheckItem_Click(object sender, EventArgs e)
        {
            string        inpath   = textBoxInFile.Text;
            string        outpath  = textBoxCheckItemOutPath.Text;
            List <string> itemlist = new List <string> {
            };

            if (textBoxInFile.Text.Equals(""))
            {
                MessageBox.Show("输入文件路径不存在");
            }
            if (textBoxCheckItemOutPath.Text.Equals(""))
            {
                MessageBox.Show("输出文件路径不存在");
            }
            //FileStream chefile = new FileStream(outpath, FileMode.Create);
            if (!textBoxInFile.Text.Equals("") && !textBoxCheckItemOutPath.Text.Equals(""))
            {
                try
                {
                    StreamReader sr = new StreamReader(inpath, Encoding.Default);
                    string       line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        //读取一行outpath
                        line = line.Replace("\t", "");
                        line = line.Replace(" ", "");
                        line = line.Replace(",", "");
                        itemlist.Add(line);
                    }
                    sr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("读取文件失败");
                }
                try
                {
                    opcGroups = opcServer.OPCGroups;
                    DateTime dt = new DateTime();
                    dt = DateTime.Now;
                    dt.Second.ToString();
                    opcGroup = opcGroups.Add("Group" + dt.Hour.ToString() + dt.Second.ToString());
                    //以下设置属性
                    {
                        opcGroups.DefaultGroupIsActive   = true;    //激活组
                        opcGroups.DefaultGroupDeadband   = 0;       //死区值,设为0时,服务器端该组内任何数据变化都通知组
                        opcGroups.DefaultGroupUpdateRate = 5000;    //默认组群的刷新频率为200ms
                        this.labelState.Text             = "UpdateRate = 5000ms";

                        opcGroup.UpdateRate   = 5000; //刷新频率为5秒
                        opcGroup.IsSubscribed = true; //如果没有订阅,则该组的DataChange回调事件不会发生
                    }
                    //添加Item
                    //设置组内的Items
                    opcItems = opcGroup.OPCItems;
                    opcItem  = new OPCItem[itemlist.Count];
                    for (int i = 0; i < itemlist.Count; i++)
                    {
                        opcItem[i] = opcItems.AddItem(itemlist[i], i + 1);
                    }
                    //同步读
                    object ItemValues, Qualities, TimeStamps;

                    for (int i = opcItem.Count() - 1; i >= 0; i--)
                    {
                        try
                        {
                            opcItem[i].Read(1, out ItemValues, out Qualities, out TimeStamps);
                            string value = ItemValues.ToString();
                            File.AppendAllText(outpath, "Item: " + itemlist[i] + " value:" + value + "\r\n");
                        }
                        catch (Exception ex)
                        {
                            File.AppendAllText(outpath, "Item: " + itemlist[i] + " error: " + ex.Message + "\r\n");
                            //  labelState.Text = ex.Message;
                        }
                    }
                }
                catch (Exception err)
                {
                    //throw new Exception("CreateGroup:" + err.Message);
                }
            }
        }