Пример #1
0
        public void Next(int enumcountmax, out OPCItemAttributes[] attributes)
        {
            attributes = null;

            IntPtr ptrAtt;
            int    count;

            ifEnum.Next(enumcountmax, out ptrAtt, out count);
            int runatt = (int)ptrAtt;

            if ((runatt == 0) || (count <= 0) || (count > enumcountmax))
            {
                return;
            }

            attributes = new OPCItemAttributes[count];
            IntPtr ptrString;

            for (int i = 0; i < count; i++)
            {
                attributes[i] = new OPCItemAttributes();

                ptrString = (IntPtr)Marshal.ReadInt32((IntPtr)runatt);
                attributes[i].AccessPath = Marshal.PtrToStringUni(ptrString);
                Marshal.FreeCoTaskMem(ptrString);

                ptrString            = (IntPtr)Marshal.ReadInt32((IntPtr)(runatt + 4));
                attributes[i].ItemID = Marshal.PtrToStringUni(ptrString);
                Marshal.FreeCoTaskMem(ptrString);

                attributes[i].Active            = (Marshal.ReadInt32((IntPtr)(runatt + 8))) != 0;
                attributes[i].HandleClient      = Marshal.ReadInt32((IntPtr)(runatt + 12));
                attributes[i].HandleServer      = Marshal.ReadInt32((IntPtr)(runatt + 16));
                attributes[i].AccessRights      = (OPCACCESSRIGHTS)Marshal.ReadInt32((IntPtr)(runatt + 20));
                attributes[i].RequestedDataType = (VarEnum)Marshal.ReadInt16((IntPtr)(runatt + 32));
                attributes[i].CanonicalDataType = (VarEnum)Marshal.ReadInt16((IntPtr)(runatt + 34));

                attributes[i].EUType = (OPCEUTYPE)Marshal.ReadInt32((IntPtr)(runatt + 36));
                attributes[i].EUInfo = Marshal.GetObjectForNativeVariant((IntPtr)(runatt + 40));
                DUMMY_VARIANT.VariantClear((IntPtr)(runatt + 40));

                int ptrblob = Marshal.ReadInt32((IntPtr)(runatt + 28));
                if ((ptrblob != 0))
                {
                    int blobsize = Marshal.ReadInt32((IntPtr)(runatt + 24));
                    if (blobsize > 0)
                    {
                        attributes[i].Blob = new byte[blobsize];
                        Marshal.Copy((IntPtr)ptrblob, attributes[i].Blob, 0, blobsize);
                    }
                    Marshal.FreeCoTaskMem((IntPtr)ptrblob);
                }

                runatt += 56;
            }

            Marshal.FreeCoTaskMem(ptrAtt);
        }
Пример #2
0
        // ------------------------ IOPCSyncIO ---------------

        public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat)
        {
            arrStat = null;
            int    count = arrHSrv.Length;
            IntPtr ptrStat;
            IntPtr ptrErr;
            int    hresult = ifSync.Read(src, count, arrHSrv, out ptrStat, out ptrErr);

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

            int runErr  = (int)ptrErr;
            int runStat = (int)ptrStat;

            if ((runErr == 0) || (runStat == 0))
            {
                Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT);
            }

            arrStat = new OPCItemState[count];
            for (int i = 0; i < count; i++)
            {                                                                                                                           // WORKAROUND !!!
                arrStat[i] = new OPCItemState();

                arrStat[i].Error = Marshal.ReadInt32((IntPtr)runErr);
                runErr          += 4;

                arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)runStat);

                if (HRESULTS.Succeeded(arrStat[i].Error))
                {
                    short vt = Marshal.ReadInt16((IntPtr)(runStat + 16));
                    if (vt == (short)VarEnum.VT_ERROR)
                    {
                        arrStat[i].Error = Marshal.ReadInt32((IntPtr)(runStat + 24));
                    }

                    arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(runStat + 4));
                    arrStat[i].Quality   = Marshal.ReadInt16((IntPtr)(runStat + 12));
                    arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16));
                    DUMMY_VARIANT.VariantClear((IntPtr)(runStat + 16));
                }
                else
                {
                    arrStat[i].DataValue = null;
                }

                runStat += 32;
            }

            Marshal.FreeCoTaskMem(ptrStat);
            Marshal.FreeCoTaskMem(ptrErr);
            return(hresult == HRESULTS.S_OK);
        }
