Exemplo n.º 1
0
        // Функция подключения/отключения к/от серверу(а)
        public void ConnectOPCServer(bool connectToOPC)
        {
            if (connectToOPC)
            {
                connectInfo.LocalId                   = "en";
                connectInfo.KeepAliveTime             = KeepAliveTime;
                connectInfo.RetryAfterConnectionError = RetryAfterConnectionError;
                connectInfo.RetryInitialConnection    = RetryInitialConnection;
                connectInfo.ClientName                = ClientName;

                try
                {
                    if (DAServer.IsConnected == false)
                    {
                        DAServer.Connect(URL, clientHandle, ref connectInfo, out isOPCConnectionFailed);
                    }
                    int tagCount = SubscribeData();
                    ModifySubscription(true);
                    MessageBox.Show(String.Format("Succesfully connected to {0} tags", tagCount), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch { }
            }
            else
            {
                try
                {
                    UnSubscribeData();
                    if (DAServer.IsConnected == true)
                    {
                        DAServer.Disconnect();
                    }
                }
                catch { }
            }
        }
Exemplo n.º 2
0
        public void ConnectOPCServer(bool connectToOPC)
        {
            if (connectToOPC)
            {
                connectInfo.LocalId                   = "en";
                connectInfo.KeepAliveTime             = KeepAliveTime;
                connectInfo.RetryAfterConnectionError = RetryAfterConnectionError;
                connectInfo.RetryInitialConnection    = RetryInitialConnection;
                connectInfo.ClientName                = ClientName;

                try
                {
                    if (DAServer.IsConnected == false)
                    {
                        DAServer.Connect(URL, clientHandle, ref connectInfo, out isOPCConnectionFiled);
                    }
                }
                catch { }
            }
            else
            {
                try
                {
                    if (DAServer.IsConnected == true)
                    {
                        DAServer.Disconnect();
                    }
                }
                catch { }
            }
        }
Exemplo n.º 3
0
        public static bool connect()
        {
            //initialize the connection info class
            DAServer = new DaServerMgt();
            connectInfo = new ConnectInfo();

            string url = "opcda://localhost/Kepware.KEPServerEX.V5/{B3AF0BF6-4C0C-4804-A122-6F3B160F4397}";

            int clientHandle = 1;

            //initialize the connect info object data
            connectInfo.LocalId = "en";
            connectInfo.KeepAliveTime = 5000;
            connectInfo.RetryAfterConnectionError = true;
            connectInfo.RetryInitialConnection = true;
            connectInfo.ClientName = "IDCServer";

            bool connectFailed = false;

            try
            {
                logger.Info("Connecting to DAServer: {0}, {1}, {2}", url, clientHandle, connectInfo.ToString());
                DAServer.Connect(url, clientHandle, ref connectInfo, out connectFailed);

                // Update our connection status
                if (!connectFailed)
                {
                    logger.Info("Connection Succeeded");
                    return true;
                }
                else
                {
                    logger.Error("Connection Failed");
                    return false;
                }
            }
            catch (Exception e)
            {
                logger.Error("Handled connect exception. Reason: " + e.Message);
                throw e;
            }
        }
Exemplo n.º 4
0
        public bool Connect()
        {
            bool connect = false;

            try
            {
                String      s  = CultureInfo.CurrentCulture.Name;
                ConnectInfo ci = new ConnectInfo();
                ci.ClientName                = "Kepware.KEPServerEX.V6";
                ci.LocalId                   = s;// "en";
                ci.KeepAliveTime             = 10000;
                ci.RetryAfterConnectionError = true;
                ci.RetryInitialConnection    = true;


                ci.RetryInitialConnection = true;
                _daServerMgt = new DaServerMgt();

                bool   connectFailed = false;
                int    clientHandle  = 1;
                string url           = getOPCServeurURL();

                _daServerMgt.Connect(url, clientHandle, ref ci, out connectFailed);

                if (!connectFailed)
                {
                    connect = true;
                }
                else
                {
                }
            }
            catch (Exception e)
            {
                Writer.writeAll("Handled Connect exception. Reason: " + e.ToString());
                connect = false;
            }
            return(connect);
        }
Exemplo n.º 5
0
        public void Connect(/*object sender, EventArgs e*/)
        {
            connectInfo.LocalId                   = "en";
            connectInfo.KeepAliveTime             = 5000;
            connectInfo.RetryAfterConnectionError = true;
            connectInfo.RetryInitialConnection    = true;
            //connectInfo.ClientName = "Demo ClientAceC-Sharp DesktopApplication";
            bool   connectFailed;
            string url          = "opcda://127.0.0.1/Kepware.KEPServerEX.V6/{7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729}";
            int    clientHandle = 1;

            // Connessione al server
            try
            {
                daServerMgt.Connect(url, clientHandle, ref connectInfo, out connectFailed);
                if (connectFailed)
                {
                    Console.WriteLine("Connect failed" + Environment.NewLine);
                }
                else
                {
                    Console.WriteLine("Connected to Server " + url + " Succeeded." + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                // ab edit: aggiungiamo ilcontesto per capire l'errore
                Console.WriteLine($"Connect exeception: {ex.ToString()}");
            }

            InitMidnightTimer();

            InitPlantState();

            // Tag a cui mi voglio sottoscrivere
            ItemIdentifier[] items = new ItemIdentifier[1];
            items[0] = new ItemIdentifier
            {
                ItemName     = "its-iot-device.Device1.PlantStatus",
                ClientHandle = "PlantStatus"
            };

            /*items[1] = new ItemIdentifier
             * {
             *  ItemName = "its-iot-device.Device1.PieceCounter",
             *  ClientHandle = "PieceCounter"
             * };*/
            /*items[2] = new ItemIdentifier
             * {
             *  ItemName = "Simulation Examples.Functions.Ramp1",
             *  ClientHandle = "Ramp1"
             * };*/

            int        serverSubscription;
            ReturnCode returnCode;

            // Parametri di sottoscrizione agli eventi
            int   clientSubscription = 1;
            bool  active             = true;
            int   updateRate         = 1000;
            int   revisedUpdateRate;
            float deadband = 0;

            try
            {
                // Sottoscrizione agli eventi change data
                returnCode = daServerMgt.Subscribe(clientSubscription,
                                                   active,
                                                   updateRate,
                                                   out revisedUpdateRate,
                                                   deadband,
                                                   ref items,
                                                   out serverSubscription);
            }
            catch (Exception ex)
            {
                // ab edit: aggiungiamo ilcontesto per capire l'errore
                Console.WriteLine($"Connect.subscribe exeception: {ex.ToString()}");
            }
        }
Exemplo n.º 6
0
        private void Initialize()
        {
            connectInfo.LocalId                   = "en";
            connectInfo.KeepAliveTime             = 5000;
            connectInfo.RetryAfterConnectionError = true;
            connectInfo.RetryInitialConnection    = true;
            connectInfo.ClientName                = "ClientAce Client";

            bool   connectFailed;
            string url          = "opcda://<computer name>/Kepware.KEPServerEX.V6";
            int    clientHandle = 1;

            // Connect to server
            try
            {
                daServerMgt.Connect(url, clientHandle, ref connectInfo, out connectFailed);
                if (connectFailed)
                {
                    Console.WriteLine("Failed to connect to server. @ " + DateTime.Now);
                }
                else
                {
                    Console.WriteLine("Connected to Server " + url + " Succeeded." + DateTime.Now);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Failed to connect to server." + e + " @ " + DateTime.Now);
            }


            // Tags
            ItemIdentifier[] itemIdentifiers = new ItemIdentifier[2];
            itemIdentifiers[0]              = new ItemIdentifier();
            itemIdentifiers[0].ItemName     = "Channel1.Device1.Tag1";
            itemIdentifiers[0].ClientHandle = 1; // Assign unique handle

            itemIdentifiers[1]              = new ItemIdentifier();
            itemIdentifiers[1].ItemName     = "Simulation Examples.Functions.Ramp1";
            itemIdentifiers[1].ClientHandle = 2; // Assign unique handle

            int        serverSubscription;
            ReturnCode returnCode;

            // Event subscription parameters
            int   clientSubscription = 1;
            bool  active             = true;
            int   updateRate         = 1000;
            int   revisedUpdateRate;
            float deadband = 0;

            try
            {
                //Subscribe to tags
                returnCode = daServerMgt.Subscribe(clientSubscription,
                                                   active,
                                                   updateRate,
                                                   out revisedUpdateRate,
                                                   deadband,
                                                   ref itemIdentifiers,
                                                   out serverSubscription);

                if (returnCode == ReturnCode.SUCCEEDED)
                {
                    Console.WriteLine("Successfully subscribed. @ " + DateTime.Now);
                }
                else
                {
                    Console.WriteLine("Subscritpion failed. @ " + DateTime.Now);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Subscritpion failed." + e + " @ " + DateTime.Now);
            }
        }
Exemplo n.º 7
0
        private void Form1_Load(object sender, EventArgs e)
        {
            connectInfo.LocalId                   = "en";
            connectInfo.KeepAliveTime             = 5000;
            connectInfo.RetryAfterConnectionError = true;
            connectInfo.RetryInitialConnection    = true;
            connectInfo.ClientName                = "Demo ClientAceC-Sharp DesktopApplication";
            bool   connectFailed;
            string url          = "opcda://127.0.0.1/Kepware.KEPServerEX.V6/{7BC0CC8E-482C-47CA-ABDC-0FE7F9C6E729}";
            int    clientHandle = 1;

            // Connessione al server
            try
            {
                daServerMgt.Connect(url, clientHandle, ref connectInfo, out connectFailed);
                if (connectFailed)
                {
                    debugBox.Text = "Connect failed" + Environment.NewLine;
                }
                else
                {
                    debugBox.Text = "Connected to Server " + url + " Succeeded." + Environment.NewLine;
                }
            }
            catch (Exception ex)
            {
                debugBox.Text = ex.ToString();
            }

            AggiornaDati();

            // Tag a cui mi voglio sottoscrivere
            ItemIdentifier[] items = new ItemIdentifier[4];
            items[0] = new ItemIdentifier
            {
                ItemName     = "its-iot-device.Device1.PlantStatus",
                ClientHandle = "PlantStatus"
            };
            items[1] = new ItemIdentifier
            {
                ItemName     = "Simulation Examples.Functions.Ramp1",
                ClientHandle = "Ramp1"
            };
            items[2] = new ItemIdentifier
            {
                ItemName     = "its-iot-device.Device1.PiecesProducted",
                ClientHandle = "PiecesProducted"
            };
            items[3] = new ItemIdentifier
            {
                ItemName     = "its-iot-device.Device1.PiecesDischarged",
                ClientHandle = "PiecesDischarged"
            };

            int        serverSubscription;
            ReturnCode returnCode;

            // Parametri di sottoscrizione agli eventi
            int   clientSubscription = 1;
            bool  active             = true;
            int   updateRate         = 1000;
            int   revisedUpdateRate;
            float deadband = 0;

            try
            {
                // Sottoscrizione agli eventi change data
                returnCode = daServerMgt.Subscribe(clientSubscription,
                                                   active,
                                                   updateRate,
                                                   out revisedUpdateRate,
                                                   deadband,
                                                   ref items,
                                                   out serverSubscription);
            }
            catch (Exception ex)
            {
                debugBox.Text = ex.ToString();
            }
        }