Exemplo n.º 1
0
        public void Subsribe(ItemIdentifier[] id)
        {
            int    clientSubscriptionHandle = 1;
            bool   active     = true;
            int    updateRate = 100;
            Single deadBand   = 0;

            int revisedUpdateRate;
            int serverSubscription;

            try
            {
                ReturnCode rc = _daServerMgt.Subscribe(clientSubscriptionHandle, active, updateRate, out revisedUpdateRate, deadBand, ref id, out serverSubscription);
                if (rc != ReturnCode.SUCCEEDED)
                {
                    Writer.writeAll("subscribe request failed for one or more items : " + rc.ToString());
                    Writer.writeAll("ItemID : " + id.ToString());

                    // Examine ResultID objects for detailed information.
                }
                else
                {
                    Writer.writeAll("Successfully subscribe to ItemID : " + id[0].ItemName);
                }
            }
            catch (Exception e)
            {
                Writer.writeAll("Handled Subscribe exception. Reason: " + e.ToString());
            }
        }
Exemplo n.º 2
0
        private int SubscribeData()
        {
            List <OPCTag> TagList = new List <OPCTag>();

            TagList.Add(new OPCTag()
            {
                Name = settings.TagRamp1,
                Type = OPCTagType.Char
            });
            TagList.Add(new OPCTag()
            {
                Name = settings.TagRandom1,
                Type = OPCTagType.Long
            });
            TagList.Add(new OPCTag()
            {
                Name = settings.TagSin1,
                Type = OPCTagType.Float
            });

            bool   active     = false;
            int    updateRate = UpdateRate;
            Single deadBand   = 0;
            int    revisedUpdateRate;

            int clientHandleUNQ = 0;
            int index           = 0;

            itemIdentifiers = new ItemIdentifier[TagList.Count];

            foreach (OPCTag tag in TagList)
            {
                itemIdentifiers[index] = new ItemIdentifier();
                itemIdentifiers[index].ClientHandle = clientHandleUNQ;
                itemIdentifiers[index].DataType     = Type.GetType(tag.Type.ToString());
                itemIdentifiers[index].ItemName     = tag.Name;
                index++;
                clientHandleUNQ++;
            }

            try
            {
                DAServer.Subscribe(clientSubscription, active, updateRate, out revisedUpdateRate, deadBand, ref itemIdentifiers, out serverSubscription);
                int faultCounter = 0;
                for (int itemIndex = 0; itemIndex < TagList.Count; itemIndex++)
                {
                    if (itemIdentifiers[itemIndex].ResultID.Succeeded == false)
                    {
                        faultCounter++;
                    }
                }
                return(TagList.Count - faultCounter);
            }
            catch { return(0); }
        }
Exemplo n.º 3
0
        public int SubscribeData(List <OPCTag> TAG)
        {
            bool   active     = false;
            int    updateRate = UpdateRate;
            Single deadBand   = 0;
            int    revisedUpdateRate;

            int clientHandleUNQ = 0;
            int index           = 0;

            itemIdentifiers = new ItemIdentifier[TAG.Count];

            if ((IDTAG != null) && (IDTAG.Count > 0))
            {
                IDTAG.Clear();
            }
            else
            {
                IDTAG = new Dictionary <int, string>();
            }

            foreach (OPCTag tag in TAG)
            {
                itemIdentifiers[index] = new ItemIdentifier();
                itemIdentifiers[index].ClientHandle = clientHandleUNQ;
                itemIdentifiers[index].DataType     = Type.GetType(tag.Type.ToString());
                itemIdentifiers[index].ItemName     = tag.Name;
                IDTAG.Add(clientHandleUNQ, tag.Name);
                index++;
                clientHandleUNQ++;
            }

            try
            {
                DAServer.Subscribe(clientSubscription, active, updateRate, out revisedUpdateRate, deadBand, ref itemIdentifiers, out serverSubscription);
                int faultCounter = 0;
                for (int itemIndex = 0; itemIndex < IDTAG.Count; itemIndex++)
                {
                    if (itemIdentifiers[itemIndex].ResultID.Succeeded == false)
                    {
                        faultCounter++;
                    }
                }
                return(IDTAG.Count - faultCounter);
            }
            catch { return(0); }
        }
Exemplo n.º 4
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.º 5
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.º 6
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();
            }
        }