public bool GetItems(string groupName, out string[] itemNames, out string reStr) { reStr = ""; itemNames = null; try { OPCGroups myGroups = myServer.OPCGroups; if (myGroups == null) { reStr = "得到项列表失败,组名:" + groupName + "不存在"; return(false); } OPCGroup myGroup = myGroups.GetOPCGroup(groupName); if (myGroup == null) { reStr = "得到项列表失败"; return(false); } OPCItems items = myGroup.OPCItems; itemNames = new string[items.Count]; for (int i = 0; i < itemNames.Count(); i++) { itemNames[i] = items.Item(i + 1).ItemID; } reStr = "得到项列成功!"; return(true); } catch (System.Exception ex) { reStr = "得到项列表失败,返回异常:" + ex.Message; return(false); } }
/// <summary> /// 移除项,注意:此接口目前未能实现,需要继续调试 /// </summary> /// <param name="groupName"></param> /// <param name="itemName"></param> /// <param name="reStr"></param> /// <returns></returns> public bool RemoveItem(string groupName, string itemName, out string reStr) { reStr = ""; try { OPCGroup myGroup = GetGroup(groupName); if (myGroup == null) { reStr = "组:" + groupName + "不存在 "; return(false); } OPCItems items = myGroup.OPCItems; OPCItem item = items.Item(itemName); Array itemHandles = Array.CreateInstance(typeof(int), 1); itemHandles.SetValue(item.ServerHandle, 0); Array errors = null;// Array.CreateInstance(typeof(long), 10); items.Remove((int)1, ref itemHandles, out errors); reStr = "移除项:" + itemName + "成功!"; return(true); } catch (System.Exception ex) { reStr = "移除项:" + itemName + "失败,返回异常:" + ex.Message; return(false); } }
//初始化OPC行 static void InitItems() { OPCGroup group = _opcServer.OPCGroups.GetOPCGroup(groupName); OPCItems items = group.OPCItems; OPCItem a = items.AddItem(itemID1, 1); OPCItem b = items.AddItem(itemID2, 2); a = items.Item(2); b = items.Item(1); int c = 0; c = a.ClientHandle; c = b.ClientHandle; c = 0; }
public bool kep_initial(string path, string devicesymbol) { try { int num = path.LastIndexOf("\\Config"); string device = path.Substring(num + 8); KepServer = new OPCServer(); ////连接opc server KepServer.Connect("KEPware.KEPServerEx.V4", ""); //(2)建立一个opc组集合 KepGroups = KepServer.OPCGroups; //(3)建立一个opc组 KepGroup = KepGroups.Add(null); //Group组名字可有可无 //(4)添加opc标签 KepGroup.IsActive = true; //设置该组为活动状态,连接PLC时,设置为非活动状态也一样 KepGroup.IsSubscribed = true; //设置异步通知 KepGroup.UpdateRate = 250; KepServer.OPCGroups.DefaultGroupDeadband = 0; KepItems = KepGroup.OPCItems; //建立opc标签集合 //string path = System.Environment.CurrentDirectory; StreamReader sr = new StreamReader(path); string content = sr.ReadToEnd(); string[] str = content.Split(new string[] { "\r\n" }, StringSplitOptions.None); //混砂车标注名处理 if (devicesymbol == "B") { foreach (object temp0 in str) { string temp1 = temp0.ToString(); string temp = temp1.Replace("\t", ""); KepItems.AddItem("iFrac.Blender." + temp.ToString(), item_oder); Item_serverhandle1_To_PC[item_oder_To_PC] = KepItems.Item(item_oder).ServerHandle; item_oder_To_PC = item_oder_To_PC + 1; item_oder = item_oder + 1; } } //压力泵标注名处理 if (devicesymbol == "F") { for (int i = 0; i < str.Length; i++) { string temp1 = str[i].ToString(); string temp = temp1.Replace("\t", ""); int num1 = i / 5 + 1; int num2 = i % 5 + 1; KepItems.AddItem("iFrac_F" + num1.ToString("00") + ".Frac.var" + num2, item_oder); Item_serverhandle1_To_PC[item_oder_To_PC] = KepItems.Item(item_oder).ServerHandle; item_oder_To_PC = item_oder_To_PC + 1; item_oder = item_oder + 1; } } //0512 状态信号和ip地址 if (devicesymbol == "S") { for (int i = 0; i < str.Length; i++) { string temp1 = str[i].ToString(); string temp = temp1.Replace("\t", ""); KepItems.AddItem(temp, item_oder); Item_serverhandle1_To_PC[item_oder_To_PC] = KepItems.Item(item_oder).ServerHandle; item_oder_To_PC = item_oder_To_PC + 1; item_oder = item_oder + 1; } } return(true); } catch (Exception ex) { return(false); } }