コード例 #1
0
ファイル: OPCMgr.cs プロジェクト: Wolfury/MTConnect4OPC
        /// <summary>
        /// Initialize Potential OPC Tag names to canonical ids (ints).
        /// </summary>
        public void Init()
        {
            // declarations-  not a savvy enough C# programmer not to just hard code array sizes
            updateditems = new Hashtable();
            itemvalues = new string[1000];
            tags = new OPCItemDef[1000];

            _status = "Reading OPC configuration";
            Logger.LogMessage("OPCMgr  - Reading OPC configuration\n", Logger.DEBUG);

            // Read App.config file for  OPC and Exe options
            try
            {
                // ShdrService4Opc Settings
                bSynchronous = Convert.ToBoolean(ConfigurationManager.AppSettings["Synchronous"]);
                nPriority = Convert.ToInt32(ConfigurationManager.AppSettings["ProcessPriority"]);
                bAutoConnect = Convert.ToBoolean(ConfigurationManager.AppSettings["AutoConnect"]);
                nServerUpdatePeriod = Convert.ToInt32(ConfigurationManager.AppSettings["ServerUpdatePeriod"]);
                nServerRetryPeriod = Convert.ToInt32(ConfigurationManager.AppSettings["ServerRetryPeriod"]);

                // OPC Settings
                sOPCProgId = ConfigurationManager.AppSettings["OPCServer"];

                //n This works is obsolete by why risk it???
                serveritems = (System.Collections.IDictionary)ConfigurationSettings.GetConfig(sOPCProgId);

                sCNCProcessName = (string)serveritems["CNCProcessName"];
                sOpcClsid = (string)serveritems["OpcServerClsid"];
                clsid = new System.Guid(sOpcClsid);
                //myurl = new Opc.URL("opcda://" + sOPCMachine + "/" + sOPCProgId);

                sUser = ConfigurationManager.AppSettings["User"];
                sPassword = ConfigurationManager.AppSettings["Password"];
                sDomain = ConfigurationManager.AppSettings["Domain"];

                string sRpcAuthzSrv = ConfigurationManager.AppSettings["RpcAuthzSrv"];
                string sRpcAuthnLevel = ConfigurationManager.AppSettings["RpcAuthnLevel"];
                string sRpcImpersLevel = ConfigurationManager.AppSettings["RpcImpersLevel"];
                // MyEnum oMyEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "stringValue");
                OpcServer.eRpcAuthzSrv = (RpcAuthnSrv)Enum.Parse(typeof(RpcAuthnSrv), sRpcAuthzSrv);
                OpcServer.eRpcAuthnLevel = (RpcAuthnLevel)Enum.Parse(typeof(RpcAuthnLevel), sRpcAuthnLevel);
                OpcServer.eRpcImpersLevel = (RpcImpersLevel)Enum.Parse(typeof(RpcImpersLevel), sRpcImpersLevel);
                OpcServer.sUser = sUser;
                OpcServer.sPassword = sPassword;
                OpcServer.sDomain = sDomain;
                OpcServer.nSimpleOPCActivate = Convert.ToInt32(ConfigurationManager.AppSettings["SimpleOPCActivate"]);

                // Map OPC Server data names into canonical names/ids
                agent.AddDatum("power");
                agent.AddDatum("heartbeat");

                int i = 0;
                foreach (DictionaryEntry de in serveritems)
                {
                    Symbol symbol = new Symbol();
                    string id = (string)de.Key;
                    if (!id.StartsWith("Tag."))
                        continue;
                    id = id.Substring(4);

                    symbol.bEnum = false;
                    if (id.StartsWith("Enum."))
                    {
                        symbol.bEnum = true;
                        id = id.Substring(5);
                    }
                    if (id.StartsWith("Event."))
                    {
                        symbol.type = "Event";
                        id = id.Substring(6);
                    }
                    if (id.StartsWith("Sample."))
                    {
                        symbol.type = "Sample";
                        id = id.Substring(7);
                    }

                    symbol.name = (string)id;
                    symbol.opcalias = (string)de.Value;
                    symbol.nKey = i;
                    // symbols[i] = symbol;
                    symbols.Add(symbol);
                    agent.AddDatum((string)id);

                    tags[i] = new OPCItemDef(symbol.opcalias, true, i, VarEnum.VT_EMPTY);

                    i++;
                }
                ChangeProcessPriority(nPriority);
                Array.Resize<OPCItemDef>(ref tags, i);
                Array.Resize<int>(ref handlesSrv, i);
                Array.Resize<string>(ref itemvalues, i);

                _opcserver = new OpcServer();

            }
            catch (Exception e)
            {
                LogMessage("App.config Initialization Error: " + e.Message, Logger.FATAL);
                throw e;
            };
        }
