Пример #1
0
        public void QueryAvailableLocaleIDs(out int[] p_LocalIDs)
        {
            p_LocalIDs = null;
            int    l_Count;
            IntPtr l_PtrIDs;
            int    l_HResult = m_IfCommon.QueryAvailableLocaleIDs(out l_Count, out l_PtrIDs);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }
            if (((int)l_PtrIDs) == 0)
            {
                return;
            }
            if (l_Count < 1)
            {
                Marshal.FreeCoTaskMem(l_PtrIDs);
                return;
            }

            p_LocalIDs = new int[l_Count];
            Marshal.Copy(l_PtrIDs, p_LocalIDs, 0, l_Count);
            Marshal.FreeCoTaskMem(l_PtrIDs);
        }
        public bool Write(int[] p_ArrHSrv, object[] p_ArrVal, int p_TransactionID, out int p_CancelID, out int[] p_ArrErr)
        {
            p_ArrErr   = null;
            p_CancelID = 0;
            int l_Count = p_ArrHSrv.Length;

            if (l_Count != p_ArrVal.Length)
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            IntPtr l_PtrErr;
            int    l_HResult = m_IfAsync.Write(l_Count, p_ArrHSrv, p_ArrVal, p_TransactionID, out p_CancelID, out l_PtrErr);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            p_ArrErr = new int[l_Count];
            Marshal.Copy(l_PtrErr, p_ArrErr, 0, l_Count);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
        public bool Read(OpcDataSource p_Src, int[] p_ArrHSrv, out OpcItemState[] p_ArrStat)
        {
            p_ArrStat = null;
            int    l_Count = p_ArrHSrv.Length;
            IntPtr l_PtrStat;
            IntPtr l_PtrErr;

            int l_HResult = m_IfSync.Read(p_Src, l_Count, p_ArrHSrv, out l_PtrStat, out l_PtrErr);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            int l_RunErr  = (int)l_PtrErr;
            int l_RunStat = (int)l_PtrStat;

            if ((l_RunErr == 0) || (l_RunStat == 0))
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            p_ArrStat = new OpcItemState[l_Count];

            for (int i = 0; i < l_Count; ++i)
            {
                p_ArrStat[i] = new OpcItemState();

                p_ArrStat[i].Error = Marshal.ReadInt32((IntPtr)l_RunErr);
                l_RunErr          += 4;

                p_ArrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)l_RunStat);

                if (HResults.Succeeded(p_ArrStat[i].Error))
                {
                    short l_VT = Marshal.ReadInt16((IntPtr)(l_RunStat + 16));
                    if (l_VT == (short)VarEnum.VT_ERROR)
                    {
                        p_ArrStat[i].Error = Marshal.ReadInt32((IntPtr)(l_RunStat + 24));
                    }

                    p_ArrStat[i].Timestamp = Marshal.ReadInt64((IntPtr)(l_RunStat + 4));
                    p_ArrStat[i].Quality   = Marshal.ReadInt16((IntPtr)(l_RunStat + 12));
                    p_ArrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(l_RunStat + 16));
                    DummyVariant.VariantClear((IntPtr)(l_RunStat + 16));
                }
                else
                {
                    p_ArrStat[i].DataValue = null;
                }

                l_RunStat += 32;
            }

            Marshal.FreeCoTaskMem(l_PtrStat);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
