Ejemplo n.º 1
0
        /// <summary>
        /// 写入数据值(异步),并将结果发送给客户端
        /// </summary>
        /// <param name="opcUaDeviceInputParamEntity">需要封装NodeID,Value</param>
        /// <returns></returns>
        private async Task WriteAsync(Session session, ClientDataEntity clientDataEntity)
        {
            try
            {
                DeviceInputParamEntityBase deviceInputParamEntityBase = new DeviceInputParamEntityBase();
                deviceInputParamEntityBase.NodeId    = clientDataEntity.NodeId;
                deviceInputParamEntityBase.Value     = clientDataEntity.Value;
                deviceInputParamEntityBase.ValueType = clientDataEntity.ValueType;
                var deviceName = ProductionProcessEquipmentBusinessNodeMapContract.BusinessNodeMaps
                                 .FirstOrDefault(a => a.Id == clientDataEntity.ProductionProcessEquipmentBusinessNodeMapId)
                                 .DeviceNode.DeviceServerInfo.DeviceServerName;
                var opcUaDeviceOutParamEntity = await devices.FirstOrDefault(a => a.Name == deviceName)
                                                .Write <DeviceInputParamEntityBase, DeviceOutputParamEntityBase>(deviceInputParamEntityBase);

                clientDataEntity.StatusCode = opcUaDeviceOutParamEntity.StatusCode;
            }
            catch (Exception ex)
            {
                Logger.Error($"写入数据值(异步),并将结果发送给客户端异常! + \n {ex.ToString()}");
            }
            await SendAsync(session, GetMessage(JsonConvert.SerializeObject(new ClientDataEntity[] { clientDataEntity })));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 读取实时数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReadDataItem_Click(object sender, RoutedEventArgs e)
        {
            //string message = null;

            //string message = null;
            if (!opcUaDeviceHelper.IsConnected)
            {
                upMessage("请先连接服务器");
                return;
            }

            DeviceInputParamEntityBase deviceInputParamEntityBase = new DeviceInputParamEntityBase();

            deviceInputParamEntityBase.NodeId = "ns=2;s=通道 1.设备 1.标记 00001";
            //deviceInputParamEntityBase.ValueType = typeof(UInt16);
            //deviceInputParamEntityBase.ValueType = clientDataEntity.ValueType;
            //OpcUaDeviceHelper opcUaDevice = GetOpcUaDevice(clientDataEntity.KeyId);
            //var deviceName = ProductionProcessEquipmentBusinessNodeMapContract.BusinessNodeMaps
            //    .FirstOrDefault(a => a.Id == clientDataEntity.ProductionProcessEquipmentBusinessNodeMapId)
            //    .DeviceNode.DeviceServerInfo.DeviceServerName;
            var opcUaDeviceOutParamEntity = opcUaDeviceHelper.Read <DeviceInputParamEntityBase, DeviceOutputParamEntityBase>(deviceInputParamEntityBase).Result;

            upMessage(opcUaDeviceOutParamEntity.ToString());



            DeviceInputParamEntityBase deviceInputParamEntityBase2 = new DeviceInputParamEntityBase();

            deviceInputParamEntityBase2.NodeId = "ns=2;s=通道 1.设备 1.标记 00002";
            //deviceInputParamEntityBase2.ValueType = typeof(UInt16);


            var opcUaDeviceOutParamEntities =
                opcUaDeviceHelper.Reads <DeviceInputParamEntityBase, DeviceOutputParamEntityBase>(new DeviceInputParamEntityBase[] { deviceInputParamEntityBase, deviceInputParamEntityBase2 }).Result;

            foreach (var v in opcUaDeviceOutParamEntities)
            {
                upMessage(v.ToString());
            }

            //if (!opcUaDeviceHelper.IsConnected)
            //{
            //    message = DateTime.Now.ToString(dateString) + "请先连接服务器" + System.Environment.NewLine;
            //    asyncUpMessage(message);
            //    return;
            //}
            //OpcDataItem opcDataItem;
            //if (Equals(null, opcUaDeviceHelper.OpcDataItems) || opcUaDeviceHelper.OpcDataItems.Count < 1)
            //{
            //    message = DateTime.Now.ToString(dateString) + "没有数据点" + System.Environment.NewLine;
            //}
            //else
            //{
            //    //正常读取
            //    opcDataItem = opcUaDeviceHelper.OpcDataItems.FirstOrDefault().Clone() as OpcDataItem;
            //    opcDataItem.Name = opcDataItem.Name;
            //    opcDataItem = opcUaDeviceHelper.Read(opcDataItem);
            //    message = DateTime.Now.ToString("HH:mm:ss ffff ") + "读完成 " + (opcDataItem == null ? " " : opcDataItem.ToString()) + System.Environment.NewLine;
            //}
            //asyncUpMessage(message);
            //if (!Equals(null, opcUaDeviceHelper.OpcDataItems) && opcUaDeviceHelper.OpcDataItems.Count > 0)
            //{
            //    //无效读取
            //    var opcDataItem2 = opcUaDeviceHelper.OpcDataItems.LastOrDefault().Clone() as OpcDataItem;
            //    opcDataItem2.Name = opcDataItem2.Name + "xxx";
            //    opcDataItem2 = opcUaDeviceHelper.Read(opcDataItem2);
            //    message = DateTime.Now.ToString(dateString) + "读完成 " + (opcDataItem2 == null ? " " : opcDataItem2.ToString()) + System.Environment.NewLine;
            //    asyncUpMessage(message);
            //}
        }