コード例 #2
0
ファイル: OPCGroup.cs プロジェクト: Wolfury/MTConnect4OPC
        public bool AddItems(OPCItemDef[] arrDef, out OPCItemResult[] arrRes)
        {
            arrRes = null;
            bool hasblobs = false;
            int count = arrDef.Length;

            IntPtr ptrDef = Marshal.AllocCoTaskMem(count * sizeOPCITEMDEF);
            int runDef = (int)ptrDef;
            OPCITEMDEFintern idf = new OPCITEMDEFintern();
            idf.wReserved = 0;
            foreach (OPCItemDef d in arrDef)
            {
                idf.szAccessPath = d.AccessPath;
                idf.szItemID = d.ItemID;
                idf.bActive = d.Active;
                idf.hClient = d.HandleClient;
                idf.vtRequestedDataType = (short)d.RequestedDataType;
                idf.dwBlobSize = 0; idf.pBlob = IntPtr.Zero;
                if (d.Blob != null)
                {
                    idf.dwBlobSize = d.Blob.Length;
                    if (idf.dwBlobSize > 0)
                    {
                        hasblobs = true;
                        idf.pBlob = Marshal.AllocCoTaskMem(idf.dwBlobSize);
                        Marshal.Copy(d.Blob, 0, idf.pBlob, idf.dwBlobSize);
                    }
                }

                Marshal.StructureToPtr(idf, (IntPtr)runDef, false);
                runDef += sizeOPCITEMDEF;
            }

            IntPtr ptrRes;
            IntPtr ptrErr;
            int hresult = ifItems.AddItems(count, ptrDef, out ptrRes, out ptrErr);

            runDef = (int)ptrDef;
            if (hasblobs)
            {
                for (int i = 0; i < count; i++)
                {
                    IntPtr blob = (IntPtr)Marshal.ReadInt32((IntPtr)(runDef + 20));
                    if (blob != IntPtr.Zero)
                        Marshal.FreeCoTaskMem(blob);
                    Marshal.DestroyStructure((IntPtr)runDef, typeOPCITEMDEF);
                    runDef += sizeOPCITEMDEF;
                }
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    Marshal.DestroyStructure((IntPtr)runDef, typeOPCITEMDEF);
                    runDef += sizeOPCITEMDEF;
                }
            }
            Marshal.FreeCoTaskMem(ptrDef);

            if (HRESULTS.Failed(hresult))
                Marshal.ThrowExceptionForHR(hresult);

            int runRes = (int)ptrRes;
            int runErr = (int)ptrErr;
            if ((runRes == 0) || (runErr == 0))
                Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT);

            arrRes = new OPCItemResult[count];
            for (int i = 0; i < count; i++)
            {
                arrRes[i] = new OPCItemResult();
                arrRes[i].Error = Marshal.ReadInt32((IntPtr)runErr);
                if (HRESULTS.Failed(arrRes[i].Error))
                    continue;

                arrRes[i].HandleServer = Marshal.ReadInt32((IntPtr)runRes);
                arrRes[i].CanonicalDataType = (VarEnum)(int)Marshal.ReadInt16((IntPtr)(runRes + 4));
                arrRes[i].AccessRights = (OPCACCESSRIGHTS)Marshal.ReadInt32((IntPtr)(runRes + 8));

                int ptrblob = Marshal.ReadInt32((IntPtr)(runRes + 16));
                if ((ptrblob != 0))
                {
                    int blobsize = Marshal.ReadInt32((IntPtr)(runRes + 12));
                    if (blobsize > 0)
                    {
                        arrRes[i].Blob = new byte[blobsize];
                        Marshal.Copy((IntPtr)ptrblob, arrRes[i].Blob, 0, blobsize);
                    }
                    Marshal.FreeCoTaskMem((IntPtr)ptrblob);
                }

                runRes += sizeOPCITEMRESULT;
                runErr += 4;
            }

            Marshal.FreeCoTaskMem(ptrRes);
            Marshal.FreeCoTaskMem(ptrErr);
            return hresult == HRESULTS.S_OK;
        }
