예제 #1
0
파일: Form1.cs 프로젝트: simple555a/OPC-2
        public Form1()
        {
            InitializeComponent();
            OpcCom.ServerEnumerator se = new ServerEnumerator();
            Opc.Server[]            Servers;
            String sErrFunc = "";

            Opc.ConnectData cd = new Opc.ConnectData(new System.Net.NetworkCredential());
            sErrFunc = "GetAvailableServers";
            Servers  = se.GetAvailableServers(Opc.Specification.COM_DA_20, "localhost", cd);
            Console.WriteLine(Servers[1].Name.ToString());


            Opc.URL url = new Opc.URL("opcda://localhost/" + Servers[0].Name);

            OpcCom.Factory fact = new OpcCom.Factory();
            server = new Opc.Da.Server(fact, null);
            server.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential()));
        }
예제 #2
0
        // Connect to OPC HDA server
        public bool Connect(string HostName, string ServerName)
        {
            var url = new Opc.URL();

            url.Scheme   = Opc.UrlScheme.HDA;
            url.HostName = HostName;
            url.Path     = ServerName;

            try {
                var fact = new OpcCom.Factory();
                if ((_OPCServer != null) && (!_OPCServer.IsConnected))
                {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "_OPCServer is disconnected, disposing object");
                    //Unfortunately, in case of lost connection simply calling .Connect() doesn't work :(
                    //Let's try to recreate the object from scratch
                    _OPCServer.Dispose();
                    _OPCServer = null;
                }

                if (_OPCServer == null)
                {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "_OPCServer is null, creating new object");
                    _OPCServer = new Opc.Hda.Server(fact, null);
                }

                if (!_OPCServer.IsConnected)
                {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "OPC server is disconnected, trying to connect");
                    _OPCServer.Connect(url, new Opc.ConnectData(new System.Net.NetworkCredential(), null));
                    if (!_OPCServer.IsConnected)
                    {
                        _trace.TraceEvent(TraceEventType.Error, 0, "Connection failed without exception: {0}", url.ToString());
                        return(false);
                    }
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "Succesfully connected to {0}, obj: {1}", url.ToString(), _OPCServer.GetHashCode().ToString());
                }
                try {
                    Status = _OPCServer.GetStatus();
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "OPC server status:\n" +
                                      "\tCurrentTime:     {0}\n" +
                                      "\tMaxReturnValues: {1}\n" +
                                      "\tProductVersion:  {2}\n" +
                                      "\tServerState:     {3}\n" +
                                      "\tStartTime:       {4}\n" +
                                      "\tStatusInfo:      {5}\n" +
                                      "\tVendorInfo:      {6}\n",
                                      Status.CurrentTime,
                                      Status.MaxReturnValues,
                                      Status.ProductVersion,
                                      Status.ServerState,
                                      Status.StartTime,
                                      Status.StatusInfo,
                                      Status.VendorInfo);
                } catch (Exception e) {
                    _trace.TraceEvent(TraceEventType.Warning, 0, "Can't get server status: {0}, {1}", url.ToString(), e.Message);
                }

                try {
                    _trace.TraceEvent(TraceEventType.Verbose, 0, "SupportedAggregates:");
                    SupportedAggregates = _OPCServer.GetAggregates();
                    foreach (Opc.Hda.Aggregate agg in SupportedAggregates)
                    {
                        _trace.TraceEvent(TraceEventType.Verbose, 0, "{0}\t{1}\t{2}", agg.ID, agg.Name, agg.Description);
                    }
                } catch (Exception e) {
                    _trace.TraceEvent(TraceEventType.Warning, 0, "Can't get server supported aggregates: {0}, {1}", url.ToString(), e.Message);
                }
            } catch (Exception e) {
                _trace.TraceEvent(TraceEventType.Error, 0, "Connection failed: {0}, {1}", url.ToString(), e.Message);
                return(false);
            }

            return(true);
        }