Example #1
0
        private bool CreateGroup()
        {
            try
            {
                // add our only working group
                _theGrp = _theSrv.AddGroup("OPCdotNET-Group", true, 500);

                //При создании группы тут читаем данные по всем тегам с сервера.
                OPCItemDef[] i = new OPCItemDef[12];
                i[0]  = new OPCItemDef();
                i[1]  = new OPCItemDef();
                i[2]  = new OPCItemDef();
                i[3]  = new OPCItemDef();
                i[4]  = new OPCItemDef();
                i[5]  = new OPCItemDef();
                i[6]  = new OPCItemDef();
                i[7]  = new OPCItemDef();
                i[8]  = new OPCItemDef();
                i[9]  = new OPCItemDef();
                i[10] = new OPCItemDef();
                i[11] = new OPCItemDef();


                i[0].ItemID  = OpcConsts.DispPress;
                i[1].ItemID  = OpcConsts.DlinaSopr;
                i[2].ItemID  = OpcConsts.Bits;
                i[3].ItemID  = OpcConsts.PosadkaKol;
                i[4].ItemID  = OpcConsts.SpeedPress;
                i[5].ItemID  = OpcConsts.Instrument;
                i[6].ItemID  = OpcConsts.BlinL;
                i[7].ItemID  = OpcConsts.BlinR;
                i[8].ItemID  = OpcConsts.DispPress1;
                i[9].ItemID  = OpcConsts.DispPress2;
                i[10].ItemID = OpcConsts.DispPress3;
                i[11].ItemID = OpcConsts.ShowGraph;


                OPCItemResult[] resI = new OPCItemResult[4];
                _theGrp.AddItems(i, out resI);

                // add event handler for data changes
                _theGrp.DataChanged    += theGrp_DataChange;
                _theGrp.WriteCompleted += theGrp_WriteComplete;
            }
            catch (COMException e)
            {
                throw e;
            }
            return(true);
        }
Example #2
0
        private PLCCommand()
        {
            try
            {
                _opcSrv.Connect(StrSrvName);
                for (int i = 0; i < _arrGrps.Length; i++)
                {
                    string   strGrp = _arrGrps[i];
                    OpcGroup grp    = _opcSrv.AddGroup(strGrp, true, 9600);
                    grp.UpdateRate      = 100;
                    grp.Active          = true;
                    grp.PercentDeadband = 0;

                    _opcGrpsDic.Add(strGrp, grp);

                    string[] arrCurrentItems = _arrItems[i].Split(',');

                    OPCItemDef[]    opcItems   = new OPCItemDef[arrCurrentItems.Length];
                    OPCItemResult[] opcResults = new OPCItemResult[arrCurrentItems.Length];
                    for (int j = 0; j < arrCurrentItems.Length; j++)
                    {
                        string     strItemName = $"{StrItemId}.{strGrp}.{arrCurrentItems[j]}";
                        OPCItemDef item        = new OPCItemDef(strItemName, true, opcResults.Length, System.Runtime.InteropServices.VarEnum.VT_BOOL);
                        opcItems[j] = item;
                    }
                    Dictionary <string, OPCItemResult> oDic = new Dictionary <string, OPCItemResult>();
                    grp.AddItems(opcItems, out opcResults);

                    for (int k = 0; k < opcResults.Length; k++)
                    {
                        oDic.Add(arrCurrentItems[k], opcResults[k]);
                    }
                    _opcResultsDic.Add(strGrp, oDic);
                }
            }
            catch (Exception ee)
            {
                LogImpl.Debug("错误" + ee.ToString());
            }
        }