コード例 #3
0
ファイル: OPCMgr.cs プロジェクト: zyuzuguldu/MTConnect4OPC
        /// <summary>
        /// Initialize Potential OPC Tag names to canonical ids (ints).
        /// </summary>
        public void Init()
        {
            // declarations-  not a savvy enough C# programmer not to just hard code array sizes
            updateditems = new Hashtable();
            itemvalues   = new string[1000];
            tags         = new OPCItemDef[1000];

            _status = "Reading OPC configuration";
            Logger.LogMessage("OPCMgr  - Reading OPC configuration\n", Logger.DEBUG);

            // Read App.config file for  OPC and Exe options
            try
            {
                // ShdrService4Opc Settings
                bSynchronous        = Convert.ToBoolean(ConfigurationManager.AppSettings["Synchronous"]);
                nPriority           = Convert.ToInt32(ConfigurationManager.AppSettings["ProcessPriority"]);
                bAutoConnect        = Convert.ToBoolean(ConfigurationManager.AppSettings["AutoConnect"]);
                nServerUpdatePeriod = Convert.ToInt32(ConfigurationManager.AppSettings["ServerUpdatePeriod"]);
                nServerRetryPeriod  = Convert.ToInt32(ConfigurationManager.AppSettings["ServerRetryPeriod"]);


                // OPC Settings
                sOPCProgId = ConfigurationManager.AppSettings["OPCServer"];

                //n This works is obsolete by why risk it???
                serveritems = (System.Collections.IDictionary)ConfigurationSettings.GetConfig(sOPCProgId);

                sCNCProcessName = (string)serveritems["CNCProcessName"];
                sOpcClsid       = (string)serveritems["OpcServerClsid"];
                clsid           = new System.Guid(sOpcClsid);
                //myurl = new Opc.URL("opcda://" + sOPCMachine + "/" + sOPCProgId);


                sUser     = ConfigurationManager.AppSettings["User"];
                sPassword = ConfigurationManager.AppSettings["Password"];
                sDomain   = ConfigurationManager.AppSettings["Domain"];

                string sRpcAuthzSrv    = ConfigurationManager.AppSettings["RpcAuthzSrv"];
                string sRpcAuthnLevel  = ConfigurationManager.AppSettings["RpcAuthnLevel"];
                string sRpcImpersLevel = ConfigurationManager.AppSettings["RpcImpersLevel"];
                // MyEnum oMyEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "stringValue");
                OpcServer.eRpcAuthzSrv       = (RpcAuthnSrv)Enum.Parse(typeof(RpcAuthnSrv), sRpcAuthzSrv);
                OpcServer.eRpcAuthnLevel     = (RpcAuthnLevel)Enum.Parse(typeof(RpcAuthnLevel), sRpcAuthnLevel);
                OpcServer.eRpcImpersLevel    = (RpcImpersLevel)Enum.Parse(typeof(RpcImpersLevel), sRpcImpersLevel);
                OpcServer.sUser              = sUser;
                OpcServer.sPassword          = sPassword;
                OpcServer.sDomain            = sDomain;
                OpcServer.nSimpleOPCActivate = Convert.ToInt32(ConfigurationManager.AppSettings["SimpleOPCActivate"]);

                // Map OPC Server data names into canonical names/ids
                agent.AddDatum("power");
                agent.AddDatum("heartbeat");

                int i = 0;
                foreach (DictionaryEntry de in serveritems)
                {
                    Symbol symbol = new Symbol();
                    string id     = (string)de.Key;
                    if (!id.StartsWith("Tag."))
                    {
                        continue;
                    }
                    id = id.Substring(4);

                    symbol.bEnum = false;
                    if (id.StartsWith("Enum."))
                    {
                        symbol.bEnum = true;
                        id           = id.Substring(5);
                    }
                    if (id.StartsWith("Event."))
                    {
                        symbol.type = "Event";
                        id          = id.Substring(6);
                    }
                    if (id.StartsWith("Sample."))
                    {
                        symbol.type = "Sample";
                        id          = id.Substring(7);
                    }

                    symbol.name     = (string)id;
                    symbol.opcalias = (string)de.Value;
                    symbol.nKey     = i;
                    // symbols[i] = symbol;
                    symbols.Add(symbol);
                    agent.AddDatum((string)id);

                    tags[i] = new OPCItemDef(symbol.opcalias, true, i, VarEnum.VT_EMPTY);


                    i++;
                }
                ChangeProcessPriority(nPriority);
                Array.Resize <OPCItemDef>(ref tags, i);
                Array.Resize <int>(ref handlesSrv, i);
                Array.Resize <string>(ref itemvalues, i);

                _opcserver = new OpcServer();
            }
            catch (Exception e)
            {
                LogMessage("App.config Initialization Error: " + e.Message, Logger.FATAL);
                throw e;
            };
        }