Пример #3
0
        public bool GetItemProperties(string itemID, int[] propertyIDs, out OPCPropertyData[] propertiesData)
        {
            propertiesData = null;
            int count = propertyIDs.Length;

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

            IntPtr ptrDat;
            IntPtr ptrErr;
            int    hresult = ifItmProps.GetItemProperties(itemID, count, propertyIDs, out ptrDat, out ptrErr);

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

            int runDat = (int)ptrDat;
            int runErr = (int)ptrErr;

            if ((runDat == 0) || (runErr == 0))
            {
                Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT);
            }

            propertiesData = new OPCPropertyData[count];

            for (int i = 0; i < count; i++)
            {
                propertiesData[i]            = new OPCPropertyData();
                propertiesData[i].PropertyID = propertyIDs[i];

                propertiesData[i].Error = Marshal.ReadInt32((IntPtr)runErr);
                runErr += 4;

                if (propertiesData[i].Error == HRESULTS.S_OK)
                {
                    propertiesData[i].Data = Marshal.GetObjectForNativeVariant((IntPtr)runDat);
                    DUMMY_VARIANT.VariantClear((IntPtr)runDat);
                }
                else
                {
                    propertiesData[i].Data = null;
                }

                runDat += DUMMY_VARIANT.ConstSize;
            }

            Marshal.FreeCoTaskMem(ptrDat);
            Marshal.FreeCoTaskMem(ptrErr);
            return(hresult == HRESULTS.S_OK);
        }
Пример #4
0
        public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat)
        {
            IntPtr ptr;
            IntPtr ptr2;

            arrStat = null;
            int length      = arrHSrv.Length;
            int hresultcode = this.ifSync.Read(src, length, arrHSrv, out ptr, out ptr2);

            if (HRESULTS.Failed(hresultcode))
            {
                Marshal.ThrowExceptionForHR(hresultcode);
            }
            int num3 = (int)ptr2;
            int num4 = (int)ptr;

            if ((num3 == 0) || (num4 == 0))
            {
                Marshal.ThrowExceptionForHR(-2147467260);
            }
            arrStat = new OPCItemState[length];
            for (int i = 0; i < length; i++)
            {
                arrStat[i]              = new OPCItemState();
                arrStat[i].Error        = Marshal.ReadInt32((IntPtr)num3);
                num3                   += 4;
                arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)num4);
                if (HRESULTS.Succeeded(arrStat[i].Error))
                {
                    if (Marshal.ReadInt16((IntPtr)(num4 + 0x10)) == 10)
                    {
                        arrStat[i].Error = Marshal.ReadInt32((IntPtr)(num4 + 0x18));
                    }
                    arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(num4 + 4));
                    arrStat[i].Quality   = Marshal.ReadInt16((IntPtr)(num4 + 12));
                    arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(num4 + 0x10));
                    DUMMY_VARIANT.VariantClear((IntPtr)(num4 + 0x10));
                }
                else
                {
                    arrStat[i].DataValue = null;
                }
                num4 += 0x20;
            }
            Marshal.FreeCoTaskMem(ptr);
            Marshal.FreeCoTaskMem(ptr2);
            return(hresultcode == 0);
        }
