Пример #1
0
        /// <summary>
        /// opc注册组和数据点
        /// </summary>
        /// <param name="device"></param>
        /// <param name="configs"></param>
        public static void OpcRegister(DC_DEVICE device, List <DC_DATA_CONFIG> configs)
        {
            OpcGroup opcGroup = new OpcGroup(device.ID); //创建组对象

            OpcServer.Instance.AddGroup(opcGroup);       //组添加到server
            opcGroup.DataReceived += OpcGroup_DataReceived;
            opcGroup.IsSubscribed  = true;
            foreach (var config in configs)
            {
                OpcItem opcItem = new OpcItem(config.ID, device.CHANNEL + ":[" + device.S7CONNECTION + "]" + config.MEMORY_ADDRESS); //创建数据项对象
                opcItem.IsActive = config.SUBSCRIPTION == 1?1:0;                                                                     //当为0时,可读写,但不会订阅数据;为1时,可读写,会订阅数据
                opcGroup.AddItem(opcItem);                                                                                           //项添加到组
            }
        }
Пример #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;
                }
            }
        }