Beispiel #1
0
        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);
        }
Beispiel #2
0
        public void Connect()
        {
            int i;

            try
            {
#if CREDENTIALS
                LogMessage("Test if CNC Exe IsExecuting()", 1);
                if ((sCNCProcessName.Length > 0) && !IsExecuting(sOPCMachine, sCNCProcessName))
                {
                    throw new Exception("Cannot start OPC Server" + sCNCProcessName + " not running");
                }


                LogMessage("Connect to CNC", 1);

                // This is used as credentials to logon ont remote pc to see if CNC running
                System.Net.NetworkCredential credential;
                if (sDomain.Length > 0)
                {
                    credential = new System.Net.NetworkCredential(sUser, sPassword, sDomain);
                }
                else
                {
                    credential = new System.Net.NetworkCredential(sUser, sPassword); // , sDomain);
                }
#endif
                _opcserver = new OpcServer();

                LogMessage("Attempt OPC Server Connection", Logger.DEBUG);
                // _opcserver will be null if failed...
                _opcserver.Connect(this.clsid, this.sIpAddress);

                LogMessage("Create OPC Group", Logger.DEBUG);
                opcgroup = _opcserver.AddGroup("OPCCSharp-Group", false, 900);

                if (opcgroup == null)
                {
                    throw new Exception("Connect - AddGroup failed");
                }

                // FIXME: this only works if OPC item exists.
                // Add items to OPC Group
                LogMessage("Add OPC Items", Logger.DEBUG);
                OPCItemResult[] itemresult;
                opcgroup.AddItems(tags, out itemresult);

                if (itemresult == null)
                {
                    throw new Exception("Connect - OPC AddItems failed");
                }

                LogMessage("Check OPC items for errors.", Logger.DEBUG);
                for (i = 0; i < itemresult.Length; i++)
                {
                    // If the OPC item failed - remove it from the tags to be updated.
                    if (HRESULTS.Failed(itemresult[i].Error))
                    {
                        LogMessage("OPC AddItems Error: " + tags[i].ItemID, Logger.DEBUG);
                        itemresult = (OPCItemResult[])RemoveAt(itemresult, i);
                        tags       = (OPCItemDef[])RemoveAt(tags, i);
                        handlesSrv = (int[])RemoveAt(handlesSrv, i);
                        continue;
                    }
                    handlesSrv[i] = itemresult[i].HandleServer;
                }

                // read group
                LogMessage("OPC ReadStatus", Logger.DEBUG);
                ReadStatus();
                LogMessage("OPC ReadGroup", Logger.DEBUG);
                ReadGroup();
            }
            catch (Exception e)
            {
                LogMessage("OPC Connect Error: " + e.Message, Logger.ERROR);
                Disconnect();
            }
        }