Пример #5
0
        public bool GetItemProperties(string itemID, int[] propertyIDs, out OPCPropertyData[] propertiesData)
        {
            IntPtr ptr;
            IntPtr ptr2;

            propertiesData = null;
            int length = propertyIDs.Length;

            if (length < 1)
            {
                return(false);
            }
            int hresultcode = this.ifItmProps.GetItemProperties(itemID, length, propertyIDs, out ptr, out ptr2);

            if (HRESULTS.Failed(hresultcode))
            {
                Marshal.ThrowExceptionForHR(hresultcode);
            }
            int num3 = (int)ptr;
            int num4 = (int)ptr2;

            if ((num3 == 0) || (num4 == 0))
            {
                Marshal.ThrowExceptionForHR(-2147467260);
            }
            propertiesData = new OPCPropertyData[length];
            for (int i = 0; i < length; i++)
            {
                propertiesData[i]            = new OPCPropertyData();
                propertiesData[i].PropertyID = propertyIDs[i];
                propertiesData[i].Error      = Marshal.ReadInt32((IntPtr)num4);
                num4 += 4;
                if (propertiesData[i].Error == 0)
                {
                    propertiesData[i].Data = Marshal.GetObjectForNativeVariant((IntPtr)num3);
                    DUMMY_VARIANT.VariantClear((IntPtr)num3);
                }
                else
                {
                    propertiesData[i].Data = null;
                }
                num3 += DUMMY_VARIANT.ConstSize;
            }
            Marshal.FreeCoTaskMem(ptr);
            Marshal.FreeCoTaskMem(ptr2);
            return(hresultcode == 0);
        }
        public void Next(int enumcountmax, out OPCItemAttributes[] attributes)
        {
            IntPtr ptr;
            int    num;

            attributes = null;
            this.ifEnum.Next(enumcountmax, out ptr, out num);
            int num2 = (int)ptr;

            if (((num2 != 0) && (num > 0)) && (num <= enumcountmax))
            {
                attributes = new OPCItemAttributes[num];
                for (int i = 0; i < num; i++)
                {
                    attributes[i] = new OPCItemAttributes();
                    IntPtr ptr2 = (IntPtr)Marshal.ReadInt32((IntPtr)num2);
                    attributes[i].AccessPath = Marshal.PtrToStringUni(ptr2);
                    Marshal.FreeCoTaskMem(ptr2);
                    ptr2 = (IntPtr)Marshal.ReadInt32((IntPtr)(num2 + 4));
                    attributes[i].ItemID = Marshal.PtrToStringUni(ptr2);
                    Marshal.FreeCoTaskMem(ptr2);
                    attributes[i].Active            = Marshal.ReadInt32((IntPtr)(num2 + 8)) != 0;
                    attributes[i].HandleClient      = Marshal.ReadInt32((IntPtr)(num2 + 12));
                    attributes[i].HandleServer      = Marshal.ReadInt32((IntPtr)(num2 + 0x10));
                    attributes[i].AccessRights      = (OPCACCESSRIGHTS)Marshal.ReadInt32((IntPtr)(num2 + 20));
                    attributes[i].RequestedDataType = (VarEnum)Marshal.ReadInt16((IntPtr)(num2 + 0x20));
                    attributes[i].CanonicalDataType = (VarEnum)Marshal.ReadInt16((IntPtr)(num2 + 0x22));
                    attributes[i].EUType            = (OPCEUTYPE)Marshal.ReadInt32((IntPtr)(num2 + 0x24));
                    attributes[i].EUInfo            = Marshal.GetObjectForNativeVariant((IntPtr)(num2 + 40));
                    DUMMY_VARIANT.VariantClear((IntPtr)(num2 + 40));
                    int num4 = Marshal.ReadInt32((IntPtr)(num2 + 0x1c));
                    if (num4 != 0)
                    {
                        int length = Marshal.ReadInt32((IntPtr)(num2 + 0x18));
                        if (length > 0)
                        {
                            attributes[i].Blob = new byte[length];
                            Marshal.Copy((IntPtr)num4, attributes[i].Blob, 0, length);
                        }
                        Marshal.FreeCoTaskMem((IntPtr)num4);
                    }
                    num2 += 0x38;
                }
                Marshal.FreeCoTaskMem(ptr);
            }
        }
