コード例 #1
0
ファイル: BaseMachine.cs プロジェクト: cagnet/Modbus.Net
 /// <summary>
 ///     断开设备
 /// </summary>
 /// <returns>是否断开成功</returns>
 public bool Disconnect()
 {
     return(BaseUtility.Disconnect());
 }
コード例 #2
0
ファイル: BaseMachine.cs プロジェクト: cagnet/Modbus.Net
 /// <summary>
 ///     连接设备
 /// </summary>
 /// <returns>是否连接成功</returns>
 public async Task <bool> ConnectAsync()
 {
     return(await BaseUtility.ConnectAsync());
 }
コード例 #3
0
ファイル: BaseMachine.cs プロジェクト: cagnet/Modbus.Net
        /// <summary>
        ///     写入数据
        /// </summary>
        /// <param name="setDataType">写入类型</param>
        /// <param name="values">需要写入的数据字典,当写入类型为Address时,键为需要写入的地址,当写入类型为CommunicationTag时,键为需要写入的单元的描述</param>
        /// <returns>是否写入成功</returns>
        public async Task <bool> SetDatasAsync(MachineSetDataType setDataType, Dictionary <string, double> values)
        {
            try
            {
                //检测并连接设备
                if (!BaseUtility.IsConnected)
                {
                    await BaseUtility.ConnectAsync();
                }
                //如果设备无法连接,终止
                if (!BaseUtility.IsConnected)
                {
                    return(false);
                }
                var addresses = new List <AddressUnit>();
                //遍历每个要设置的值
                foreach (var value in values)
                {
                    //根据设置类型找到对应的地址描述
                    AddressUnit address = null;
                    switch (setDataType)
                    {
                    case MachineSetDataType.Address:
                    {
                        address =
                            GetAddresses.SingleOrDefault(
                                p =>
                                AddressFormater.FormatAddress(p.Area, p.Address, p.SubAddress) == value.Key ||
                                (p.DataType != typeof(bool) &&
                                 AddressFormater.FormatAddress(p.Area, p.Address) == value.Key));
                        break;
                    }

                    case MachineSetDataType.CommunicationTag:
                    {
                        address =
                            GetAddresses.SingleOrDefault(p => p.CommunicationTag == value.Key);
                        break;
                    }

                    case MachineSetDataType.Name:
                    {
                        address = GetAddresses.SingleOrDefault(p => p.Name == value.Key);
                        break;
                    }
                    }
                    //地址为空报错
                    if (address == null)
                    {
                        Console.WriteLine($"Machine {ConnectionToken} Address {value.Key} doesn't exist.");
                        continue;
                    }
                    //不能写报错
                    if (!address.CanWrite)
                    {
                        Console.WriteLine($"Machine {ConnectionToken} Address {value.Key} cannot write.");
                        continue;
                    }
                    addresses.Add(address);
                }
                //将地址编码成与实际设备通讯的地址
                var communcationUnits = AddressCombinerSet.Combine(addresses);
                //遍历每条通讯的连续地址
                foreach (var communicateAddress in communcationUnits)
                {
                    //编码开始地址
                    var addressStart = AddressFormater.FormatAddress(communicateAddress.Area,
                                                                     communicateAddress.Address);

                    var datasReturn = await BaseUtility.GetDatasAsync(
                        AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, 0),
                        (int)
                        Math.Ceiling(communicateAddress.GetCount *
                                     BigEndianValueHelper.Instance.ByteLength[
                                         communicateAddress.DataType.FullName]));

                    var valueHelper = ValueHelper.GetInstance(BaseUtility.Endian);
                    //如果设备本身能获取到数据但是没有数据
                    var datas = datasReturn;

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

                    foreach (var addressUnit in communicateAddress.OriginalAddresses)
                    {
                        //字节坐标地址
                        var byteCount =
                            AddressHelper.MapProtocalGetCountToAbstractByteCount(
                                addressUnit.Address - communicateAddress.Address +
                                addressUnit.SubAddress * 0.125 /
                                AddressTranslator.GetAreaByteLength(communicateAddress.Area),
                                AddressTranslator.GetAreaByteLength(communicateAddress.Area), 0);
                        //字节坐标主地址
                        var mainByteCount = (int)byteCount;
                        //字节坐标自地址
                        var localByteCount = (int)((byteCount - (int)byteCount) * 8);

                        //协议坐标地址
                        var localPos = byteCount / AddressTranslator.GetAreaByteLength(communicateAddress.Area);
                        //协议坐标子地址
                        var subPos =
                            (int)
                            ((localPos - (int)localPos) /
                             (0.125 / AddressTranslator.GetAreaByteLength(communicateAddress.Area)));
                        //协议主地址字符串
                        var address = AddressFormater.FormatAddress(communicateAddress.Area,
                                                                    communicateAddress.Address + (int)localPos, subPos);
                        //协议完整地址字符串
                        var address2 = subPos != 0
                            ? null
                            : AddressFormater.FormatAddress(communicateAddress.Area,
                                                            communicateAddress.Address + (int)localPos);
                        //获取写入类型
                        var dataType = addressUnit.DataType;
                        switch (setDataType)
                        {
                        case MachineSetDataType.Address:
                        {
                            //获取要写入的值
                            var value =
                                values.SingleOrDefault(
                                    p => p.Key == address || (address2 != null && p.Key == address2));
                            //将要写入的值加入队列
                            var data = Convert.ChangeType(value.Value / addressUnit.Zoom, dataType);

                            if (!valueHelper.SetValue(datas, mainByteCount, localByteCount, data))
                            {
                                return(false);
                            }
                            break;
                        }

                        case MachineSetDataType.CommunicationTag:
                        {
                            var value = values.SingleOrDefault(p => p.Key == addressUnit.CommunicationTag);
                            var data  = Convert.ChangeType(value.Value / addressUnit.Zoom, dataType);
                            if (!valueHelper.SetValue(datas, mainByteCount, localByteCount, data))
                            {
                                return(false);
                            }
                            break;
                        }

                        case MachineSetDataType.Name:
                        {
                            var value = values.SingleOrDefault(p => p.Key == addressUnit.Name);
                            var data  = Convert.ChangeType(value.Value / addressUnit.Zoom, dataType);
                            if (!valueHelper.SetValue(datas, mainByteCount, localByteCount, data))
                            {
                                return(false);
                            }
                            break;
                        }
                        }
                    }
                    //写入数据
                    await
                    BaseUtility.SetDatasAsync(addressStart,
                                              valueHelper.ByteArrayToObjectArray(datas,
                                                                                 new KeyValuePair <Type, int>(communicateAddress.DataType, communicateAddress.GetCount)));
                }
                //如果不保持连接,断开连接
                if (!KeepConnect)
                {
                    BaseUtility.Disconnect();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(ConnectionToken + " " + e.Message);
                return(false);
            }
            return(true);
        }
コード例 #4
0
ファイル: BaseMachine.cs プロジェクト: cagnet/Modbus.Net
 /// <summary>
 ///     连接设备
 /// </summary>
 /// <returns>是否连接成功</returns>
 public bool Connect()
 {
     return(BaseUtility.Connect());
 }
コード例 #5
0
ファイル: BaseMachine.cs プロジェクト: cagnet/Modbus.Net
        /// <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);
            }
        }