Exemple #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);                                                                                           //项添加到组
            }
        }
Exemple #2
0
        /// <summary>
        /// 添加组
        /// </summary>
        /// <param name="opcGroup"></param>
        public void AddGroup(OpcGroup opcGroup)
        {
            GCHandle hTimeBias        = GCHandle.Alloc(0, GCHandleType.Pinned);//为指定的对象分配句柄。
            GCHandle hPercendDeadBand = GCHandle.Alloc(0, GCHandleType.Pinned);
            int      updateRate;
            Guid     riid = typeof(IOPCItemMgt).GUID;

            if (string.IsNullOrEmpty(opcGroup.GroupName))//组名称为空,给个默认的组名
            {
                opcGroup.GroupName = _defaultGroupName;
                _defaultGroupName  = "Group" + (Convert.ToInt32(_defaultGroupName.Remove(0, 5)) + 1);
            }
            //if (opcGroup.ClientGroupHandle == 0)
            //{
            //    opcGroup.ClientGroupHandle = _defaultClientGroupHandle;
            //    _defaultClientGroupHandle++;
            //}
            try
            {
                opcServer.AddGroup(opcGroup.GroupName, opcGroup.IsActive, opcGroup.RequestedUpdateRate, opcGroup.ClientGroupHandle,
                                   hTimeBias.AddrOfPinnedObject(), hPercendDeadBand.AddrOfPinnedObject(), opcGroup.LocalId, out opcGroup.ServerGroupHandle,
                                   out updateRate, ref riid, out opcGroup.ObjGroup);
                // _lstOpcGroup.Add(opcGroup);
                OpcGroups.Add(opcGroup.ClientGroupHandle, opcGroup);
                Guid iid = typeof(IOPCDataCallback).GUID;                                                     //为所有的异步调用创建回调
                opcGroup.ConnectionPointContainer.FindConnectionPoint(ref iid, out opcGroup.ConnectionPoint); //为OPCServer的连接点与客户端接收点之间建立连接
                opcGroup.ConnectionPoint.Advise(opcGroup, out opcGroup.dwCookie);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (hTimeBias.IsAllocated)
                {
                    hTimeBias.Free();
                }
                if (hPercendDeadBand.IsAllocated)
                {
                    hPercendDeadBand.Free();
                }
            }
        }