Пример #7
0
        public OPCItemState[] Read(int[] arrHSrv)
        {
            OPCItemState[] arrStat;
            arrStat = null;
            int    count = arrHSrv.Length;
            IntPtr ptrStat;
            IntPtr ptrErr;



            int hresult = ifSync.Read(OPCDATASOURCE.OPC_DS_DEVICE, count, arrHSrv, out ptrStat, out ptrErr);

            if (HRESULTS.Failed(hresult))
            {
                #region 新建组读取
                values = new List <ItemValue>();
                readed = false;

                OPC.Data.OpcGroup grouop = new OpcGroup("wef2", true, 500, 500, 0);
                this.group = grouop;
                this.Server.OpcGroups.Add(grouop);
                grouop.DataChanged += new DataChangeEventHandler(grouop_DataChanged);
                int index = 0;

                foreach (int hid in arrHSrv)
                {
                    OPCItem item = this.Items.GetItemByServerHandler(hid);
                    values.Add(new ItemValue(item.ID, null));
                }

                foreach (int hid in arrHSrv)
                {
                    OPCItem item    = this.Items.GetItemByServerHandler(hid);
                    OPCItem newItem = new OPCItem(item.ID, index);
                    grouop.Items.Add(newItem);
                    index++;
                }

                while (true)
                {
                    if (readed)
                    {
                        OPCItemState[] states = new OPCItemState[arrHSrv.Length];
                        for (int i = 0; i < states.Length; i++)
                        {
                            states[i] = values[i].value;
                        }
                        this.group = null;
                        grouop.Items.Clear();
                        this.Server.OpcGroups.Remove(grouop);
                        return(states);
                    }
                    Thread.Sleep(20);
                }
                #endregion

                if (HRESULTS.Failed(hresult))
                {
                    throw (new Exception("读取Item值出错,public OPCItemState[] Read(int[] arrHSrv)函数ifSync.Read(OPCDATASOURCE.OPC_DS_CACHE, count, arrHSrv, out ptrStat, out ptrErr)语句"));
                }
            }

            int runErr  = (int)ptrErr;
            int runStat = (int)ptrStat;
            if ((runErr == 0) || (runStat == 0))
            {
                Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT);
            }

            arrStat = new OPCItemState[count];
            for (int i = 0; i < count; i++)
            {                                                                                                           // WORKAROUND !!!
                OPCItemState item = new OPCItemState();
                arrStat[i] = item;
                item.Error = Marshal.ReadInt32((IntPtr)runErr);
                runErr    += 4;

                item.HandleClient = Marshal.ReadInt32((IntPtr)runStat);

                if (HRESULTS.Succeeded(item.Error))
                {
                    short vt = Marshal.ReadInt16((IntPtr)(runStat + 16));
                    if (vt == (short)VarEnum.VT_ERROR)
                    {
                        item.Error = Marshal.ReadInt32((IntPtr)(runStat + 24));
                    }
                    try
                    {
                        item.TimeStamp = DateTime.FromFileTime(Marshal.ReadInt64((IntPtr)(runStat + 4)));
                    }
                    catch
                    {
                    }
                    try
                    {
                        item.QualityString = OpcGroup.QualityToString(Marshal.ReadInt16((IntPtr)(runStat + 12)));
                    }
                    catch
                    {
                    }
                    item.DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16));
                    DUMMY_VARIANT.VariantClear((IntPtr)(runStat + 16));
                }
                else
                {
                    item.DataValue = null;
                }

                runStat += 32;
            }

            Marshal.FreeCoTaskMem(ptrStat);
            Marshal.FreeCoTaskMem(ptrErr);
            return(arrStat);
            //if (hresult == HRESULTS.S_OK)
            //{
            //    return arrStat;
            //}
            //else
            //{
            //    return null;
            //}
        }