예제 #1
0
        /// <summary>
        /// 添加项
        /// </summary>
        /// <param name="lstOpcItem"></param>
        public void AddItems(List <OpcItem> lstOpcItem)
        {
            OPCITEMDEF[] itemArray = new OPCITEMDEF[lstOpcItem.Count];
            for (int i = 0; i < itemArray.Length; i++)
            {
                itemArray[i].szAccessPath        = lstOpcItem[i].AccessPath;
                itemArray[i].szItemID            = lstOpcItem[i].PlcAddress;
                itemArray[i].bActive             = lstOpcItem[i].IsActive;
                itemArray[i].hClient             = lstOpcItem[i].ClientItemHandle;
                itemArray[i].dwBlobSize          = lstOpcItem[i].dwBlobSize;
                itemArray[i].pBlob               = lstOpcItem[i].pBlob;
                itemArray[i].vtRequestedDataType = lstOpcItem[i].RequestedDataType;
            }
            IntPtr pAddResults = IntPtr.Zero;
            IntPtr pErrors     = IntPtr.Zero;

            try
            {
                OPCItemMgt.AddItems(itemArray.Length, itemArray, out pAddResults, out pErrors);
                //获取ServerItemrHandles
                int[] errors = new int[itemArray.Length];
                Marshal.Copy(pErrors, errors, 0, itemArray.Length);
                IntPtr pos = pAddResults;
                for (int i = 0; i < errors.Length; i++)
                {
                    if (errors[i] == 0)
                    {
                        if (i != 0)
                        {
                            pos = new IntPtr(pos.ToInt32() + Marshal.SizeOf(typeof(OPCITEMRESULT)));
                        }
                        OPCITEMRESULT result = (OPCITEMRESULT)Marshal.PtrToStructure(pos, typeof(OPCITEMRESULT));
                        lstOpcItem[i].ServerItemHandle = result.hServer;
                        OpcItems.Add(lstOpcItem[i].ClientItemHandle, lstOpcItem[i]);
                    }
                    else
                    {
                        throw new ApplicationException($"添加组[{GroupName}]时,添加项[{itemArray[i].szItemID}]失败!");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pAddResults != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pAddResults);
                    pAddResults = IntPtr.Zero;
                }
                if (pErrors != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pErrors);
                    pErrors = IntPtr.Zero;
                }
            }
        }
예제 #2
0
        /// <summary>
        /// 添加一项
        /// </summary>
        /// <param name="opcItem"></param>
        public void AddItem(OpcItem opcItem)
        {
            OPCITEMDEF item = new OPCITEMDEF();

            item.szAccessPath        = opcItem.AccessPath;
            item.szItemID            = opcItem.PlcAddress;
            item.bActive             = opcItem.IsActive;
            item.hClient             = opcItem.ClientItemHandle;
            item.dwBlobSize          = opcItem.dwBlobSize;
            item.pBlob               = opcItem.pBlob;
            item.vtRequestedDataType = opcItem.RequestedDataType;
            IntPtr pAddResults = IntPtr.Zero;
            IntPtr pErrors     = IntPtr.Zero;

            try
            {
                OPCItemMgt.AddItems(1, new OPCITEMDEF[] { item }, out pAddResults, out pErrors);
                //获取ServerItemrHandles
                int[] errors = new int[1];
                Marshal.Copy(pErrors, errors, 0, 1);
                IntPtr pos = pAddResults;
                if (errors[0] == 0)
                {
                    OPCITEMRESULT result = (OPCITEMRESULT)Marshal.PtrToStructure(pos, typeof(OPCITEMRESULT));
                    opcItem.ServerItemHandle = result.hServer;
                    OpcItems.Add(opcItem.ClientItemHandle, opcItem);
                }
                else
                {
                    throw new ApplicationException($"添加组[{GroupName}]时,添加项[{item.szItemID}]失败!");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (pAddResults != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pAddResults);
                    pAddResults = IntPtr.Zero;
                }
                if (pErrors != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(pErrors);
                    pErrors = IntPtr.Zero;
                }
            }
        }