private void StartAsyncRefreshFromDevice() { _timerRefreshFromDevice = new System.Timers.Timer(); _timerRefreshFromDevice.Interval = 1 * 60 * 1000; _timerRefreshFromDevice.Elapsed += (o, e) => { try { if (this.IsOpcConnected) { _group.AsyncRefresh((short)OPCDataSource.OPCDevice, 1002, out int cancelId); } } catch (Exception ee) { TraceManagerForOPC.AppendErrMsg("OPC DA定时从客户端刷新数据异常" + ee.Message); } }; _timerRefreshFromDevice.Enabled = true; }
// 枚举 OPC 服务器中所有标签 private void BuildGroupsAndTags() { _client.OPCGroups.DefaultGroupIsActive = true; _client.OPCGroups.DefaultGroupDeadband = _defaultGroupDeadband; _client.OPCGroups.DefaultGroupUpdateRate = _updateRate; // 准备批量创建节点 List <string> listItemIds = new List <string>(); List <int> listClientHandles = new List <int>(); int itemHandle = 1; int itemCount = 0; // 批量创建时, 数组标号从 1 开始, 0 位无用 listItemIds.Add(""); listClientHandles.Add(0); // 枚举节点 OPCBrowser opcBrowser = _client.CreateBrowser(); opcBrowser.ShowBranches(); opcBrowser.ShowLeafs(true); foreach (object turn in opcBrowser) { string name = turn.ToString(); if (string.IsNullOrEmpty(name)) { continue; } // 过滤系统变量 if (name.StartsWith("_") || name.IndexOf("._") > 0) { continue; } listItemIds.Add(name); listClientHandles.Add(itemHandle++); itemCount++; } // 创建组和节点 _group = _client.OPCGroups.Add("RootGroup"); Array itemIds = listItemIds.ToArray(); Array clientHandles = listClientHandles.ToArray(); _group.OPCItems.AddItems(itemCount, ref itemIds, ref clientHandles, out Array serverHandles, out Array errors); // 创建标签 for (int i = 1; i < itemCount + 1; i++) { string itemName = Convert.ToString(itemIds.GetValue(i)); int clientHandle = Convert.ToInt32(clientHandles.GetValue(i)); int serverHandle = Convert.ToInt32(serverHandles.GetValue(i)); OpcTag opcTag = new OpcTag(itemName, clientHandle, serverHandle); lock (_tagNamedIndex) _tagNamedIndex[itemName] = opcTag; lock (_tagClientHandleIndex) _tagClientHandleIndex[clientHandle] = opcTag; lock (_tagServerHandleIndex) _tagServerHandleIndex[serverHandle] = opcTag; } // 启用订阅 _group.IsSubscribed = true; _group.DataChange += OnDataChange; // 从客户端缓存读取一次, 获取初始值 itemCount = _group.OPCItems.Count; _group.SyncRead((short)OPCDataSource.OPCCache, itemCount, ref serverHandles, out Array values, out errors, out object qualities, out object timeStamps); for (int i = 1; i < itemCount + 1; i++) { int serverHandle = Convert.ToInt32(serverHandles.GetValue(i)); OpcTag opcTag = null; lock (_tagServerHandleIndex) _tagServerHandleIndex.TryGetValue(serverHandle, out opcTag); if (opcTag == null) { continue; } object value = values.GetValue(i); int quality = Convert.ToInt32(((Array)qualities).GetValue(i)); DateTime timeStamp = Convert.ToDateTime(((Array)timeStamps).GetValue(i)); opcTag.Set(value, quality, timeStamp); } // 从设备异步读取一次 _group.AsyncRefresh((short)OPCDataSource.OPCDevice, 1001, out int cancelId); }
/* * public override bool AddAddress(int intAddress) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * * public override bool AddAddress(int intAddress, int intLength) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * * public override bool AddAddress(string[] strAddress) * { * try * { * foreach (string strAdd in strAddress) * { * this.Item_Add(strAdd); * } * return true; * } * catch (Exception ex) * { * throw ex; * } * } * * * * public override bool DelAddress(int intAddress) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * public override bool DelAddress(string[] strAddress) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * * * * public override int GetValue(int intAddress) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * public override int GetValue(int intAddress, int intLength) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * public override string GetValue(int intAddress, int intLength, int intRetrunLength) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * * public override int GetValue(string[] strAddress) * { * try * { * string strValue = string.Empty; * foreach (string strAdd in strAddress) * { * strValue += IntToHex(this.Item_Read(strAdd)); * } * * return int.Parse(strValue); * } * catch (Exception ex) * { * throw ex; * } * } * public override string GetValue(string[] strAddress, int intRetrunLength) * { * try * { * return string.Format("{0:D" + intRetrunLength.ToString() + "}", GetValue(strAddress)); * } * catch (Exception ex) * { * throw ex; * } * } * * public override int GetValueInt(string strAddress) * { * try * { * return this.Item_Read(strAddress); * } * catch (Exception ex) * { * throw ex; * } * } * * public override bool WriteOrder(int Address, int Value) * { * throw new Exception("구현 되지 않는 기능 입니다."); * } * * public override bool WriteOrder(string[] Address, int[] Value) * { * try * { * int i = 0; * string strValue = string.Empty; * foreach (string strAdd in Address) * { * this.Item_Write(strAdd, Value[i]); * i++; * } * * return true; * } * catch (Exception ex) * { * throw ex; * } * } */ #endregion /// <summary> /// opc연결 상태 체크 쓰레드.. /// </summary> /// <param name="obj"></param> private void tmrCheckOpcStatus(object obj) { if (bolChecking) { return; } try { bolChecking = true; int intCancelId = 0; if (OpcGrp == null) { if (bolOpcStatus) { return; } else { throw new Exception(string.Empty); } } if (OpcGrp.OPCItems.Count < 1) { //if (bolOpcStatus) return; //else // throw new Exception(string.Empty); } OpcGrp.AsyncRefresh(1, 2, out intCancelId); RsiOPCAuto.OPCItem item = OpcGrp.OPCItems.Item(1); //값을 정상 적으로 받지 못함.. if (item.Quality == 0) { throw new Exception(string.Empty); } if (this.Opc.ServerState == 1 && !this.bolOpcStatus) //접속 정상 { //연결 회복.. this.bolOpcStatus = true; this.ChConnection_Status(bolOpcStatus); } } catch (Exception ex) //(System.Runtime.InteropServices.ExternalException ex) { //접속이 끊겼을 경우 상태 체크도 에러 발생 하므로... Console.WriteLine(ex.ToString()); if (this.bolOpcStatus) { this.bolOpcStatus = false; this.ChConnection_Status(bolOpcStatus); } //접속재시도.. try { this.close(); this.Open(); this.Item_Add(); } catch { } } finally { bolChecking = false; } }