Exemplo n.º 1
0
        public static Opc.Hda.Server ServerHdaConnect()
        {
            var tagsList = new Tags();

            //Задаем расположение серверов
            foreach (var tag in tagsList.GetMassurm())
            {
                switch (tag.Name)
                {
                case "MasterIpHda":
                    MasterIpHda = tag.Value;
                    break;

                case "SlaveIpHda":
                    SlaveIpHda = tag.Value;
                    break;

                case "OpcHdaId":
                    OpcHdaId = tag.Value;
                    break;
                }
            }
            //const string masterIpHda = "localhost";
            //const string slaveIpHda = "localhost";
            //const string opcHdaId = "Infinity.OPCHDAServer";
            //Соединение с сервером
            var url       = new Opc.URL("opchda://" + MasterIpHda + "/" + OpcHdaId);
            var serverHda = new Opc.Hda.Server(new OpcCom.Factory(), url);

            try
            {
                serverHda.Connect();
            }
            catch
            {
                url       = new Opc.URL("opchda://" + SlaveIpHda + "/" + OpcHdaId);
                serverHda = new Opc.Hda.Server(new OpcCom.Factory(), url);
                try
                {
                    serverHda.Connect();
                }
                catch (Exception)
                {
                    MessageBox.Show(@"Отсутствует лицензия или проверьте подключения к серверу C:\Tag.ini", @"Ошибка подключения сервера", MessageBoxButton.OK, MessageBoxImage.Error);

                    Environment.Exit(0);
                }
            }
            // System.Threading.Thread.Sleep(1500);
            return(serverHda);
        }
Exemplo n.º 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);
        }