private void ReadTagValueFromKepServer( string kepServerName, TDeviceTagValueRWReqBodyTags read, out int errCode, out string errText) { errCode = 0; errText = ""; for (int i = 0; i < read.Tags.Count; i++) { TIRAPOPCTag opcTag = TIRAPOPCDevices.Instance.FindOPCTagItem( string.Format( "{0}.{1}", kepServerName, read.Tags[i].TagName)); if (opcTag == null) { errCode = 903341; errText = string.Format("读取失败:标签[{0}]在 KepServer 中未定义", read.Tags[i].TagName); return; } else { content.Response.ReadTags.Add( new TDeviceTagValueRWRspBodyTag() { TagName = read.Tags[i].TagName, TagValue = opcTag.TagValue, }); } } }
private void WriteTagValueToKepServer( string kepServerName, TDeviceTagValueRWReqBodyTags write, out int errCode, out string errText) { errCode = 0; errText = ""; for (int i = 0; i < write.Tags.Count; i++) { TIRAPOPCTag opcTag = TIRAPOPCDevices.Instance.FindOPCTagItem( string.Format( "{0}.{1}", kepServerName, write.Tags[i].TagName)); if (opcTag == null) { errCode = 903341; errText = string.Format("回写失败:标签[{0}]在 KepServer 中未定义", write.Tags[i].TagName); return; } else { try { opcTag.WriteTagValueBack(write.Tags[i].TagValue, out errCode, out errText); if (errCode != 0) { errCode = 903341; errText = string.Format( "标签[{0}]回写失败:[{1}]", write.Tags[i].TagName, errText); return; } } catch (Exception error) { errCode = 903341; errText = string.Format( "标签[{0}]回写失败:[{1}]", write.Tags[i].TagName, error.Message); return; } } } }
public bool Init(string ipKepServer, string nameKepServer) { if (kepServerConnected) { kepServer.Disconnect(); } this.ipKepServer = ipKepServer; this.nameKepServer = nameKepServer; Debug.WriteLine( string.Format( "连接远程服务器[{0}][{1}]......", nameKepServer, ipKepServer)); try { kepServer.Connect(nameKepServer, ipKepServer); Debug.WriteLine("远程服务器连接成功"); } catch (Exception error) { kepServerConnected = false; Debug.WriteLine( string.Format( "连接远程服务器[{0}]出现错误:[{1}]", ipKepServer, error.Message)); return(false); } // 连接了远程服务器后,枚举中该服务器中所有的节点 OPCBrowser browser = kepServer.CreateBrowser(); browser.ShowBranches(); browser.ShowLeafs(true); foreach (object tag in browser) { string tagName = tag.ToString(); if (!tagName.Contains("._")) { tags.Add( new TIRAPOPCTagItem() { TagName = tagName, }); } } // 建立数据变化的侦听 kepGroups = kepServer.OPCGroups; kepGroups.RemoveAll(); kepGroup = kepGroups.Add("OPCLISTENERGROUP"); kepGroups.DefaultGroupIsActive = true; kepGroups.DefaultGroupDeadband = 0; kepGroup.UpdateRate = 250; kepGroup.IsActive = true; kepGroup.IsSubscribed = true; kepGroup.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange); kepGroup.AsyncWriteComplete += new DIOPCGroupEvent_AsyncWriteCompleteEventHandler(KepGroup_AsyncWriteComplete); kepItems = kepGroup.OPCItems; for (int i = 0; i < tags.Count; i++) { //if (!tags[i].TagName.Contains("DATA_READY")) //{ tags[i].ServerHandle = kepItems.AddItem(tags[i].TagName, i + 1).ServerHandle; TIRAPOPCTag tag = TIRAPOPCDevices.Instance.FindOPCTagItem( string.Format( "{0}.{1}", nameKepServer, tags[i].TagName)); if (tag != null) { tag.ServerHandle = tags[i].ServerHandle; tag.WriteTagValueMethod = WriteTagValue; Debug.WriteLine( string.Format( "TagName:[{0}], ServerHandle:[{1}]", tag.TagName, tag.ServerHandle)); } //} } //writter.Init(ipKepServer, nameKepServer); return(true); }
private void Settle() { try { string fullTagName = string.Format( "{0}.{1}", item.KepServerName, item.TagName); Debug.WriteLine(string.Format("TagName:[{0}]", fullTagName)); TIRAPOPCTag tag = TIRAPOPCDevices.Instance.FindOPCTagItem(fullTagName); if (tag != null) { Console.WriteLine( string.Format( "[{0}]收到消息事件:TagName[{1}],Value[{2}],TimeStamp[{3}]", item.ReceiveTime.ToString("HH:mm:ss.fff"), item.TagName, item.Value, item.TimeStamp)); Console.WriteLine( string.Format( "[{0}][线程 #{1}]处理 OPC 消息:TagName:[{2}],Value:[{3}]", DateTime.Now.ToString("HH:mm:ss.fff"), threadID, item.TagName, item.Value)); //WriteLog.Instance.Write( // guid, // string.Format( // "[线程 #{0}]处理 OPC 消息:TagName:[{1}],Value:[{2}]", // threadID, // item.TagName, // item.Value)); tag.SetTagValue(item.Value, item.TimeStamp); } else { Debug.WriteLine( string.Format( "KepTag:未注册[{0}]", fullTagName)); } } catch (Exception error) { string errCode = ""; string errText = ""; if (error.Data["ErrCode"] != null) { errCode = error.Data["ErrCode"].ToString(); } if (error.Data["ErrText"] != null) { errText = error.Data["ErrText"].ToString(); } else { errText = error.Message; } Debug.WriteLine( string.Format( "[线程 #{0}]处理消息时出错:[({1}){2}]", threadID, errCode, errText)); } }
private void Settle() { Debug.WriteLine( string.Format( "[线程 #{0}]开始处理 OPC 出队消息", threadID)); TIRAPOPCTagValueItem value = TIRAPOPCTagValueItemQueue.Instance.Get(); while (!needStopped || value != null) { if (value != null) { Debug.WriteLine( string.Format( "[线程 #{0}]处理 OPC 出队消息:TagName:[{1}],Value:[{2}]", threadID, value.TagName, value.Value)); try { string fullTagName = string.Format( "{0}.{1}", value.KepServerName, value.TagName); Debug.WriteLine(string.Format("TagName:[{0}]", fullTagName)); TIRAPOPCTag tag = TIRAPOPCDevices.Instance.FindOPCTagItem(fullTagName); if (tag != null) { Console.WriteLine( string.Format( "[线程 #{0}]处理 OPC 出队消息:TagName:[{1}],Value:[{2}]", threadID, value.TagName, value.Value)); tag.SetTagValue(value.Value, value.TimeStamp); } else { Debug.WriteLine( string.Format( "KepTag:未注册[{0}]", fullTagName)); } } catch (Exception error) { string errCode = ""; string errText = ""; if (error.Data["ErrCode"] != null) { errCode = error.Data["ErrCode"].ToString(); } if (error.Data["ErrText"] != null) { errText = error.Data["ErrText"].ToString(); } else { errText = error.Message; } Debug.WriteLine( string.Format( "[线程 #{0}]处理消息时出错:[({1}){2}]", threadID, errCode, errText)); } } Thread.Sleep(10); value = TIRAPOPCTagValueItemQueue.Instance.Get(); } Debug.WriteLine("[线程 #[{0}]已停止。", threadID); }
private void WaittingFor( string kepServerName, TDeviceTagValueRWReqBodyFlagTags flags, out int errCode, out string errText) { errCode = 0; errText = ""; List <TIRAPOPCTag> opcTags = new List <TIRAPOPCTag>(); for (int i = 0; i < flags.Tags.Count; i++) { TIRAPOPCTag opcTag = TIRAPOPCDevices.Instance.FindOPCTagItem( string.Format( "{0}.{1}", kepServerName, flags.Tags[i].TagName)); if (opcTag == null) { errCode = 903341; errText = string.Format( "标识标签错误:标签[{0}]在 KepServer 中未定义", flags.Tags[i].TagName); return; } else { opcTags.Add(opcTag); } } DateTime start = DateTime.Now; TimeSpan span = DateTime.Now - start; bool allAbove = true; while (span.TotalSeconds <= flags.TimeOut) { allAbove = true; for (int i = 0; i < opcTags.Count; i++) { if (opcTags[i].TagValue != flags.Tags[i].TagValue) { allAbove = false; break; } } if (allAbove) { break; } Thread.Sleep(300); span = DateTime.Now - start; } if (!allAbove) { errCode = 903342; errText = string.Format( "在[{0}]秒内,未满足规定的读取条件。", flags.TimeOut); } }