Exemplo n.º 1
0
        static void Main(string[] args)
        {
            string progID = args.Length > 0 ? args[0] : "HWHsc.OPCServer";
            string itemID = args.Length > 1 ? args[1] : "/ASSETS.Guid";
            string propID = args.Length > 2 ? args[2] : "2";

            OpcServer opcServer = new OpcServer();

            opcServer.Connect(progID);
            System.Threading.Thread.Sleep(1000); // we are faster than some servers!

            OpcProperty[] props = opcServer.QueryAvailableProperties(itemID);
            for (int i = 0; i < props.Length; i++)
            {
                Console.WriteLine($"{props[i].PropertyID} - {props[i].Description}");
            }
            OpcPropertyData[] data = opcServer.GetItemProperties(itemID, new int[] { int.Parse(propID) });

            for (int i = 0; i < data.Length; i++)
            {
                if (data[i].Error == HRESULTS.S_OK)
                {
                    Console.WriteLine(data[i].Data);
                }
                else
                {
                    Console.WriteLine("!ERROR:{0}", data[i].Error);
                }
            }

            opcServer.Disconnect();
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            string progID = args.Length > 0 ? args[0] : "Kepware.KEPServerEX.V5";
            string itemID = args.Length > 1 ? args[1] : "Simulation Examples.Functions.Ramp1";

            OpcServer opcServer = new OpcServer();

            opcServer.Connect(progID);
            System.Threading.Thread.Sleep(500); // we are faster than some servers!

            OpcProperty[] props   = opcServer.QueryAvailableProperties(itemID);
            int[]         propIDs = new int[props.Length];
            for (int i = 0; i < props.Length; i++)
            {
                propIDs[i] = props[i].PropertyID;
            }
            OpcPropertyData[] data = opcServer.GetItemProperties(itemID, propIDs);

            for (int i = 0; i < props.Length; i++)
            {
                Console.Write(" {0}: '{1}' ({2}) = ", props[i].PropertyID, props[i].Description, Extensions.VarEnumToString(props[i].DataType));
                if (data[i].Error == HRESULTS.S_OK)
                {
                    Console.WriteLine(data[i].Data);
                }
                else
                {
                    Console.WriteLine("!ERROR:{0}", data[i].Error);
                }
            }

            opcServer.Disconnect();
        }
Exemplo n.º 3
0
        public void TestErr_GetItemProperties02()
        {
            //testing --
            OpcServer opcServer         = new OpcServer();
            Accessor  opcServerAccessor = ReflectionAccessor.Wrap(opcServer);

            opcServerAccessor.SetField("ifServer", new OPCServerComClass());
            opcServerAccessor.SetField("ifItmProps", ((IOPCBrowseServerAddressSpace)opcServerAccessor.GetField("ifServer")));
            int[]             propertyIDs = new int[1];
            OPCPropertyData[] propertiesData;
            //Test Procedure Call
            bool rRes = opcServer.GetItemProperties("ItemId1", propertyIDs, out propertiesData);
            //Post Condition Check
        }
Exemplo n.º 4
0
        public void ListAllProperties(ref OpcServer theSrv, string itemid)
        {
            listPropsView.Items.Clear();

            string[]          istrs = new string[5];
            OPCProperty[]     props;
            OPCPropertyData[] propdata;
            int[]             propertyIDs = new int[1];
            OPCPropertyItem[] propitm;

            try
            {
                theSrv.QueryAvailableProperties(itemid, out props);
                if (props == null)
                {
                    return;
                }

                foreach (OPCProperty p in props)
                {
                    istrs[0] = p.PropertyID.ToString();
                    istrs[1] = p.Description;
                    istrs[2] = DUMMY_VARIANT.VarEnumToString(p.DataType);
                    istrs[3] = "";
                    istrs[4] = "";

                    propertyIDs[0] = p.PropertyID;
                    theSrv.GetItemProperties(itemid, propertyIDs, out propdata);
                    if (propdata != null)
                    {
                        if (propdata[0].Error != HRESULTS.S_OK)
                        {
                            istrs[3] = "!Error 0x" + propdata[0].Error.ToString("X");
                        }
                        else
                        {
                            istrs[3] = propdata[0].Data.ToString();
                        }
                    }

                    if (p.PropertyID > 6)
                    {
                        theSrv.LookupItemIDs(itemid, propertyIDs, out propitm);
                        if (propitm != null)
                        {
                            if (propitm[0].Error != HRESULTS.S_OK)
                            {
                                istrs[4] = "!Error 0x" + propitm[0].Error.ToString("X");
                            }
                            else
                            {
                                istrs[4] = propitm[0].newItemID;
                            }
                        }
                    }

                    listPropsView.Items.Add(new ListViewItem(istrs));
                }
            }
            catch (COMException)
            {
                MessageBox.Show(this, "QueryAvailableProperties failed!", "Item Properties", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // preselect top item in ListView
            listPropsView.Items[0].Selected = true;
        }
Exemplo n.º 5
0
        public List <OpcParameter> ProcessOpcNodeParams(ArrayList lst)
        {
            if (lst == null)
            {
                throw new ArgumentNullException("lst");
            }

            List <OpcParameter> result = new List <OpcParameter>();

            _theSrv.Browse(OPCBROWSETYPE.OPC_LEAF, out lst);

            if (lst.Count < 1)
            {
                return(result);
            }

            string[] istrs = new string[5];

            OPCProperty[] props;

            OPCPropertyData[] propdata;

            int[] propertyIDs = new int[1];

            for (int i = 0; i < lst.Count; i++)
            {
                lst[i] = string.Format("PLC.PLC.{0}", lst[i]);
            }

            // enum+add all item names to ListView
            //string[] itemstrings = new string[2];
            //перебор списка имён переменных
            foreach (string item in lst)
            {
                OpcParameter parameterData = new OpcParameter();
                string       i             = item;
                parameterData.ParameterName = i;
                //itemstrings[0] = item;
                //получение списка свойств переменной
                _theSrv.QueryAvailableProperties(i, out props);

                if (props == null)
                {
                    continue;
                }

                // перебираем все свойства
                foreach (OPCProperty p in props)
                {
                    // PropertyID == 2 - это свойство в котором хранится значение переменной
                    if (p.PropertyID == 2)
                    {
                        propertyIDs[0] = p.PropertyID;
                        //получение свойства
                        _theSrv.GetItemProperties(i, propertyIDs, out propdata);
                        parameterData.ParameterType  = propdata[0].Data.GetType();
                        parameterData.ParameterValue = propdata[0].Data;
                    }
                }

                result.Add(parameterData);
            }

            return(result);
        }