public static int GetOpcServerAllItem(string opcSerHostIp, Guid opcSerGuid, out List <DriveNodeEntity> driveNotes) { int rtc = -1; driveNotes = null; try { OpcServer opcSer = new OpcServer(); rtc = opcSer.Connect(new Host(opcSerHostIp), opcSerGuid); if (!HRESULTS.Succeeded(rtc)) { return(rtc); } //获取组 if (GetOPCServerStatus(opcSer, out rtc)) { return(GetOpcServerAllItem(opcSer, out driveNotes)); } } catch (Exception err) { NLogHelper.ExceptionInfo(err, "GetDriveNodes error :{0}", err.Message); } return(rtc); }
public bool Start() { try { bool bRet = InitOPCService(); if (!bRet) { return(false); } if (null == opcSerNodes || null == allOPCItemNodes) { NLogHelper.DefalutError("OPCBUSService.Start error,未配置opc服务"); return(false); } opcSer.Connect(host, guid); syncIOGroup = new SyncIOGroup(opcSer); refreshEventHandler = new RefreshEventHandler(RefreshEvent); refreshGroup = new RefreshGroup(opcSer, 10, refreshEventHandler); foreach (DriveNodeEntity item in allOPCItemNodes) { int iRet = refreshGroup.Add(item.nodeName); if (HRESULTS.Succeeded(iRet)) { Console.WriteLine(" true " + iRet); } } return(true); } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "Start StartOPCService exception: {0}", ex.Message); return(false); } }
public int GetNodeValue(string nodeStrGuid, out object nodeValue) { int rtc = -1; nodeValue = null; try { OPCDATASOURCE opcDataSource = OPCDATASOURCE.OPC_DS_DEVICE; OPCItemState opcItemState = new OPCItemState(); DriveNodeEntity driveNode = allOPCItemNodes.Find(p => nodeStrGuid.Equals(p.nodeId)); ItemDef itemdef = syncIOGroup.Item(driveNode.nodeName); rtc = syncIOGroup.Read(opcDataSource, itemdef, out opcItemState); if (HRESULTS.Succeeded(rtc)) { nodeValue = opcItemState.DataValue; //txtItemQuality.Text = syncIOGroup.GetQualityString(opcItemState.Quality); //txtTimeStamp.Text = DateTime.FromFileTime(opcItemState.TimeStamp).ToString(); } } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "GetNodeValue param:'{1}' ,exception:{0}", ex.Message, nodeStrGuid); } return(rtc); }
public static int GetOpcAllItemByNodeRoot(DriveNodeEntity nodeRoot, out List <DriveNodeEntity> listItemNodes) { listItemNodes = null; try { if (null == nodeRoot) { return(1); } listItemNodes = new List <DriveNodeEntity>(); foreach (DriveNodeEntity group in nodeRoot.childNode) { foreach (DriveNodeEntity item in group.childNode) { listItemNodes.Add(item); } } return(0); } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "GetOpcAllItemByNodeRoot exception:{0} ", ex.Message); return(-1); } }
public static List <Tuple <string, Guid> > GetOPCServiceList(string opcSerHostIp) { try { Host host = new Host(opcSerHostIp); OpcServerBrowser opcServerBrowser = new OpcServerBrowser(host); string[] servers; Guid[] serverGuids; opcServerBrowser.GetServerList(out servers, out serverGuids); if (null != servers) { List <Tuple <string, Guid> > serverList = new List <Tuple <string, Guid> >(); for (int i = 0; i < servers.Length; i++) { serverList.Add(new Tuple <string, Guid>(servers[i], serverGuids[i])); } return(serverList); } } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "GetOPCServiceList error :{0}", ex.Message); } return(null); }
public static int ConnectOPCServer(ref OpcServer opcServer, Host opcSerHost, Guid opcSerGuid) { int rtc = -1; try { if (null == opcServer) { opcServer = new OpcServer(); } rtc = opcServer.Connect(opcSerHost, opcSerGuid); } catch (Exception err) { NLogHelper.ExceptionInfo(err, "ConnectOPCServer error :{0}", err.Message); } return(rtc); }
private bool InitOPCService() { try { int iRet = OPCHelper.GetOpcAllItemByNodeRoot(opcSerNodes, out allOPCItemNodes); if (0 != iRet) { NLogHelper.DefalutError("GetOpcAllItemByNodeRoot 获取opc服务设备项失败:{0}", iRet.ToString()); return(false); } host = new Host(opcSerNodes.serviceIp); opcSer = new OpcServer(); guid = new Guid(opcSerNodes.nodeStrGuid); return(true); } catch (Exception ex) { NLogHelper.ExceptionInfo(ex, "InitOPCService exception: {0}", ex.Message); } return(false); }
public static bool GetOPCServerStatus(OpcServer opcSer, out int opcSerStatus) { bool theGetServerStatusSucceed = false; opcSerStatus = -1; SERVERSTATUS theServerStatus; try { opcSerStatus = opcSer.GetStatus(out theServerStatus); if (HRESULTS.Succeeded(opcSerStatus)) { theGetServerStatusSucceed = true; } } catch (Exception err) { NLogHelper.ExceptionInfo(err, "GetOPCServerStatus error :{0}", err.Message); } return(theGetServerStatusSucceed); }
public static Dictionary <string, Guid> GetServersNameAndGuid(string hostNameOrAddress) { string[] servers = null; Guid[] guids = null; Dictionary <string, Guid> serverAndGuidDic = new Dictionary <string, Guid>(); try { IPHostEntry ip_host = Dns.GetHostEntry(hostNameOrAddress); Host host = new Host(ip_host.HostName); OpcServerBrowser browser = new OpcServerBrowser(host); browser.GetServerList(out servers, out guids); for (int i = 0; i < servers.Length; i++) { serverAndGuidDic[servers[i]] = guids[i]; } } catch (Exception err) { NLogHelper.ExceptionInfo(err, "GetServersNameAndGuid error :{0}", err.Message); } return(serverAndGuidDic); }