Example #3
0
        private PLCCommand2()
        {
            try
            {
                opcSrv.Connect(strSrvName);
                for (int i = 0; i < arrGrps.Length; i++)
                {
                    string   strGrp = arrGrps[i];
                    OpcGroup grp    = opcSrv.AddGroup(strGrp, true, 9600);
                    opcGrpsDic.Add(strGrp, grp);

                    string[] arrCurrentItems = arrItems[i].Split(',');

                    OPCItemDef[]    opcItems   = new OPCItemDef[arrCurrentItems.Length];
                    OPCItemResult[] opcResults = new OPCItemResult[arrCurrentItems.Length];
                    for (int j = 0; j < arrCurrentItems.Length; j++)
                    {
                        string     strItemName = string.Format("{0}.{1}.{2}", strItemID, strGrp, arrCurrentItems[j]);
                        OPCItemDef item        = new OPCItemDef(strItemName, true, opcResults.Length, System.Runtime.InteropServices.VarEnum.VT_BOOL);
                        opcItems[j] = item;
                    }
                    Dictionary <string, OPCItemResult> oDic = new Dictionary <string, OPCItemResult>();
                    grp.AddItems(opcItems, out opcResults);

                    for (int k = 0; k < opcResults.Length; k++)
                    {
                        oDic.Add(arrCurrentItems[k], opcResults[k]);
                    }
                    opcResultsDic.Add(strGrp, oDic);
                }
            }
            catch (Exception ee)
            {
                LogImpl.Error("plc初始化:" + ee.StackTrace);
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            /*create array of readable Tags*/
            var Tags = new List <OPCClientItem>();

            Tags.Add(new OPCClientItem()
            {
                Name        = ".test",
                ClientHanle = 1
            });

            OpcServer server = new OpcServer();

            try
            {
                int transactionID = new Random().Next(1024, 65535);
                int cancelID      = 0;
                int updateRate    = 1000;

                /*connect to the OPC Server and check it's state*/
                server.Connect("Matrikon.OPC.Simulation.1");
                var serverStatus = new SERVERSTATUS();
                server.GetStatus(out serverStatus);
                if (serverStatus.eServerState == OPCSERVERSTATE.OPC_STATUS_RUNNING)
                {
                    /*create group of items*/
                    OpcGroup group = server.AddGroup("Group1", true, updateRate);
                    group.ReadCompleted += group_ReadCompleted;
                    List <OPCItemDef> items = new List <OPCItemDef>();
                    Tags.ToList()
                    .ForEach(x => items.Add(new OPCItemDef(x.Name, true, x.ClientHanle, VarEnum.VT_EMPTY)));

                    /* add items and collect their attributes*/
                    OPCItemResult[] itemAddResults = null;
                    group.AddItems(items.ToArray(), out itemAddResults);
                    for (int i = 0; i < itemAddResults.Length; i++)
                    {
                        OPCItemResult itemResult = itemAddResults[i];
                        OPCClientItem tag        = Tags[i];
                        tag.ServerHandle = itemResult.HandleServer;
                        tag.AccessRight  = (itemResult.AccessRights == OPCACCESSRIGHTS.OPC_READABLE) ? OPCClientItem.EAccessRight.ReadOnly : OPCClientItem.EAccessRight.ReadAndWrite;
                    }
                    ;

                    /*Refresh items in group*/
                    // group.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE, transactionID, out cancelID);

                    /*Async read data for the group items*/
                    int[] serverHandles = new int[Tags.Count];
                    for (int i = 0; i < Tags.Count; i++)
                    {
                        serverHandles[i] = Tags[i].ServerHandle;
                    }
                    ;
                    OPCItemState[] itemsStateResult = null;
                    /*sync read*/
                    group.Read(OPCDATASOURCE.OPC_DS_DEVICE, serverHandles, out itemsStateResult);
                    Console.WriteLine("Sync read:");
                    for (int i = 0; i < itemsStateResult.Length; i++)
                    {
                        OPCItemState itemResult = itemsStateResult[i];
                        Console.WriteLine(" -> item:{0}; value:{1}; timestamp{2}; qualituy:{3}", Tags[i].Name, itemResult.DataValue.ToString(), itemResult.TimeStamp, itemResult.Quality);
                    }
                    ;

                    /*sync write*/
                    object[] values       = new object[Tags.Count];
                    int[]    resultErrors = new int[Tags.Count];
                    values[0] = (object)256;
                    group.Write(serverHandles, values, out resultErrors);

                    /*async read*/
                    group.Read(serverHandles, transactionID, out cancelID, out resultErrors);

                    /*wait for a while befor remove group to process async event*/
                    System.Threading.Thread.Sleep(3000);

                    /*the group must be removed !!! */
                    group.Remove(true);
                }
                ;
            }
            finally
            {
                server.Disconnect();
                server = null;
                GC.Collect();
            };
            Console.ReadKey();
        }
Example #5
0
        public string ServerAccessOpc(string serverName)
        {
            OpcServer Srv = new OpcServer();
            int       rtc = Srv.Connect(serverName);

            if (HRESULTS.Failed(rtc))
            {
                try
                {
                    return(Srv.GetErrorString(rtc, 0));
                }
                catch {
                    return(rtc.ToString());
                }
            }

            OpcGroup oGrp;
            float    deadBand = 0.0F;

            try
            {
                oGrp = Srv.AddGroup("Grp1", true, 500, ref deadBand, 0, 0);
            }
            catch (Exception ex)
            {
                Srv.Disconnect();
                return(ex.Message);
            }
            OPCItemDef[] items = new OPCItemDef[1];
            items[0] = new OPCItemDef("Saw-toothed Waves.Int2",
                                      true, 0, VarEnum.VT_I2);
            OPCItemResult[] addRslt = new OPCItemResult[1];
            rtc = oGrp.AddItems(items, out addRslt);
            if (HRESULTS.Failed(rtc))
            {
                Srv.Disconnect();
                return("Error at AddItem");
            }
            int[] iHnd = new Int32[1];
            iHnd[0] = addRslt[0].HandleServer;
            OPCItemState[] rslt;
            rtc = oGrp.Read(OPCDATASOURCE.OPC_DS_CACHE,
                            iHnd, out rslt);
            string retVal;

            if (HRESULTS.Failed(rtc))
            {
                retVal = "Read error";
            }
            else
            if (HRESULTS.Failed(rslt[0].Error))
            {
                retVal = "Item Read error";
            }
            else
            {
                retVal = "data=" + rslt[0].DataValue.ToString();
            }
            oGrp.Remove(true);
            Srv.Disconnect();
            return(retVal);
        }
Example #6
0
        public void TestCreateOPCItemResult01()
        {
            OPCItemResult oPCItemResult = CreateOPCItemResult01();

            Assert.IsNotNull(oPCItemResult);
        }
Example #7
0
        public static OPCItemResult CreateOPCItemResult01()
        {
            OPCItemResult oPCItemResult = new OPCItemResult();

            return(oPCItemResult);
        }
Example #8
0
 public void TestOPCItemResultConstructor01()
 {
     OPCItemResult oPCItemResult = new OPCItemResult();
 }