Пример #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 ViewItem(string opcid)
        {
            try
            {
                RemoveItem();                   // first remove previous item if any

                itmHandleClient = 1234;
                OPCItemDef[] aD = new OPCItemDef[1];
                aD[0] = new OPCItemDef(opcid, true, itmHandleClient, VarEnum.VT_EMPTY);
                OPCItemResult[] arrRes;
                theGrp.AddItems(aD, out arrRes);
                if (arrRes == null)
                {
                    return(false);
                }
                if (arrRes[0].Error != HRESULTS.S_OK)
                {
                    return(false);
                }

                btnItemMore.Enabled = true;
                itmHandleServer     = arrRes[0].HandleServer;
                itmAccessRights     = arrRes[0].AccessRights;
                itmTypeCode         = VT2TypeCode(arrRes[0].CanonicalDataType);

                txtItemID.Text       = opcid;
                txtItemDataType.Text = DUMMY_VARIANT.VarEnumToString(arrRes[0].CanonicalDataType);

                if ((itmAccessRights & OPCACCESSRIGHTS.OPC_READABLE) != 0)
                {
                    int cancelID;
                    theGrp.Refresh2(OPCDATASOURCE.OPC_DS_DEVICE, 7788, out cancelID);
                }
                else
                {
                    txtItemValue.Text = "no read access";
                }

                if (itmTypeCode != TypeCode.Object)                                     // Object=failed!
                {
                    // check if write is premitted
                    if ((itmAccessRights & OPCACCESSRIGHTS.OPC_WRITEABLE) != 0)
                    {
                        btnItemWrite.Enabled = true;
                    }
                }
            }
            catch (COMException)
            {
                MessageBox.Show(this, "AddItem OPC error!", "ViewItem", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            return(true);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
            }
        }
Пример #8
0
        public void TestVarEnumToString01()
        {
            System.Runtime.InteropServices.VarEnum varEnum = (System.Runtime.InteropServices.VarEnum)DUMMY_VARIANT.VT_ILLEGAL;
            //Test Procedure Call
            string str = DUMMY_VARIANT.VarEnumToString(varEnum);

            //Post Condition Check
            Assert.AreEqual("VT_ILLEGAL", str);

            //test
            varEnum = (System.Runtime.InteropServices.VarEnum)DUMMY_VARIANT.VT_ARRAY;
            //Test Procedure Call
            str = DUMMY_VARIANT.VarEnumToString(varEnum);
            //Post Condition Check
            Assert.AreEqual("VT_ARRAY | VT_EMPTY", str);

            //test
            varEnum = (System.Runtime.InteropServices.VarEnum)DUMMY_VARIANT.VT_BYREF;
            //Test Procedure Call
            str = DUMMY_VARIANT.VarEnumToString(varEnum);
            //Post Condition Check
            Assert.AreEqual("VT_BYREF | VT_EMPTY", str);

            //test
            varEnum = (System.Runtime.InteropServices.VarEnum)DUMMY_VARIANT.VT_VECTOR;
            //Test Procedure Call
            str = DUMMY_VARIANT.VarEnumToString(varEnum);
            //Post Condition Check
            Assert.AreEqual("VT_VECTOR | VT_EMPTY", str);

            //test
            varEnum = (System.Runtime.InteropServices.VarEnum)DUMMY_VARIANT.VT_TYPEMASK;
            //Test Procedure Call
            str = DUMMY_VARIANT.VarEnumToString(varEnum);


            //test
            varEnum = System.Runtime.InteropServices.VarEnum.VT_I4;
            //Test Procedure Call
            str = DUMMY_VARIANT.VarEnumToString(varEnum);
            //Post Condition Check
            Assert.AreEqual("VT_I4", str);
        }
Пример #9
0
        public void ListAllProperties(ref OpcServer theSrv, string itemid)
        {
            listPropsView.Items.Clear();

            string[]          istrs = new string[5];
            OPCProperty[]     props;
            OPCPropertyData[] propdata;
            int[]             propertyIDs = new int[1];
            OPCPropertyItem[] propitm;

            try
            {
                theSrv.QueryAvailableProperties(itemid, out props);
                if (props == null)
                {
                    return;
                }

                foreach (OPCProperty p in props)
                {
                    istrs[0] = p.PropertyID.ToString();
                    istrs[1] = p.Description;
                    istrs[2] = DUMMY_VARIANT.VarEnumToString(p.DataType);
                    istrs[3] = "";
                    istrs[4] = "";

                    propertyIDs[0] = p.PropertyID;
                    theSrv.GetItemProperties(itemid, propertyIDs, out propdata);
                    if (propdata != null)
                    {
                        if (propdata[0].Error != HRESULTS.S_OK)
                        {
                            istrs[3] = "!Error 0x" + propdata[0].Error.ToString("X");
                        }
                        else
                        {
                            istrs[3] = propdata[0].Data.ToString();
                        }
                    }

                    if (p.PropertyID > 6)
                    {
                        theSrv.LookupItemIDs(itemid, propertyIDs, out propitm);
                        if (propitm != null)
                        {
                            if (propitm[0].Error != HRESULTS.S_OK)
                            {
                                istrs[4] = "!Error 0x" + propitm[0].Error.ToString("X");
                            }
                            else
                            {
                                istrs[4] = propitm[0].newItemID;
                            }
                        }
                    }

                    listPropsView.Items.Add(new ListViewItem(istrs));
                }
            }
            catch (COMException)
            {
                MessageBox.Show(this, "QueryAvailableProperties failed!", "Item Properties", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // preselect top item in ListView
            listPropsView.Items[0].Selected = true;
        }
Пример #10
0
        public void TestCreateDUMMY_VARIANT01()
        {
            DUMMY_VARIANT dUMMY_VARIANT = CreateDUMMY_VARIANT01();

            Assert.IsNotNull(dUMMY_VARIANT);
        }
Пример #11
0
        public static DUMMY_VARIANT CreateDUMMY_VARIANT01()
        {
            DUMMY_VARIANT dUMMY_VARIANT = new DUMMY_VARIANT();

            return(dUMMY_VARIANT);
        }
Пример #12
0
 public override string ToString()
 {
     return(string.Concat(new object[] { "ID:", this.PropertyID, " '", this.Description, "' T:", DUMMY_VARIANT.VarEnumToString(this.DataType) }));
 }
Пример #13
0
 public override string ToString()
 {
     return("ID:" + PropertyID + " '" + Description + "' T:" + DUMMY_VARIANT.VarEnumToString(DataType));
 }
Пример #14
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;
            //}
        }