Пример #4
0
        public bool LookupItemIDs(string p_ItemID, int[] p_PropertyIDs, out OpcPropertyItem[] p_PropertyItems)
        {
            p_PropertyItems = null;
            int l_Count = p_PropertyIDs.Length;

            if (l_Count < 1)
            {
                return(false);
            }

            IntPtr l_PtrErr;
            IntPtr l_PtrIDs;
            int    l_HResult = m_IfItmProps.LookupItemIDs(p_ItemID, l_Count, p_PropertyIDs, out l_PtrIDs, out l_PtrErr);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            int l_RunIDs = (int)l_PtrIDs;
            int l_RunErr = (int)l_PtrErr;

            if ((l_RunIDs == 0) || (l_RunErr == 0))
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            p_PropertyItems = new OpcPropertyItem[l_Count];

            IntPtr l_PtrString;

            for (int i = 0; i < l_Count; ++i)
            {
                p_PropertyItems[i]            = new OpcPropertyItem();
                p_PropertyItems[i].PropertyId = p_PropertyIDs[i];

                p_PropertyItems[i].Error = Marshal.ReadInt32((IntPtr)l_RunErr);
                l_RunErr += 4;

                if (p_PropertyItems[i].Error == HResults.S_OK)
                {
                    l_PtrString = (IntPtr)Marshal.ReadInt32((IntPtr)l_RunIDs);
                    p_PropertyItems[i].NewItemId = Marshal.PtrToStringUni(l_PtrString);
                    Marshal.FreeCoTaskMem(l_PtrString);
                }
                else
                {
                    p_PropertyItems[i].NewItemId = null;
                }

                l_RunIDs += 4;
            }

            Marshal.FreeCoTaskMem(l_PtrIDs);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
Пример #5
0
        public bool GetItemProperties(string p_ItemID, int[] p_PropertyIDs, out OpcPropertyData[] p_PropertiesData)
        {
            p_PropertiesData = null;
            int l_Count = p_PropertyIDs.Length;

            if (l_Count < 1)
            {
                return(false);
            }

            IntPtr l_PtrDat;
            IntPtr l_PtrErr;
            int    l_HResult = m_IfItmProps.GetItemProperties(p_ItemID, l_Count, p_PropertyIDs, out l_PtrDat, out l_PtrErr);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            int l_RunDat = (int)l_PtrDat;
            int l_RunErr = (int)l_PtrErr;

            if ((l_RunDat == 0) || (l_RunErr == 0))
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            p_PropertiesData = new OpcPropertyData[l_Count];

            for (int i = 0; i < l_Count; ++i)
            {
                p_PropertiesData[i]            = new OpcPropertyData();
                p_PropertiesData[i].PropertyId = p_PropertyIDs[i];

                p_PropertiesData[i].Error = Marshal.ReadInt32((IntPtr)l_RunErr);
                l_RunErr += 4;

                if (p_PropertiesData[i].Error == HResults.S_OK)
                {
                    p_PropertiesData[i].Data = Marshal.GetObjectForNativeVariant((IntPtr)l_RunDat);
                    DummyVariant.VariantClear((IntPtr)l_RunDat);
                }
                else
                {
                    p_PropertiesData[i].Data = null;
                }

                l_RunDat += DummyVariant.CONST_SIZE;
            }

            Marshal.FreeCoTaskMem(l_PtrDat);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
        void IOPCDataCallback.OnReadComplete(int p_TransID, int p_Group, int p_MasterQuality,
                                             int p_MasterError, int p_Count, IntPtr p_ClientItems, IntPtr p_Values,
                                             IntPtr p_Qualities, IntPtr p_TimeStamps, IntPtr p_Errors)
        {
            Trace.WriteLine("OPCGroup.OnReadComplete");
            if ((p_Count == 0) || (p_Group != m_State.HandleClient))
            {
                return;
            }

            int l_Count = (int)p_Count;
            int l_RunH  = (int)p_ClientItems;
            int l_RunV  = (int)p_Values;
            int l_RunQ  = (int)p_Qualities;
            int l_RunT  = (int)p_TimeStamps;
            int l_RunE  = (int)p_Errors;

            OpcItemState[] l_Sts = new OpcItemState[l_Count];

            for (int i = 0; i < l_Count; ++i)
            {
                l_Sts[i]       = new OpcItemState();
                l_Sts[i].Error = Marshal.ReadInt32((IntPtr)l_RunE);
                l_RunE        += 4;

                l_Sts[i].HandleClient = Marshal.ReadInt32((IntPtr)l_RunH);
                l_RunH += 4;

                if (HResults.Succeeded(l_Sts[i].Error))
                {
                    short l_VT = Marshal.ReadInt16((IntPtr)l_RunV);
                    if (l_VT == (short)VarEnum.VT_ERROR)
                    {
                        l_Sts[i].Error = Marshal.ReadInt32((IntPtr)(l_RunV + 8));
                    }

                    l_Sts[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)l_RunV);
                    l_Sts[i].Quality   = Marshal.ReadInt16((IntPtr)l_RunQ);
                    l_Sts[i].Timestamp = Marshal.ReadInt64((IntPtr)l_RunT);
                }

                l_RunV += DummyVariant.CONST_SIZE;
                l_RunQ += 2;
                l_RunT += 8;
            }

            var args = new ReadCompleteEventArgs(p_TransID, p_Group, p_MasterQuality, p_MasterError, l_Sts);

            ReadCompleted?.Invoke(this, args);
        }
        public bool SetActiveState(int[] p_ArrHSrv, bool p_Activate, out int[] p_ArrErr)
        {
            p_ArrErr = null;
            int    l_Count = p_ArrHSrv.Length;
            IntPtr l_PtrErr;

            int l_HResult = m_IfItems.SetActiveState(l_Count, p_ArrHSrv, p_Activate, out l_PtrErr);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            p_ArrErr = new int[l_Count];
            Marshal.Copy(l_PtrErr, p_ArrErr, 0, l_Count);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
        public bool SetDatatypes(int[] p_ArrHSrv, VarEnum[] p_ArrVT, out int[] p_ArrErr)
        {
            p_ArrErr = null;
            int l_Count = p_ArrHSrv.Length;

            if (l_Count != p_ArrVT.Length)
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            IntPtr l_PtrVT = Marshal.AllocCoTaskMem(l_Count * 2);
            int    l_RunVT = (int)l_PtrVT;

            for (int i = 0; i < p_ArrVT.Length; ++i)
            {
                VarEnum l_VarEnm = p_ArrVT[i];

                Marshal.WriteInt16((IntPtr)l_RunVT, (short)l_VarEnm);
                l_RunVT += 2;
            }

            IntPtr l_PtrErr;
            int    l_HResult = m_IfItems.SetDatatypes(l_Count, p_ArrHSrv, l_PtrVT, out l_PtrErr);

            Marshal.FreeCoTaskMem(l_PtrVT);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            p_ArrErr = new int[l_Count];
            Marshal.Copy(l_PtrErr, p_ArrErr, 0, l_Count);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
        public OPCEnumItemAttributes CreateAttrEnumerator()
        {
            Type   l_TypEnuAtt  = typeof(IEnumOPCItemAttributes);
            Guid   l_GuidEnuAtt = l_TypEnuAtt.GUID;
            object l_ObjTemp;

            int l_HResult = m_IfItems.CreateEnumerator(ref l_GuidEnuAtt, out l_ObjTemp);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }
            if ((l_HResult == HResults.S_FALSE) || (l_ObjTemp == null))
            {
                return(null);
            }

            IEnumOPCItemAttributes l_IfEnu = (IEnumOPCItemAttributes)l_ObjTemp;

            l_ObjTemp = null;
            OPCEnumItemAttributes l_Enu = new OPCEnumItemAttributes(l_IfEnu);

            return(l_Enu);
        }
        public bool SetClientHandles(int[] p_ArrHSrv, int[] p_ArrHClt, out int[] p_ArrErr)
        {
            p_ArrErr = null;
            int l_Count = p_ArrHSrv.Length;

            if (l_Count != p_ArrHClt.Length)
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            IntPtr l_PtrErr;
            int    l_HResult = m_IfItems.SetClientHandles(l_Count, p_ArrHSrv, p_ArrHClt, out l_PtrErr);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            p_ArrErr = new int[l_Count];
            Marshal.Copy(l_PtrErr, p_ArrErr, 0, l_Count);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
        public bool ValidateItems(OpcItemDef[] p_ArrDef, bool p_BlobUpd, out OpcItemResult[] p_ArrRes)
        {
            p_ArrRes = null;
            bool             l_HasBlobs    = false;
            int              l_Count       = p_ArrDef.Length;
            IntPtr           l_PtrDef      = Marshal.AllocCoTaskMem(l_Count * m_SizeOPCItemDef);
            int              l_RunDef      = (int)l_PtrDef;
            OpcItemDefIntern l_ItmDefIntrn = new OpcItemDefIntern();

            l_ItmDefIntrn.Reserved = 0;

            for (int i = 0; i < p_ArrDef.Length; ++i)
            {
                OpcItemDef l_ItmDef = p_ArrDef[i];

                l_ItmDefIntrn.AccessPath        = l_ItmDef.AccessPath;
                l_ItmDefIntrn.ItemID            = l_ItmDef.ItemID;
                l_ItmDefIntrn.Active            = l_ItmDef.Active;
                l_ItmDefIntrn.Client            = l_ItmDef.HandleClient;
                l_ItmDefIntrn.RequestedDataType = (short)l_ItmDef.RequestedDataType;
                l_ItmDefIntrn.BlobSize          = 0;
                l_ItmDefIntrn.Blob = IntPtr.Zero;

                if (l_ItmDef.Blob != null)
                {
                    l_ItmDefIntrn.BlobSize = l_ItmDef.Blob.Length;
                    if (l_ItmDefIntrn.BlobSize > 0)
                    {
                        l_HasBlobs         = true;
                        l_ItmDefIntrn.Blob = Marshal.AllocCoTaskMem(l_ItmDefIntrn.BlobSize);
                        Marshal.Copy(l_ItmDef.Blob, 0, l_ItmDefIntrn.Blob, l_ItmDefIntrn.BlobSize);
                    }
                }

                Marshal.StructureToPtr(l_ItmDefIntrn, (IntPtr)l_RunDef, false);
                l_RunDef += m_SizeOPCItemDef;
            }

            IntPtr l_PtrRes;
            IntPtr l_PtrErr;
            int    l_HResult = m_IfItems.ValidateItems(l_Count, l_PtrDef, p_BlobUpd, out l_PtrRes, out l_PtrErr);

            l_RunDef = (int)l_PtrDef;
            if (l_HasBlobs)
            {
                for (int i = 0; i < l_Count; ++i)
                {
                    IntPtr l_Blob = (IntPtr)Marshal.ReadInt32((IntPtr)(l_RunDef + 20));
                    if (l_Blob != IntPtr.Zero)
                    {
                        Marshal.FreeCoTaskMem(l_Blob);
                    }
                    Marshal.DestroyStructure((IntPtr)l_RunDef, m_TypeOPCItemDef);
                    l_RunDef += m_SizeOPCItemDef;
                }
            }
            else
            {
                for (int i = 0; i < l_Count; ++i)
                {
                    Marshal.DestroyStructure((IntPtr)l_RunDef, m_TypeOPCItemDef);
                    l_RunDef += m_SizeOPCItemDef;
                }
            }
            Marshal.FreeCoTaskMem(l_PtrDef);

            if (HResults.Failed(l_HResult))
            {
                Marshal.ThrowExceptionForHR(l_HResult);
            }

            int l_RunRes = (int)l_PtrRes;
            int l_RunErr = (int)l_PtrErr;

            if ((l_RunRes == 0) || (l_RunErr == 0))
            {
                Marshal.ThrowExceptionForHR(HResults.E_ABORT);
            }

            p_ArrRes = new OpcItemResult[l_Count];
            for (int i = 0; i < l_Count; ++i)
            {
                p_ArrRes[i]       = new OpcItemResult();
                p_ArrRes[i].Error = Marshal.ReadInt32((IntPtr)l_RunErr);
                if (HResults.Failed(p_ArrRes[i].Error))
                {
                    continue;
                }

                p_ArrRes[i].HandleServer      = Marshal.ReadInt32((IntPtr)l_RunRes);
                p_ArrRes[i].CanonicalDataType = (VarEnum)(int)Marshal.ReadInt16((IntPtr)(l_RunRes + 4));
                p_ArrRes[i].AccessRights      = (OpcAccessRights)Marshal.ReadInt32((IntPtr)(l_RunRes + 8));

                int l_PtrBlob = Marshal.ReadInt32((IntPtr)(l_RunRes + 16));
                if (l_PtrBlob != 0)
                {
                    int l_BlobSize = Marshal.ReadInt32((IntPtr)(l_RunRes + 12));
                    if (l_BlobSize > 0)
                    {
                        p_ArrRes[i].Blob = new byte[l_BlobSize];
                        Marshal.Copy((IntPtr)l_PtrBlob, p_ArrRes[i].Blob, 0, l_BlobSize);
                    }
                    Marshal.FreeCoTaskMem((IntPtr)l_PtrBlob);
                }

                l_RunRes += m_SizeOPCItemResult;
                l_RunErr += 4;
            }

            Marshal.FreeCoTaskMem(l_PtrRes);
            Marshal.FreeCoTaskMem(l_PtrErr);

            return(l_HResult == HResults.S_OK);
        }
        void IOPCDataCallback.OnDataChange(int transId,
                                           int @group,
                                           int masterQuality,
                                           int masterError,
                                           int count,
                                           IntPtr clientItemsPtr,
                                           IntPtr valuesPtr,
                                           IntPtr qualitiesPtr,
                                           IntPtr timeStampsPtr,
                                           IntPtr errorsPtr)
        {
            Trace.WriteLine("OPCGroup.OnDataChange, " + DateTime.Now);
            if ((count == 0) || (@group != m_State.HandleClient))
            {
                return;
            }

            //int l_Count = (int) count;
            int runH       = (int)clientItemsPtr;
            int runValue   = (int)valuesPtr;
            int runQuality = (int)qualitiesPtr;
            int runTime    = (int)timeStampsPtr;
            int runError   = (int)errorsPtr;

            var itemStates = new OpcItemState[count];

            for (int i = 0; i < count; ++i)
            {
                itemStates[i] = new OpcItemState {
                    Error = Marshal.ReadInt32((IntPtr)runError)
                };
                runError += 4;

                itemStates[i].HandleClient = Marshal.ReadInt32((IntPtr)runH);
                runH += 4;

                if (HResults.Succeeded(itemStates[i].Error))
                {
                    short value = Marshal.ReadInt16((IntPtr)runValue);
                    if (value == (short)VarEnum.VT_ERROR)
                    {
                        itemStates[i].Error = Marshal.ReadInt32((IntPtr)(runValue + 8));
                    }

                    itemStates[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)runValue);
                    itemStates[i].Quality   = Marshal.ReadInt16((IntPtr)runQuality);
                    itemStates[i].Timestamp = Marshal.ReadInt64((IntPtr)runTime);
                }

                runValue   += DummyVariant.CONST_SIZE;
                runQuality += 2;
                runTime    += 8;
            }

            //实体序列化和反序列化
            string json1 = JsonHelper.SerializeObject(itemStates);

            try
            {
                var parameter = new MySqlParameter("?p1", MySqlDbType.Text, 0)
                {
                    Value = json1
                };

                var executeScalar = MySqlHelper.ExecuteScalar("insert into opc_data(item_data) values(?p1)", parameter);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            var eventArgs = new DataChangeEventArgs(transId, @group, masterQuality, masterError, itemStates);

            DataChanged?.Invoke(this, eventArgs);
        }