/// <summary>
        ///     构造函数
        /// </summary>
        /// <param name="returnFunc">返回值的处理函数</param>
        /// <param name="getDataType">返回值的键类型</param>
        /// <param name="timeout">任务的超时时间</param>
        public TaskItemGetData(Action <DataReturnDef> returnFunc, MachineGetDataType getDataType, int timeout = 100000)
        {
            Name        = "GetDatas";
            TimeoutTime = timeout;
            Invoke      = async(machine, tasks, parameters, timeoutTime) =>
            {
                var cts = new CancellationTokenSource();
                cts.CancelAfter(TimeSpan.FromMilliseconds(timeoutTime));
                var ans =
                    await tasks.StartNew(
                        async() => await machine.GetMachineMethods <IMachineMethodData>()
                        .GetDatasAsync(
                            getDataType).WithCancellation(cts.Token)).Unwrap();

                return(new DataReturnDef
                {
                    MachineId = machine.GetMachineIdString(),
                    ReturnValues = ans
                });
            };
            Params = null;
            Return = returnFunc;
        }
 /// <summary>
 ///     构造函数
 /// </summary>
 /// <param name="returnFunc">返回值的处理函数</param>
 /// <param name="getDataType">返回值的键类型</param>
 /// <param name="getCycle">循环间隔(毫秒)</param>
 /// <param name="sleepCycle">设备离线时的循环间隔(毫秒)</param>
 /// <param name="timeout">任务的超时时间</param>
 public TaskItemGetData(Action <DataReturnDef> returnFunc, MachineGetDataType getDataType, int getCycle,
                        int sleepCycle, int timeout = 100000) : this(returnFunc, getDataType, timeout)
 {
     TimerDisconnectedTime = sleepCycle;
     TimerTime             = getCycle;
 }
Exemple #3
0
        /// <summary>
        ///     读取数据
        /// </summary>
        /// <returns>从设备读取的数据</returns>
        public async Task <Dictionary <string, ReturnUnit> > GetDatasAsync(MachineGetDataType getDataType)
        {
            try
            {
                var ans = new Dictionary <string, ReturnUnit>();
                //检测并连接设备
                if (!BaseUtility.IsConnected)
                {
                    await BaseUtility.ConnectAsync();
                }
                //如果无法连接,终止
                if (!BaseUtility.IsConnected)
                {
                    return(null);
                }
                //遍历每一个实际向设备获取数据的连续地址
                foreach (var communicateAddress in CommunicateAddresses)
                {
                    //获取数据
                    var datas =
                        await
                        BaseUtility.GetDatasAsync(
                            AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address,
                                                          communicateAddress.SubAddress),
                            (int)
                            Math.Ceiling(communicateAddress.GetCount *
                                         BigEndianValueHelper.Instance.ByteLength[
                                             communicateAddress.DataType.FullName]));


                    //如果没有数据,终止
                    if (datas == null || (datas.Length != 0 && datas.Length <
                                          (int)
                                          Math.Ceiling(communicateAddress.GetCount *
                                                       BigEndianValueHelper.Instance.ByteLength[
                                                           communicateAddress.DataType.FullName])))
                    {
                        return(null);
                    }


                    foreach (var address in communicateAddress.OriginalAddresses)
                    {
                        //字节坐标的位置
                        var localPos = AddressHelper.MapProtocalCoordinateToAbstractCoordinate(address.Address,
                                                                                               communicateAddress.Address,
                                                                                               AddressTranslator.GetAreaByteLength(communicateAddress.Area)) +
                                       address.SubAddress * 0.125;
                        //字节坐标的主地址位置
                        var localMainPos = (int)localPos;
                        //字节坐标的子地址位置
                        var localSubPos = (int)((localPos - localMainPos) * 8);

                        //根据类型选择返回结果的键是通讯标识还是地址
                        string key;
                        switch (getDataType)
                        {
                        case MachineGetDataType.CommunicationTag:
                        {
                            key = address.CommunicationTag;
                            break;
                        }

                        case MachineGetDataType.Address:
                        {
                            key = AddressFormater.FormatAddress(address.Area, address.Address, address.SubAddress);
                            break;
                        }

                        case MachineGetDataType.Name:
                        {
                            key = address.Name;
                            break;
                        }

                        default:
                        {
                            key = address.CommunicationTag;
                            break;
                        }
                        }

                        //如果没有数据返回空
                        if (datas.Length == 0)
                        {
                            ans.Add(key, new ReturnUnit
                            {
                                PlcValue   = null,
                                UnitExtend = address.UnitExtend
                            });
                        }
                        else
                        {
                            //将获取的数据和对应的通讯标识对应
                            ans.Add(key,
                                    new ReturnUnit
                            {
                                PlcValue =
                                    Convert.ToDouble(
                                        ValueHelper.GetInstance(BaseUtility.Endian)
                                        .GetValue(datas, ref localMainPos, ref localSubPos,
                                                  address.DataType)) * address.Zoom,
                                UnitExtend = address.UnitExtend
                            });
                        }
                    }
                }
                //如果不保持连接,断开连接
                if (!KeepConnect)
                {
                    BaseUtility.Disconnect();
                }
                //返回数据
                if (ans.All(p => p.Value.PlcValue == null))
                {
                    ans = null;
                }
                ErrorCount = 0;
                return(ans);
            }
            catch (Exception e)
            {
                Console.WriteLine(ConnectionToken + " " + e.Message);
                ErrorCount++;
                if (ErrorCount >= _maxErrorCount)
                {
                    Disconnect();
                }
                return(null);
            }
        }
Exemple #4
0
 /// <summary>
 ///     读取数据
 /// </summary>
 /// <returns>从设备读取的数据</returns>
 public Dictionary <string, ReturnUnit> GetDatas(MachineGetDataType getDataType)
 {
     return(AsyncHelper.RunSync(() => GetDatasAsync(getDataType)));
 }