/// <summary> /// 设置默认的OPC项,假如已添加,则移除后再重新添加(同一时刻默认标签只有一个) /// </summary> /// <param name="itemId">标签ID</param> /// <param name="clientHandle">标签的客户端句柄</param> /// <param name="message">返回的错误信息</param> /// <returns></returns> public bool SetItem(string itemId, int clientHandle, out string message) { try { if (this.DefaultGroupInfo == null) { message = "未找到默认组"; return(false); } //初始化OPC项信息并在默认OPC组中添加 List <OpcItemInfo> list = new List <OpcItemInfo>() { new OpcItemInfo(itemId, clientHandle) }; this.DefaultGroupInfo.SetItems(list, out message); if (this.DefaultGroupInfo.ItemCount > 0) { OpcItemInfo item = this.DefaultGroupInfo.ListItemInfo.Last(); //保存默认OPC项的客户端句柄,服务端句柄,标签名称 this.ItemHandleClient = item.ClientHandle; this.ItemId = item.ItemId; this.ItemHandleServer = item.ServerHandle; } } catch (Exception ex) { this.ItemHandleClient = 0; message = "移除或添加标签时发生错误:" + ex.Message; return(false); } return(true); }
/// <summary> /// 根据给定的OPC项集合信息添加OPC项 /// </summary> /// <param name="items">给出添加OPC项时所需信息的集合</param> /// <param name="message">返回信息</param> /// <returns></returns> public bool SetItems(IEnumerable <OpcItemInfo> items, out string message) { message = string.Empty; if (items == null || items.Count() == 0) { return(false); } try { //假如已存在OPC项,先移除 if (this.ItemCount > 0) { this.OpcGroup.OPCItems.Remove(this.ItemCount, ref this.server_handles, out this.errors); } List <OpcItemInfo> itemList = items.ToList(); //转换为新List对象,防止枚举改变对象时出现未知影响 this.ListItemInfo.Clear(); this.ListItemInfo.Add(this.opc_pack_basic); itemList.Insert(0, this.opc_pack_basic); this.item_ids = itemList.Select(p => p.ItemId).ToArray(); this.client_handles = itemList.Select(p => p.ClientHandle).ToArray(); this.OpcGroup.OPCItems.AddItems(itemList.Count - 1, ref this.item_ids, ref this.client_handles, out this.server_handles, out this.errors); //添加OPC项后根据ID找到OPC项信息对象并设置服务端句柄,向OPC项信息List中添加 if (this.item_ids.Length > 1) { for (int i = 1; i < this.item_ids.Length; i++) { OpcItemInfo itemInfo = itemList.Find(p => p.ItemId.Equals(this.item_ids.GetValue(i))); itemInfo.ServerHandle = int.Parse(this.server_handles.GetValue(i).ToString()); this.ListItemInfo.Add(itemInfo); } } //假如添加后的数量对不上,则至少有一个OPC项未添加成功 if (this.ListItemInfo.Count < itemList.Count) { message = "至少有1个OPC项未添加成功"; } } catch (Exception ex) { message = string.Format("OPC组{0}移除或添加标签时发生错误:{1}", this.OpcGroup.Name, ex.Message); return(false); } return(this.ListItemInfo.Count > 1); //假如至少有1个添加成功,返回true }
/// <summary> /// 为OPC组OPC项List内给定数量的、与给定服务端句柄对应的OPC项读取数据 /// </summary> /// <param name="serverHandles">服务端句柄Array</param> /// <param name="itemCount">读取的OPC项数量</param> /// <param name="message">返回信息</param> /// <returns></returns> public bool ReadValues(Array serverHandles, /*int itemCount, */ out string message) { message = string.Empty; object qualities, timeStamps; Array values; try { IEnumerable <int> temp = serverHandles == null || serverHandles.Length == 0 ? null : serverHandles.Cast <int>(); Array handles = this.GetServerHandles(temp); int itemCount = handles.Length - 1; this.OpcGroup.SyncRead((short)OPCDataSource.OPCDevice, itemCount, ref handles, out values, out this.errors, out qualities, out timeStamps); //假如至少读取到1个值,根据服务端句柄找到OPC项信息并更新值 if (values.Length > 0) { for (int i = 1; i <= values.Length; i++) { OpcItemInfo itemInfo = this.ListItemInfo.Find(item => item.ServerHandle.Equals(handles.GetValue(i))); if (itemInfo != null) { itemInfo.Value = values.GetValue(i).ToString(); } } } if (values.Length < itemCount) { message = "至少有1个OPC项的值未找到"; } GC.Collect(); } catch (Exception ex) { message = string.Format("从名称为{0}的OPC组读取值失败:{1}", this.OpcGroup.Name, ex.Message); return(false); } return(values.Length > 0); }