Exemplo n.º 1
0
        public bool AddItems(List <Tag> biList)
        {
            try
            {
                #region  入的标签集合为空时,直接返回
                if (biList.Count == 0)
                {
                    return(false);
                }
                #endregion
                #region 当前标签集合已包含此Tag,则退出
                if (Tags.Count(p => biList.Select(b => b.TagName).Contains(p.TagName)) != 0)
                {
                    MessageBox.Show("Tag重复:在在重复的Tag点", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return(false);
                }


                #endregion

                //根据组名,取出组下的子集合
                OPCItems        items = this.OPCGroup.OPCItems;
                int             itmHandleBegin;
                int             NUMITEMS;
                Array           OPCItemIDs;
                Array           ItemClientHandles;
                Array           ItemServerHandles;
                Array           AddItemServerErrors;
                Func <Tag, int> f = (p => p != null ? p.ExtraAs <DaExtra>().ItmHandleClient : 0);

                itmHandleBegin      = f(Tags.LastOrDefault()); //itmHandleBegin的逻辑为最后一个标签itmHandle值
                NUMITEMS            = biList.Count + 1;        //OPCitem集合元素是以1为数组的基数,例:如果新增5个标签的话,就需要创建一个长度为6的标签,
                OPCItemIDs          = Array.CreateInstance(typeof(string), NUMITEMS);
                ItemClientHandles   = Array.CreateInstance(typeof(int), NUMITEMS);
                ItemServerHandles   = Array.CreateInstance(typeof(int), NUMITEMS);
                AddItemServerErrors = default(System.Array);

                //标签赋值后加入daTags
                for (int i = 1; i < NUMITEMS; i++)
                {
                    OPCItemIDs.SetValue(biList[i - 1].OpcTagName, i);
                    ItemClientHandles.SetValue(itmHandleBegin + i, i);
                    biList[i - 1].ExtraAs <DaExtra>().ItmHandleClient = itmHandleBegin + i;
                    Tags.Add((Tag)(Object)biList[i - 1]);
                }

                //关键,此步骤将标签添加入OPCclient的监听列表中
                items.AddItems(biList.Count, ref OPCItemIDs, ref ItemClientHandles, out ItemServerHandles, out AddItemServerErrors);

                #region ***********判断tag点是否正常监听************
                for (short i = 1; i < NUMITEMS; i++)
                {
                    if ((int)AddItemServerErrors.GetValue(i) == 0) //If the item was added successfully then allow it to be used.
                    {
                        Tags[i - 1].Message = "OPC Add Item Successful";
                        Tags[i - 1].Enabled = true;

                        //设置标签的数据类型及ServerHandle
                        OPCItem ki = items.GetOPCItem((int)ItemServerHandles.GetValue(i));
                        biList[i - 1].DataType = Enum.GetName(typeof(OpcTypes.CanonicalDataTypesForDa), ki.CanonicalDataType);
                        biList[i - 1].ExtraAs <DaExtra>().ItmHandleServer = (int)ItemServerHandles.GetValue(i);
                    }
                    else
                    {
                        Tags[i - 1].Message = "OPC Add Item Fail";
                        Tags[i - 1].Enabled = false;
                        //ItemServerHandles(i) = 0; // If the handle was bad mark it as empty
                        //OPCItemValue(i).Enabled = false;
                        //OPCItemValue(i).Text = "OPC Add Item Fail";
                    }
                }
                #endregion
                return(true);
            }
            catch (Exception e)
            {
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// OPC初始化
        /// 当定时时间到触发下面事件
        /// 开机后延迟1秒开始初始化OPC,以后10秒检查与OPC链接转态,如果断开链接就重新链接并初始化OPC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OPCconnectTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                OPCconnectTimer.Interval = 10000;
                if (objServer.ServerState != 1)
                {
                    //连接opc server
                    objServer.Connect("Kepware.KEPServerEX.V5", "");
                    #region 获取所有线体首站点以及PLC模板
                    string  sql       = @"select * from StationInfo(nolock) where IsFirstStation=1;
                                select a.ID as SID,a.StationCode,b.PLCTrigger,C.* from StationInfo(nolock) a 
                                join PLCTemplateOperationInfo(nolock) b on a.ID=b.PLCStationId
                                join PLCTemplateInfoDetail(nolock) c on b.PLCTemplateId=c.PLCTemplateId 
                                where a.IsFirstStation=1
                                order by a.ID,c.ID";
                    DataSet dsstation = SQLHelper.GetDataSet(sql);
                    #endregion
                    //(2)建立一个opc组集合
                    objGroups = objServer.OPCGroups;
                    //(3)建立一个opc组
                    objGroup = objGroups.Add(null); //Group组名字可有可无
                    //(4)添加opc标签
                    objGroup.IsActive     = true;   //设置该组为活动状态,连接PLC时,设置为非活动状态也一样
                    objGroup.IsSubscribed = true;   //设置异步通知
                    objGroup.UpdateRate   = 250;
                    objServer.OPCGroups.DefaultGroupDeadband = 0;
                    objGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                    objGroup.AsyncReadComplete  += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(AsyncReadComplete);
                    objGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(AsyncWriteComplete);
                    objItems = objGroup.OPCItems; //建立opc标签集合
                    #region 循环创建连接并监控PLC地址
                    if (dsstation == null || dsstation.Tables[0].Rows.Count <= 0 || dsstation.Tables[1].Rows.Count <= 0)
                    {
                        SystemLogs.InsertPLCLog("Order2PLC", "获取站点PLC模板失败!请确认配置!");
                    }
                    else
                    {
                        scount = dsstation.Tables[0].Rows.Count;
                        dcount = dsstation.Tables[1].Rows.Count;
                        int count = dsstation.Tables[1].Rows.Count + 1;
                        tmpIDs = new string[count];
                        int[] tmpCHandles = new int[count];
                        objChangeItem = new OPCItem[count];
                        List <int> statinindex = new List <int>();
                        for (int i = 1; i < count; i++)
                        {
                            tmpCHandles[i] = i;
                            tmpIDs[i]      = dsstation.Tables[1].Rows[i - 1]["PLCTrigger"].ToString() + "." + dsstation.Tables[1].Rows[i - 1]["PLCUPDBAddress"].ToString();
                            if (dsstation.Tables[1].Rows[i - 1]["PLCUPDBAddress"].ToString() == "Order0Init")
                            {
                                statinindex.Add(i);
                            }
                        }

                        strItemIDs     = (Array)tmpIDs;//必须转成Array型,否则不能调用AddItems方法
                        lClientHandles = (Array)tmpCHandles;
                        // 添加opc标签
                        SystemLogs.InsertPLCLog("Order2PLC", "添加监控标签!");
                        objItems.AddItems(count - 1, ref strItemIDs, ref lClientHandles, out lserverhandles, out lErrors, RequestedDataTypes, AccessPaths);
                        for (int i = 1; i < count; i++)
                        {
                            objChangeItem[i] = objItems.GetOPCItem(Convert.ToInt32(lserverhandles.GetValue(i)));
                        }
                        for (int i = 1; i < count; i++)
                        {
                            if (statinindex.IndexOf(i) >= 0)
                            {
                                SystemLogs.InsertPLCLog("Order2PLC", dsstation.Tables[1].Rows[i - 1]["PLCTrigger"].ToString() + "初始化站点编码:" + dsstation.Tables[1].Rows[i - 1]["StationCode"].ToString());
                                objChangeItem[i + 3].Write(dsstation.Tables[1].Rows[i - 1]["StationCode"].ToString());
                            }
                        }
                        //SystemLogs.InsertPLCLog("Order2PLC", "添加监控标签!");
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                objServer.Disconnect();
                SystemLogs.InsertPLCLog("Order2PLC", ex.Message);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// OPC初始化
        /// 当定时时间到触发下面事件
        /// 开机后延迟1秒开始初始化OPC,以后10秒检查与OPC链接转态,如果断开链接就重新链接并初始化OPC
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OPCconnectTimer_Tick(object sender, EventArgs e)
        {
            try
            {
                OPCconnectTimer.Interval = 10000;
                if (objServer.ServerState != 1)
                {
                    //连接opc server
                    objServer.Connect("Kepware.KEPServerEX.V5", "");
                    #region 获取所有监控信息点
                    string  sql       = @"select a.ID,a.EquipmentId,a.ParentId,a.EquipmentCode,a.EquipmentName,a.Team,a.PLCIP+'.'+b.PLCDB as PLCDB,b.FaultCode,b.FaultDesc,b.FaultType,b.ID AS FaultId,a.EType,'1' as alarmtype,a.IsPayPoint
                                    from EquipmentData(nolock) a 
                                    join FaultCodeInfo(nolock) b on a.ID=b.EquipmentId
union 
select a.ID,a.EquipmentId,a.ParentId,a.EquipmentCode,a.EquipmentName,a.Team,a.PLCIP+'.'+b.PLCUPDBAddress as PLCDB,'' FaultCode,b.UPDataDesc,'' FaultType,b.ID AS FaultId,a.EType,'0' as alarmtype,a.IsPayPoint
                                    from EquipmentData(nolock) a 
                                    join PLCTemplateInfoDetail(nolock) b on a.PLCDB=b.PLCTemplateID
order by EquipmentId,alarmtype asc";
                    DataSet dsstation = SQLHelper.GetDataSet(sql);
                    #endregion
                    //(2)建立一个opc组集合
                    objGroups = objServer.OPCGroups;
                    //(3)建立一个opc组
                    objGroup = objGroups.Add(null); //Group组名字可有可无
                    //(4)添加opc标签
                    objGroup.IsActive     = true;   //设置该组为活动状态,连接PLC时,设置为非活动状态也一样
                    objGroup.IsSubscribed = true;   //设置异步通知
                    objGroup.UpdateRate   = 250;
                    objServer.OPCGroups.DefaultGroupDeadband = 0;
                    objGroup.DataChange         += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
                    objGroup.AsyncReadComplete  += new DIOPCGroupEvent_AsyncReadCompleteEventHandler(AsyncReadComplete);
                    objGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(AsyncWriteComplete);
                    objItems = objGroup.OPCItems; //建立opc标签集合
                    #region 循环创建连接并监控PLC地址
                    if (dsstation == null || dsstation.Tables[0].Rows.Count <= 0)
                    {
                        SystemLogs.InsertPLCLog("AndonResult", "获取PLC报警配置失败!请确认配置!");
                    }
                    else
                    {
                        scount = dsstation.Tables[0].Rows.Count;
                        int count = dsstation.Tables[0].Rows.Count + 1;
                        tmpIDs = new string[count + dcount];
                        int[] tmpCHandles = new int[count];
                        objChangeItem = new OPCItem[count];
                        //设备部分
                        for (int i = 1; i < count; i++)
                        {
                            tmpCHandles[i] = i;
                            tmpIDs[i]      = dsstation.Tables[0].Rows[i - 1]["PLCDB"].ToString();

                            #region 保存站点和PLCDB关系
                            ItemDB it = new ItemDB();
                            it.dbseq         = i;
                            it.DBTabid       = dsstation.Tables[0].Rows[i - 1]["PLCDB"].ToString();
                            it.Team          = dsstation.Tables[0].Rows[i - 1]["Team"].ToString();
                            it.FaultDesc     = dsstation.Tables[0].Rows[i - 1]["FaultDesc"].ToString();
                            it.FaultCode     = dsstation.Tables[0].Rows[i - 1]["FaultCode"].ToString();
                            it.FaultType     = dsstation.Tables[0].Rows[i - 1]["FaultType"].ToString();
                            it.ID            = dsstation.Tables[0].Rows[i - 1]["ID"].ToString();
                            it.ParentId      = dsstation.Tables[0].Rows[i - 1]["ParentId"].ToString();
                            it.FaultId       = dsstation.Tables[0].Rows[i - 1]["FaultId"].ToString();
                            it.EquipmentCode = dsstation.Tables[0].Rows[i - 1]["EquipmentCode"].ToString();
                            it.EquipmentName = dsstation.Tables[0].Rows[i - 1]["EquipmentName"].ToString();
                            it.EquipmentId   = dsstation.Tables[0].Rows[i - 1]["EquipmentId"].ToString();
                            it.EType         = dsstation.Tables[0].Rows[i - 1]["EType"].ToString();
                            it.alarmtype     = dsstation.Tables[0].Rows[i - 1]["alarmtype"].ToString();
                            it.IsPayPoint    = dsstation.Tables[0].Rows[i - 1]["IsPayPoint"].ToString();

                            itemsdb.Add(it);
                            #endregion

                            if (dsstation.Tables[0].Rows[i - 1]["PLCDB"].ToString().IndexOf("E0AssetID") >= 0)
                            {
                                statinindex.Add(i);
                            }
                        }

                        strItemIDs     = (Array)tmpIDs;//必须转成Array型,否则不能调用AddItems方法
                        lClientHandles = (Array)tmpCHandles;
                        // 添加opc标签
                        SystemLogs.InsertPLCLog("AndonResult", "添加监控标签!");
                        objItems.AddItems(count - 1, ref strItemIDs, ref lClientHandles, out lserverhandles, out lErrors, RequestedDataTypes, AccessPaths);
                        for (int i = 1; i < (count); i++)
                        {
                            objChangeItem[i] = objItems.GetOPCItem(Convert.ToInt32(lserverhandles.GetValue(i)));
                        }
                        //初始化站点编码
                        for (int i = 1; i < count; i++)
                        {
                            if (statinindex.IndexOf(i) >= 0)
                            {
                                SystemLogs.InsertPLCLog("StationResult", dsstation.Tables[0].Rows[i - 1]["PLCDB"].ToString() + "初始化AssetID:" + dsstation.Tables[0].Rows[i - 1]["EquipmentId"].ToString());
                                objChangeItem[i].Write(dsstation.Tables[0].Rows[i - 1]["EquipmentId"].ToString());
                            }
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                objServer.Disconnect();
                SystemLogs.InsertPLCLog("AndonResult", ex.Message);
            }
        }