diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs
index eba6b3c..fac9f5b 100644
--- a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs
+++ b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs
@@ -27,7 +27,7 @@ namespace Modbus.Net.Modbus
///
/// Modbus基础Api入口
///
- public class ModbusUtility : BaseUtility, IUtilityTime
+ public class ModbusUtility : BaseUtility, IUtilityMethodTime
{
///
/// Modbus协议类型
diff --git a/Modbus.Net/Modbus.Net/ComProtocalLinker.cs b/Modbus.Net/Modbus.Net/ComProtocalLinker.cs
index e596f0f..8ddcfc6 100644
--- a/Modbus.Net/Modbus.Net/ComProtocalLinker.cs
+++ b/Modbus.Net/Modbus.Net/ComProtocalLinker.cs
@@ -7,11 +7,26 @@ namespace Modbus.Net
///
public abstract class ComProtocalLinker : ProtocalLinker
{
+ ///
+ /// 构造器
+ ///
+ /// 波特率
+ /// 校验位
+ /// 停止位
+ /// 数据位
protected ComProtocalLinker(int baudRate, Parity parity, StopBits stopBits, int dataBits)
: this(ConfigurationManager.COM, baudRate, parity, stopBits, dataBits)
{
}
+ ///
+ /// 构造器
+ ///
+ /// 串口端口号
+ /// 波特率
+ /// 校验位
+ /// 停止位
+ /// 数据位
protected ComProtocalLinker(string com, int baudRate, Parity parity, StopBits stopBits, int dataBits)
{
//初始化连对象
diff --git a/Modbus.Net/src/Base.Common/AddressCombiner.cs b/Modbus.Net/src/Base.Common/AddressCombiner.cs
index f60226a..4ad03d5 100644
--- a/Modbus.Net/src/Base.Common/AddressCombiner.cs
+++ b/Modbus.Net/src/Base.Common/AddressCombiner.cs
@@ -29,6 +29,11 @@ namespace Modbus.Net
///
public class AddressCombinerContinus : AddressCombinerContinus
{
+ ///
+ /// 构造函数
+ ///
+ /// 地址转换器
+ /// 单个发送协议允许的数据最长长度(字节)
public AddressCombinerContinus(AddressTranslator addressTranslator, int maxLength) : base(addressTranslator, maxLength)
{
}
@@ -39,16 +44,32 @@ namespace Modbus.Net
///
public class AddressCombinerContinus : AddressCombiner where TKey : IEquatable
{
+ ///
+ /// 协议的数据最长长度(字节)
+ ///
protected int MaxLength { get; set; }
+ ///
+ /// 构造函数
+ ///
+ /// 地址转换器
+ /// 单个发送协议允许的数据最长长度(字节)
public AddressCombinerContinus(AddressTranslator addressTranslator, int maxLength)
{
AddressTranslator = addressTranslator;
MaxLength = maxLength;
}
+ ///
+ /// 地址转换器
+ ///
protected AddressTranslator AddressTranslator { get; set; }
+ ///
+ /// 组合地址
+ ///
+ /// 需要组合的地址
+ /// 组合后的地址
public override IEnumerable> Combine(IEnumerable> addresses)
{
//按从小到大的顺序对地址进行排序
@@ -218,6 +239,11 @@ namespace Modbus.Net
///
public class AddressCombinerSingle : AddressCombiner where TKey : IEquatable
{
+ ///
+ /// 组合地址
+ ///
+ /// 需要组合的地址
+ /// 组合后的地址
public override IEnumerable> Combine(IEnumerable> addresses)
{
return
@@ -249,6 +275,12 @@ namespace Modbus.Net
///
public class AddressCombinerNumericJump : AddressCombinerNumericJump
{
+ ///
+ /// 构造函数
+ ///
+ /// 需要跳过的字节个数
+ /// 单个协议允许的数据最长长度(字节)
+ /// 地址转换器
public AddressCombinerNumericJump(int jumpByteCount, int maxLength, AddressTranslator addressTranslator)
: base(jumpByteCount, maxLength, addressTranslator)
{
@@ -260,14 +292,28 @@ namespace Modbus.Net
///
public class AddressCombinerNumericJump : AddressCombinerContinus where TKey : IEquatable
{
+ ///
+ /// 构造函数
+ ///
+ /// 需要跳过的字节个数
+ /// 单个协议允许的数据最长长度(字节)
+ /// 地址转换器
public AddressCombinerNumericJump(int jumpByteCount, int maxLength, AddressTranslator addressTranslator)
: base(addressTranslator, maxLength)
{
JumpNumber = jumpByteCount;
}
+ ///
+ /// 跳过的地址长度
+ ///
private int JumpNumber { get; }
+ ///
+ /// 组合地址
+ ///
+ /// 需要组合的地址
+ /// 组合后的地址
public override IEnumerable> Combine(IEnumerable> addresses)
{
var continusAddresses = base.Combine(addresses).ToList();
@@ -339,6 +385,12 @@ namespace Modbus.Net
///
public class AddressCombinerPercentageJump : AddressCombinerPercentageJump
{
+ ///
+ /// 构造函数
+ ///
+ /// 允许跳过的字节数除以待组合的地址的字节数的百分比
+ /// 单个协议允许的数据最大长度
+ /// 地址转换器
public AddressCombinerPercentageJump(double percentage, int maxLength, AddressTranslator addressTranslator)
: base(percentage, maxLength, addressTranslator)
{
@@ -350,6 +402,12 @@ namespace Modbus.Net
///
public class AddressCombinerPercentageJump : AddressCombinerContinus where TKey : IEquatable
{
+ ///
+ /// 构造函数
+ ///
+ /// 允许跳过的字节数除以待组合的地址的字节数的百分比
+ /// 单个协议允许的数据最大长度
+ /// 地址转换器
public AddressCombinerPercentageJump(double percentage, int maxLength, AddressTranslator addressTranslator)
: base(addressTranslator, maxLength)
{
@@ -357,8 +415,16 @@ namespace Modbus.Net
Percentage = percentage;
}
+ ///
+ /// 跳过的百分比
+ ///
private double Percentage { get; }
+ ///
+ /// 组合地址
+ ///
+ /// 需要组合的地址
+ /// 组合后的地址
public override IEnumerable> Combine(IEnumerable> addresses)
{
var addressUnits = addresses as IList> ?? addresses.ToList();
diff --git a/Modbus.Net/src/Base.Common/AddressFormater.cs b/Modbus.Net/src/Base.Common/AddressFormater.cs
index c24307f..111b7c8 100644
--- a/Modbus.Net/src/Base.Common/AddressFormater.cs
+++ b/Modbus.Net/src/Base.Common/AddressFormater.cs
@@ -5,6 +5,12 @@
///
public abstract class AddressFormater
{
+ ///
+ /// 编码地址
+ ///
+ /// 地址所在的数据区域
+ /// 地址
+ /// 编码后的地址
public abstract string FormatAddress(string area, int address);
///
@@ -22,11 +28,24 @@
///
public class AddressFormaterBase : AddressFormater
{
+ ///
+ /// 编码地址
+ ///
+ /// 地址所在的数据区域
+ /// 地址
+ /// 编码后的地址
public override string FormatAddress(string area, int address)
{
return area + ":" + address;
}
+ ///
+ /// 编码地址
+ ///
+ /// 地址所在的数据区域
+ /// 地址
+ /// 子地址
+ /// 编码后的地址
public override string FormatAddress(string area, int address, int subAddress)
{
return area + ":" + address + ":" + subAddress;
diff --git a/Modbus.Net/src/Base.Common/AddressTranslator.cs b/Modbus.Net/src/Base.Common/AddressTranslator.cs
index cc09a15..39c1c68 100644
--- a/Modbus.Net/src/Base.Common/AddressTranslator.cs
+++ b/Modbus.Net/src/Base.Common/AddressTranslator.cs
@@ -7,18 +7,36 @@ namespace Modbus.Net
///
public class AddressDef
{
+ ///
+ /// 地址区域的字符串描述
+ ///
public string AreaString { get; set; }
+ ///
+ /// 地址区域的数字描述
+ ///
public int Area { get; set; }
+ ///
+ /// 地址
+ ///
public int Address { get; set; }
+ ///
+ /// 子地址
+ ///
public int SubAddress { get; set; }
}
///
- /// 区域数据定义类
+ /// 地址区域数据定义类
///
public class AreaOutputDef
{
+ ///
+ /// 地址区域的编码
+ ///
public int Code { get; set; }
+ ///
+ /// 地址区域的单个地址占用的字节数
+ ///
public double AreaWidth { get; set; }
}
@@ -48,6 +66,12 @@ namespace Modbus.Net
///
public class AddressTranslatorBase : AddressTranslator
{
+ ///
+ /// 地址转换
+ ///
+ /// 地址前地址
+ /// 是否为读取,是为读取,否为写入
+ /// Key为转换后的地址,Value为辅助码
public override AddressDef AddressTranslate(string address, bool isRead)
{
int num1, num2, num3;
@@ -79,6 +103,11 @@ namespace Modbus.Net
throw new FormatException();
}
+ ///
+ /// 获取区域中的单个地址占用的字节长度
+ ///
+ /// 区域名称
+ /// 字节长度
public override double GetAreaByteLength(string area)
{
return 1;
diff --git a/Modbus.Net/src/Base.Common/BaseMachine.cs b/Modbus.Net/src/Base.Common/BaseMachine.cs
index 10f8bdc..80d26c8 100644
--- a/Modbus.Net/src/Base.Common/BaseMachine.cs
+++ b/Modbus.Net/src/Base.Common/BaseMachine.cs
@@ -84,24 +84,48 @@ namespace Modbus.Net
Id
}
+ ///
+ /// 设备
+ ///
public abstract class BaseMachine : BaseMachine
{
+ ///
+ /// 构造器
+ ///
+ /// 需要与设备通讯的地址
protected BaseMachine(IEnumerable getAddresses) : base(getAddresses)
{
}
+ ///
+ /// 构造器
+ ///
+ /// 需要与设备通讯的地址
+ /// 是否保持连接
protected BaseMachine(IEnumerable getAddresses, bool keepConnect)
: base(getAddresses, keepConnect)
{
}
+ ///
+ /// 构造器
+ ///
+ /// 需要与设备通讯的地址
+ /// 是否保持连接
+ /// 从站地址
+ /// 主站地址
protected BaseMachine(IEnumerable getAddresses, bool keepConnect, byte slaveAddress,
byte masterAddress) : base(getAddresses, keepConnect, slaveAddress, masterAddress)
{
}
}
- public abstract class BaseMachine : IMachineData, IMachineProperty where TKey : IEquatable
+ ///
+ /// 设备
+ ///
+ /// 设备的Id类型
+ /// 设备中使用的AddressUnit的Id类型
+ public abstract class BaseMachine : IMachineMethodData, IMachineProperty where TKey : IEquatable
where TUnitKey : IEquatable
{
private readonly int _maxErrorCount = 3;
@@ -254,7 +278,7 @@ namespace Modbus.Net
//获取数据
var datas =
await
- BaseUtility.InvokeUtilityMethod>("GetDatasAsync",
+ BaseUtility.InvokeUtilityMethod>("GetDatasAsync",
AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address,
communicateAddress.SubAddress),
(int)
@@ -454,7 +478,7 @@ namespace Modbus.Net
var addressStart = AddressFormater.FormatAddress(communicateAddress.Area,
communicateAddress.Address);
- var datasReturn = await BaseUtility.InvokeUtilityMethod>("GetDatasAsync",
+ var datasReturn = await BaseUtility.InvokeUtilityMethod>("GetDatasAsync",
AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, 0),
(int)
Math.Ceiling(communicateAddress.GetCount*
@@ -544,7 +568,7 @@ namespace Modbus.Net
}
//写入数据
await
- BaseUtility.InvokeUtilityMethod>("SetDatasAsync",addressStart,
+ BaseUtility.InvokeUtilityMethod>("SetDatasAsync",addressStart,
valueHelper.ByteArrayToObjectArray(datas,
new KeyValuePair(communicateAddress.DataType, communicateAddress.GetCount)));
}
@@ -639,7 +663,7 @@ namespace Modbus.Net
}
}
- public class BaseMachineEqualityComparer : IEqualityComparer>
+ internal class BaseMachineEqualityComparer : IEqualityComparer>
where TKey : IEquatable
{
public bool Equals(IMachineProperty x, IMachineProperty y)
@@ -793,10 +817,7 @@ namespace Modbus.Net
public UnitExtend UnitExtend { get; set; }
}
- ///
- /// AddressUnit大小比较
- ///
- public struct AddressUnitEqualityComparer : IEqualityComparer> where TKey : IEquatable
+ internal struct AddressUnitEqualityComparer : IEqualityComparer> where TKey : IEquatable
{
public bool Equals(AddressUnit x, AddressUnit y)
{
diff --git a/Modbus.Net/src/Base.Common/BaseProtocal.cs b/Modbus.Net/src/Base.Common/BaseProtocal.cs
index 1d7101d..c4db1f3 100644
--- a/Modbus.Net/src/Base.Common/BaseProtocal.cs
+++ b/Modbus.Net/src/Base.Common/BaseProtocal.cs
@@ -22,16 +22,6 @@ namespace Modbus.Net
{
}
- ///
- /// 发送协议内容并接收,一般方法
- ///
- /// 写入的内容,使用对象数组描述
- /// 从设备获取的字节流
- public virtual byte[] SendReceive(params object[] content)
- {
- return AsyncHelper.RunSync(() => SendReceiveAsync(Endian, content));
- }
-
///
/// 发送协议内容并接收,一般方法
///
@@ -54,7 +44,7 @@ namespace Modbus.Net
///
/// 基本协议
///
- public abstract class BaseProtocal where TProtocalUnit : ProtocalUnit
+ public abstract class BaseProtocal : IProtocal where TProtocalUnit : ProtocalUnit
{
///
/// 构造器
@@ -67,9 +57,18 @@ namespace Modbus.Net
MasterAddress = masterAddress;
}
+ ///
+ /// 协议的端格式
+ ///
protected Endian Endian { get; set; }
-
+
+ ///
+ /// 从站地址
+ ///
public byte SlaveAddress { get; set; }
+ ///
+ /// 主站地址
+ ///
public byte MasterAddress { get; set; }
///
@@ -181,7 +180,22 @@ namespace Modbus.Net
return null;
}
- public virtual async Task SendReceiveAsync(params object[] content)
+ ///
+ /// 发送协议内容并接收,一般方法
+ ///
+ /// 写入的内容,使用对象数组描述
+ /// 从设备获取的字节流
+ public virtual byte[] SendReceive(params object[] content)
+ {
+ return AsyncHelper.RunSync(() => SendReceiveAsync(content));
+ }
+
+ ///
+ /// 发送协议内容并接收,一般方法
+ ///
+ /// 写入的内容,使用对象数组描述
+ /// 从设备获取的字节流
+ public virtual Task SendReceiveAsync(params object[] content)
{
throw new NotImplementedException();
}
diff --git a/Modbus.Net/src/Base.Common/BaseUtility.cs b/Modbus.Net/src/Base.Common/BaseUtility.cs
index 085ed67..830b401 100644
--- a/Modbus.Net/src/Base.Common/BaseUtility.cs
+++ b/Modbus.Net/src/Base.Common/BaseUtility.cs
@@ -48,7 +48,7 @@ namespace Modbus.Net
///
/// 基础Api入口
///
- public abstract class BaseUtility : IUtilityProperty, IUtilityData
+ public abstract class BaseUtility : IUtilityProperty, IUtilityMethodData
{
///
/// 协议收发主体
diff --git a/Modbus.Net/src/Base.Common/CRC16.cs b/Modbus.Net/src/Base.Common/CRC16.cs
index 2f9dc03..67594dc 100644
--- a/Modbus.Net/src/Base.Common/CRC16.cs
+++ b/Modbus.Net/src/Base.Common/CRC16.cs
@@ -7,15 +7,27 @@ using System;
namespace Modbus.Net
{
+ ///
+ /// CRC-LRC校验工具
+ ///
public class Crc16
{
private static Crc16 _crc16;
+ private Crc16()
+ {
+
+ }
+
///
/// CRC验证表
///
- public byte[] crc_table = new byte[512];
+ private byte[] crc_table = new byte[512];
+ ///
+ /// 获取校验工具实例
+ ///
+ ///
public static Crc16 GetInstance()
{
if (_crc16 == null)
@@ -88,12 +100,14 @@ namespace Modbus.Net
#endregion
+ #region LRC验证
+
///
/// 取模FF(255)
/// 取反+1
///
- ///
- ///
+ /// 待验证的LRC消息
+ /// LRC校验是否正确
public bool LrcEfficacy(string message)
{
var index = message.IndexOf(Environment.NewLine, StringComparison.Ordinal);
@@ -176,6 +190,15 @@ namespace Modbus.Net
return hexTotal == checkString;
}
+ #endregion
+
+ #region 生成LRC码
+
+ ///
+ /// 生成LRC校验码
+ ///
+ /// 需要生成的信息
+ /// 生成的校验码
public string GetLRC(byte[] code)
{
byte sum = 0;
@@ -183,9 +206,12 @@ namespace Modbus.Net
{
sum += b;
}
- sum = (byte) (~sum + 1); //取反+1
+ sum = (byte)(~sum + 1); //取反+1
var lrc = sum.ToString("X2");
return lrc;
}
+
+ #endregion
+
}
}
\ No newline at end of file
diff --git a/Modbus.Net/src/Base.Common/IMachineMethod.cs b/Modbus.Net/src/Base.Common/IMachineMethod.cs
index cac1f19..c5fc50b 100644
--- a/Modbus.Net/src/Base.Common/IMachineMethod.cs
+++ b/Modbus.Net/src/Base.Common/IMachineMethod.cs
@@ -6,6 +6,9 @@ using System.Threading.Tasks;
namespace Modbus.Net
{
+ ///
+ /// Machineдӿ
+ ///
public interface IMachineMethod
{
@@ -14,7 +17,7 @@ namespace Modbus.Net
///
/// Machineݶдӿ
///
- public interface IMachineData : IMachineMethod
+ public interface IMachineMethodData : IMachineMethod
{
///
/// ȡ
diff --git a/Modbus.Net/src/Base.Common/IProtocal.cs b/Modbus.Net/src/Base.Common/IProtocal.cs
index f7f2f18..b2b4fae 100644
--- a/Modbus.Net/src/Base.Common/IProtocal.cs
+++ b/Modbus.Net/src/Base.Common/IProtocal.cs
@@ -6,23 +6,27 @@ using System.Threading.Tasks;
namespace Modbus.Net
{
- public interface IProtocal
+ ///
+ /// 协议接口
+ ///
+ /// 向Connector传入的类型
+ /// 从Connector返回的类型
+ /// 协议单元的类型
+ public interface IProtocal where TProtocalUnit : IProtocalFormatting
{
///
/// 发送协议内容并接收,一般方法
///
- /// 是否是小端格式
/// 写入的内容,使用对象数组描述
/// 从设备获取的字节流
- byte[] SendReceive(bool isLittleEndian, params object[] content);
+ byte[] SendReceive(params object[] content);
///
/// 发送协议内容并接收,一般方法
///
- /// 是否是小端格式
/// 写入的内容,使用对象数组描述
/// 从设备获取的字节流
- Task SendReceiveAsync(bool isLittleEndian, params object[] content);
+ Task SendReceiveAsync(params object[] content);
///
/// 发送协议,通过传入需要使用的协议内容和输入结构
@@ -30,7 +34,7 @@ namespace Modbus.Net
/// 协议的实例
/// 输入信息的结构化描述
/// 输出信息的结构化描述
- IOutputStruct SendReceive(ProtocalUnit unit, IInputStruct content);
+ IOutputStruct SendReceive(TProtocalUnit unit, IInputStruct content);
///
/// 发送协议,通过传入需要使用的协议内容和输入结构
@@ -38,6 +42,6 @@ namespace Modbus.Net
/// 协议的实例
/// 输入信息的结构化描述
/// 输出信息的结构化描述
- Task SendReceiveAsync(ProtocalUnit unit, IInputStruct content);
+ Task SendReceiveAsync(TProtocalUnit unit, IInputStruct content);
}
}
diff --git a/Modbus.Net/src/Base.Common/IProtocalFormatting.cs b/Modbus.Net/src/Base.Common/IProtocalFormatting.cs
index 0fcad73..d494e8c 100644
--- a/Modbus.Net/src/Base.Common/IProtocalFormatting.cs
+++ b/Modbus.Net/src/Base.Common/IProtocalFormatting.cs
@@ -11,6 +11,8 @@
///
/// 协议转换的接口
///
+ /// 向Connector传入的数据类型
+ /// 从Connector返回的数据类型
public interface IProtocalFormatting
{
///
diff --git a/Modbus.Net/src/Base.Common/IProtocalLinker.cs b/Modbus.Net/src/Base.Common/IProtocalLinker.cs
index 9b03f50..94633ad 100644
--- a/Modbus.Net/src/Base.Common/IProtocalLinker.cs
+++ b/Modbus.Net/src/Base.Common/IProtocalLinker.cs
@@ -6,55 +6,60 @@ using System.Threading.Tasks;
namespace Modbus.Net
{
- public interface IProtocalLinker
+ ///
+ /// 协议连接器接口
+ ///
+ /// 向Connector传入的数据类型
+ /// 从Connector返回的数据类型
+ public interface IProtocalLinker
{
///
/// 发送并接收数据
///
/// 发送协议的内容
/// 接收协议的内容
- byte[] SendReceive(byte[] content);
+ TParamOut SendReceive(TParamIn content);
///
/// 发送并接收数据
///
/// 发送协议的内容
/// 接收协议的内容
- Task SendReceiveAsync(byte[] content);
+ Task SendReceiveAsync(TParamIn content);
///
/// 发送并接收数据,不进行协议扩展和收缩,用于特殊协议
///
/// 发送协议的内容
/// 接收协议的内容
- byte[] SendReceiveWithoutExtAndDec(byte[] content);
+ TParamOut SendReceiveWithoutExtAndDec(TParamIn content);
///
/// 发送并接收数据,不进行协议扩展和收缩,用于特殊协议
///
/// 发送协议的内容
/// 接收协议的内容
- Task SendReceiveWithoutExtAndDecAsync(byte[] content);
+ Task SendReceiveWithoutExtAndDecAsync(TParamIn content);
///
/// 检查接收的数据是否正确
///
/// 接收协议的内容
/// 协议是否是正确的
- bool? CheckRight(byte[] content);
+ bool? CheckRight(TParamOut content);
///
/// 协议内容扩展,发送时根据需要扩展
///
/// 扩展前的基本协议内容
/// 扩展后的协议内容
- byte[] BytesExtend(byte[] content);
+ TParamIn BytesExtend(TParamIn content);
///
/// 协议内容缩减,接收时根据需要缩减
///
/// 缩减前的完整协议内容
/// 缩减后的协议内容
- byte[] BytesDecact(byte[] content);
+ TParamOut BytesDecact(TParamOut content);
}
}
diff --git a/Modbus.Net/src/Base.Common/IProtocalLinkerBytesExtend.cs b/Modbus.Net/src/Base.Common/IProtocalLinkerBytesExtend.cs
index 4dafffe..77f168e 100644
--- a/Modbus.Net/src/Base.Common/IProtocalLinkerBytesExtend.cs
+++ b/Modbus.Net/src/Base.Common/IProtocalLinkerBytesExtend.cs
@@ -3,20 +3,28 @@
///
/// 协议字节伸缩
///
- public interface IProtocalLinkerBytesExtend
+ public interface IProtocalLinkerBytesExtend : IProtocalLinkerBytesExtend
+ {
+
+ }
+
+ ///
+ /// 协议字节伸缩
+ ///
+ public interface IProtocalLinkerBytesExtend
{
///
/// 协议扩展,协议内容发送前调用
///
/// 扩展前的原始协议内容
/// 扩展后的协议内容
- byte[] BytesExtend(byte[] content);
+ TParamIn BytesExtend(TParamIn content);
///
/// 协议收缩,协议内容接收后调用
///
/// 收缩前的完整协议内容
/// 收缩后的协议内容
- byte[] BytesDecact(byte[] content);
+ TParamOut BytesDecact(TParamOut content);
}
}
\ No newline at end of file
diff --git a/Modbus.Net/src/Base.Common/IUtilityMethod.cs b/Modbus.Net/src/Base.Common/IUtilityMethod.cs
index 8e6bca3..a6b9deb 100644
--- a/Modbus.Net/src/Base.Common/IUtilityMethod.cs
+++ b/Modbus.Net/src/Base.Common/IUtilityMethod.cs
@@ -6,15 +6,18 @@ using System.Threading.Tasks;
namespace Modbus.Net
{
+ ///
+ /// Utility方法读写接口
+ ///
public interface IUtilityMethod
{
}
///
- /// Utility的数据读写接口
+ /// Utility的数据读写接口
///
- public interface IUtilityData : IUtilityMethod
+ public interface IUtilityMethodData : IUtilityMethod
{
///
/// 获取数据
@@ -91,19 +94,19 @@ namespace Modbus.Net
}
///
- /// Utility的时间读写接口
+ /// Utility的时间读写接口
///
- public interface IUtilityTime : IUtilityMethod
+ public interface IUtilityMethodTime : IUtilityMethod
{
///
- /// 获取PLC时间
+ /// 获取PLC时间
///
/// PLC时间
Task GetTimeAsync();
///
- /// 设置PLC时间
+ /// 设置PLC时间
///
/// 设置PLC时间
/// 设置是否成功
diff --git a/Modbus.Net/src/Base.Common/ProtocalLinker.cs b/Modbus.Net/src/Base.Common/ProtocalLinker.cs
index 1ca94be..0d2bf45 100644
--- a/Modbus.Net/src/Base.Common/ProtocalLinker.cs
+++ b/Modbus.Net/src/Base.Common/ProtocalLinker.cs
@@ -9,6 +9,9 @@ namespace Modbus.Net
///
public abstract class ProtocalLinker : ProtocalLinker
{
+ ///
+ /// 传输连接器
+ ///
protected new BaseConnector BaseConnector;
///
@@ -70,10 +73,16 @@ namespace Modbus.Net
///
/// 基本的协议连接器
///
- public abstract class ProtocalLinker
+ public abstract class ProtocalLinker : IProtocalLinker
{
+ ///
+ /// 传输连接器
+ ///
protected BaseConnector BaseConnector;
+ ///
+ /// 通讯字符串
+ ///
public string ConnectionToken => BaseConnector.ConnectionToken;
///
diff --git a/Modbus.Net/src/Base.Common/ProtocalUnit.cs b/Modbus.Net/src/Base.Common/ProtocalUnit.cs
index 33b7506..f10c579 100644
--- a/Modbus.Net/src/Base.Common/ProtocalUnit.cs
+++ b/Modbus.Net/src/Base.Common/ProtocalUnit.cs
@@ -95,6 +95,10 @@ namespace Modbus.Net
///
public class ProtocalErrorException : Exception
{
+ ///
+ /// 构造函数
+ ///
+ ///
public ProtocalErrorException(string message)
: base(message)
{
diff --git a/Modbus.Net/src/Base.Common/TaskManager.cs b/Modbus.Net/src/Base.Common/TaskManager.cs
index cd73294..f6058ec 100644
--- a/Modbus.Net/src/Base.Common/TaskManager.cs
+++ b/Modbus.Net/src/Base.Common/TaskManager.cs
@@ -725,7 +725,7 @@ namespace Modbus.Net
machine = _machines.FirstOrDefault(p => p.ConnectionToken == connectionToken);
}
if (machine == null) return false;
- return await machine.InvokeMachineMethod>("SetDatasAsync", SetDataType, values);
+ return await machine.InvokeMachineMethod>("SetDatasAsync", SetDataType, values);
}
///
@@ -799,7 +799,7 @@ namespace Modbus.Net
cts.CancelAfter(TimeSpan.FromSeconds(_getCycle));
//读取数据
var ans =
- await machine.InvokeMachineMethod>>("GetDatasAsync",
+ await machine.InvokeMachineMethod>>("GetDatasAsync",
GetDataType).WithCancellation(cts.Token);
if (!machine.IsConnected)
{
diff --git a/Modbus.Net/src/Base.Common/TcpConnector.cs b/Modbus.Net/src/Base.Common/TcpConnector.cs
index 62e14bf..2b354e3 100644
--- a/Modbus.Net/src/Base.Common/TcpConnector.cs
+++ b/Modbus.Net/src/Base.Common/TcpConnector.cs
@@ -10,12 +10,19 @@ namespace Modbus.Net
///
public class SocketMessageEventArgs : EventArgs
{
+ ///
+ /// 构造器
+ ///
+ /// 需要返回的信息
public SocketMessageEventArgs(byte[] message)
{
Message = message;
}
- public byte[] Message { get; set; }
+ ///
+ /// 返回的信息
+ ///
+ public byte[] Message { get; }
}
///
@@ -39,6 +46,12 @@ namespace Modbus.Net
private bool m_disposed;
+ ///
+ /// 构造器
+ ///
+ /// Ip地址
+ /// 端口
+ /// 超时时间
public TcpConnector(string ipaddress, int port, int timeoutTime)
{
_host = ipaddress;
@@ -46,8 +59,14 @@ namespace Modbus.Net
TimeoutTime = timeoutTime;
}
+ ///
+ /// 连接关键字
+ ///
public override string ConnectionToken => _host;
+ ///
+ /// 超时时间
+ ///
public int TimeoutTime
{
get { return _timeoutTime; }
@@ -61,6 +80,9 @@ namespace Modbus.Net
}
}
+ ///
+ /// 是否已经连接
+ ///
public override bool IsConnected => _socketClient?.Client != null && _socketClient.Connected;
///
@@ -106,11 +128,19 @@ namespace Modbus.Net
Dispose(false);
}
+ ///
+ /// 连接
+ ///
+ /// 是否连接成功
public override bool Connect()
{
return AsyncHelper.RunSync(ConnectAsync);
}
+ ///
+ /// 连接
+ ///
+ /// 是否连接成功
public override async Task ConnectAsync()
{
if (_socketClient != null)
@@ -150,6 +180,10 @@ namespace Modbus.Net
}
}
+ ///
+ /// 断开
+ ///
+ /// 是否断开成功
public override bool Disconnect()
{
if (_socketClient == null)
@@ -183,6 +217,11 @@ namespace Modbus.Net
Console.WriteLine(message);
}
+ ///
+ /// 发送数据,不需要返回任何值
+ ///
+ /// 发送的信息
+ /// 是否发送成功
public override bool SendMsgWithoutReturn(byte[] message)
{
return AsyncHelper.RunSync(() => SendMsgWithoutReturnAsync(message));
@@ -219,6 +258,11 @@ namespace Modbus.Net
}
}
+ ///
+ /// 发送数据,需要返回
+ ///
+ /// 发送的数据
+ /// 是否发送成功
public override byte[] SendMsg(byte[] message)
{
return AsyncHelper.RunSync(() => SendMsgAsync(message));
@@ -256,6 +300,11 @@ namespace Modbus.Net
}
}
+ ///
+ /// 接收返回消息
+ ///
+ /// Network Stream
+ /// 返回的消息
protected async Task ReceiveAsync(NetworkStream stream)
{
try
diff --git a/Modbus.Net/src/Base.Common/TcpProtocalLinker.cs b/Modbus.Net/src/Base.Common/TcpProtocalLinker.cs
index a117c00..cb1077a 100644
--- a/Modbus.Net/src/Base.Common/TcpProtocalLinker.cs
+++ b/Modbus.Net/src/Base.Common/TcpProtocalLinker.cs
@@ -5,10 +5,18 @@
///
public abstract class TcpProtocalLinker : ProtocalLinker
{
+ ///
+ /// 构造器
+ ///
protected TcpProtocalLinker() : this(ConfigurationManager.IP, int.Parse(ConfigurationManager.ModbusPort))
{
}
+ ///
+ /// 构造器
+ ///
+ /// Ip地址
+ /// 端口
protected TcpProtocalLinker(string ip, int port)
{
//初始化连接对象
diff --git a/Modbus.Net/src/Base.Common/ValueHelper.cs b/Modbus.Net/src/Base.Common/ValueHelper.cs
index e534bfb..c5c953e 100644
--- a/Modbus.Net/src/Base.Common/ValueHelper.cs
+++ b/Modbus.Net/src/Base.Common/ValueHelper.cs
@@ -7,10 +7,13 @@ using System.Text;
namespace Modbus.Net
{
///
- /// 值与字节数组之间转换的辅助类,这是一个Singleton类
+ /// 值与字节数组之间转换的辅助类(小端格式),这是一个Singleton类
///
public class ValueHelper
{
+ ///
+ /// 兼容数据类型对应的字节长度
+ ///
public Dictionary ByteLength = new Dictionary
{
{"System.Boolean", 0.125},
@@ -25,6 +28,9 @@ namespace Modbus.Net
{"System.Double", 8}
};
+ ///
+ /// 构造器
+ ///
protected ValueHelper()
{
}
@@ -34,6 +40,9 @@ namespace Modbus.Net
///
public static bool LittleEndian => true;
+ ///
+ /// 协议中的比特位内容构造是否小端的,默认是小端构造协议。
+ ///
public static bool LittleEndianBit => true;
///
@@ -49,8 +58,8 @@ namespace Modbus.Net
///
/// 将short数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(short value)
{
return BitConverter.GetBytes(value);
@@ -59,8 +68,8 @@ namespace Modbus.Net
///
/// 将int数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(int value)
{
return BitConverter.GetBytes(value);
@@ -69,8 +78,8 @@ namespace Modbus.Net
///
/// 将long数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(long value)
{
return BitConverter.GetBytes(value);
@@ -79,8 +88,8 @@ namespace Modbus.Net
///
/// 将ushort数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(ushort value)
{
return BitConverter.GetBytes(value);
@@ -89,8 +98,8 @@ namespace Modbus.Net
///
/// 将uint数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(uint value)
{
return BitConverter.GetBytes(value);
@@ -99,8 +108,8 @@ namespace Modbus.Net
///
/// 将ulong数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(ulong value)
{
return BitConverter.GetBytes(value);
@@ -109,8 +118,8 @@ namespace Modbus.Net
///
/// 将float数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(float value)
{
return BitConverter.GetBytes(value);
@@ -119,8 +128,8 @@ namespace Modbus.Net
///
/// 将double数字转换为byte数组
///
- ///
- ///
+ /// 待转换的值
+ /// 转换后的byte数组
public virtual byte[] GetBytes(double value)
{
return BitConverter.GetBytes(value);
@@ -129,9 +138,9 @@ namespace Modbus.Net
///
/// 将object数字转换为byte数组
///
- ///
- ///
- ///
+ /// 待转换的值
+ /// 待转换的值的类型
+ /// 转换后的byte数组
public virtual byte[] GetBytes(object value, Type type)
{
switch (type.FullName)
@@ -191,11 +200,11 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为对应类型的数字
///
- ///
- ///
- ///
- ///
- ///
+ /// 待转换的字节数组
+ /// 转换数字的位置
+ /// 转换数字的比特位置(仅Type为bool的时候有效)
+ /// 转换的类型
+ /// 转换出的数字
public virtual object GetValue(byte[] data, ref int pos, ref int subPos, Type t)
{
switch (t.FullName)
@@ -260,9 +269,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为byte数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual byte GetByte(byte[] data, ref int pos)
{
var t = data[pos];
@@ -273,9 +282,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为short数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual short GetShort(byte[] data, ref int pos)
{
var t = BitConverter.ToInt16(data, pos);
@@ -286,9 +295,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为int数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual int GetInt(byte[] data, ref int pos)
{
var t = BitConverter.ToInt32(data, pos);
@@ -299,9 +308,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为long数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual long GetLong(byte[] data, ref int pos)
{
var t = BitConverter.ToInt64(data, pos);
@@ -312,9 +321,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为ushort数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual ushort GetUShort(byte[] data, ref int pos)
{
var t = BitConverter.ToUInt16(data, pos);
@@ -325,9 +334,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为uint数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual uint GetUInt(byte[] data, ref int pos)
{
var t = BitConverter.ToUInt32(data, pos);
@@ -338,9 +347,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为ulong数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual ulong GetULong(byte[] data, ref int pos)
{
var t = BitConverter.ToUInt64(data, pos);
@@ -351,9 +360,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为float数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual float GetFloat(byte[] data, ref int pos)
{
var t = BitConverter.ToSingle(data, pos);
@@ -364,9 +373,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为double数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public virtual double GetDouble(byte[] data, ref int pos)
{
var t = BitConverter.ToDouble(data, pos);
@@ -377,11 +386,11 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为字符串
///
- ///
- ///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换的个数
+ /// 转换数字的位置
+ /// 编码方法
+ /// 转换出的字符串
public virtual string GetString(byte[] data, int count, ref int pos, Encoding encoding)
{
var t = encoding.GetString(data, pos, count);
@@ -392,9 +401,9 @@ namespace Modbus.Net
///
/// 将byte数组中相应的位置转换为8个bit数字
///
- ///
- ///
- ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的位数组
public virtual bool[] GetBits(byte[] data, ref int pos)
{
var t = new bool[8];
@@ -438,10 +447,10 @@ namespace Modbus.Net
///
/// 获取一个字节数组中某个Bit位的数据
///
- ///
- ///
- ///
- ///
+ /// byte数字
+ /// bit数组中的对应位置
+ /// 小数位
+ /// 对应位置的bit元素
public virtual bool GetBit(byte[] number, ref int pos, ref int subPos)
{
return GetBit(number[pos], ref pos, ref subPos);
@@ -450,8 +459,8 @@ namespace Modbus.Net
///
/// 反转一个字节的8个Bit位
///
- ///
- ///
+ /// 原始bit数组
+ /// 反转的bit数组
public virtual byte ReverseByte(byte originalByte)
{
byte result = 0;
@@ -467,8 +476,8 @@ namespace Modbus.Net
///
/// 将待转换的对象数组转换为需要发送的byte数组
///
- ///
- ///
+ /// object数组
+ /// byte数组
public virtual byte[] ObjectArrayToByteArray(object[] contents)
{
var b = false;
@@ -606,10 +615,10 @@ namespace Modbus.Net
///
/// 将一个byte数组转换成用户指定类型的数组,使用模板参数确定需要转换的类型
///
- ///
- ///
- ///
- ///
+ /// 目标数组类型
+ /// 待转换的数组
+ /// 转换的个数
+ /// 以T为类型的数组
public virtual T[] ByteArrayToDestinationArray(byte[] contents, int getCount)
{
var objectArray = _Instance.ByteArrayToObjectArray(contents,
@@ -733,6 +742,14 @@ namespace Modbus.Net
return array;
}
+ ///
+ /// 在一个数组中写一个值
+ ///
+ /// 待写入的字节数组
+ /// 设置的位置
+ /// 设置的比特位位置(仅setValue为bit的时候有效)
+ /// 写入的值
+ /// 写入是否成功
public bool SetValue(byte[] contents, int setPos, int subPos, object setValue)
{
var type = setValue.GetType();
@@ -802,7 +819,7 @@ namespace Modbus.Net
/// 待设置的byte数组
/// 设置的位置
/// 要设置的值
- ///
+ /// 设置是否成功
public virtual bool SetValue(byte[] contents, int pos, object setValue)
{
try
@@ -823,7 +840,7 @@ namespace Modbus.Net
/// byte数子
/// 设置位置
/// 设置bit大小,true为1,false为0
- ///
+ /// 设置是否成功
protected byte SetBit(byte number, int subPos, bool setBit)
{
var creation = 0;
@@ -851,7 +868,7 @@ namespace Modbus.Net
/// 位置
/// bit位置
/// bit数
- ///
+ /// 设置是否成功
public virtual bool SetBit(byte[] contents, int pos, int subPos, bool setValue)
{
try
@@ -869,6 +886,9 @@ namespace Modbus.Net
private static ValueHelper _instance;
+ ///
+ /// 实例,继承时请把它覆写掉
+ ///
protected virtual ValueHelper _Instance => _instance;
///
@@ -876,6 +896,11 @@ namespace Modbus.Net
///
public static ValueHelper Instance => _instance ?? (_instance = new ValueHelper());
+ ///
+ /// 根据端格式获取ValueHelper实例
+ ///
+ /// 端格式
+ /// 对应的ValueHelper实例
public static ValueHelper GetInstance(Endian endian)
{
switch (endian)
@@ -902,61 +927,123 @@ namespace Modbus.Net
#endregion
}
+ ///
+ /// 值与字节数组之间转换的辅助类(大端格式),这是一个Singleton类
+ ///
public class BigEndianValueHelper : ValueHelper
{
private static BigEndianValueHelper _bigEndianInstance;
+ ///
+ /// 构造器
+ ///
protected BigEndianValueHelper()
{
}
+ ///
+ /// 覆写的实例获取
+ ///
protected override ValueHelper _Instance => _bigEndianInstance;
+ ///
+ /// 是否为大端
+ ///
protected new bool LittleEndian => false;
+ ///
+ /// 覆盖的获取实例的方法
+ ///
public new static BigEndianValueHelper Instance
=> _bigEndianInstance ?? (_bigEndianInstance = new BigEndianValueHelper());
+ ///
+ /// 将short数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
public override byte[] GetBytes(short value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将int数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
public override byte[] GetBytes(int value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将long数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
+ /// 转换出的数字
public override byte[] GetBytes(long value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将ushort数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
public override byte[] GetBytes(ushort value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将uint数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
public override byte[] GetBytes(uint value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将ulong数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
public override byte[] GetBytes(ulong value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将float数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
public override byte[] GetBytes(float value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将double数字转换为byte数组
+ ///
+ /// 待转换的值
+ /// 转换后的byte数组
public override byte[] GetBytes(double value)
{
return Reverse(BitConverter.GetBytes(value));
}
+ ///
+ /// 将byte数组中相应的位置转换为short数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override short GetShort(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 2);
@@ -966,6 +1053,12 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 将byte数组中相应的位置转换为int数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override int GetInt(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 4);
@@ -975,6 +1068,12 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 将byte数组中相应的位置转换为long数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override long GetLong(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 8);
@@ -984,6 +1083,12 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 将byte数组中相应的位置转换为ushort数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override ushort GetUShort(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 2);
@@ -993,6 +1098,12 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 将byte数组中相应的位置转换为uint数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override uint GetUInt(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 4);
@@ -1002,6 +1113,12 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 将byte数组中相应的位置转换为ulong数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override ulong GetULong(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 8);
@@ -1011,6 +1128,12 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 将byte数组中相应的位置转换为float数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override float GetFloat(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 4);
@@ -1020,6 +1143,12 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 将byte数组中相应的位置转换为double数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的数字
public override double GetDouble(byte[] data, ref int pos)
{
Array.Reverse(data, pos, 8);
@@ -1029,6 +1158,11 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 反转一个byte数组
+ ///
+ /// 待反转的数组
+ /// 反转后的数组
private byte[] Reverse(byte[] data)
{
Array.Reverse(data);
@@ -1036,23 +1170,48 @@ namespace Modbus.Net
}
}
+ ///
+ /// 值与字节数组之间转换的辅助类(大端-大端位格式),这是一个Singleton类
+ ///
public class BigEndianMsbValueHelper : BigEndianValueHelper
{
private static BigEndianValueHelper _bigEndianInstance;
+ ///
+ /// 构造函数
+ ///
protected BigEndianMsbValueHelper()
{
}
+ ///
+ /// 覆写的实例获取方法
+ ///
protected override ValueHelper _Instance => _bigEndianInstance;
+ ///
+ /// 是否为小端
+ ///
protected new bool LittleEndian => false;
+ ///
+ /// 是否为小端位
+ ///
protected new bool LittleEndianBit => false;
+ ///
+ /// 覆盖的实例获取方法
+ ///
public new static BigEndianValueHelper Instance
=> _bigEndianInstance ?? (_bigEndianInstance = new BigEndianMsbValueHelper());
+ ///
+ /// 获取一个byte中相对应的bit数组展开中第n个位置中的bit元素。
+ ///
+ /// byte数字
+ /// bit数组中的对应位置
+ /// 小数位
+ /// 对应位置的bit元素
public override bool GetBit(byte[] number, ref int pos, ref int subPos)
{
if (subPos < 0 && subPos > 7) throw new IndexOutOfRangeException();
@@ -1068,6 +1227,12 @@ namespace Modbus.Net
return bit;
}
+ ///
+ /// 将byte数组中相应的位置转换为8个bit数字
+ ///
+ /// 待转换的数组
+ /// 转换数字的位置
+ /// 转换出的位数组
public override bool[] GetBits(byte[] data, ref int pos)
{
var t = base.GetBits(data, ref pos);
@@ -1075,6 +1240,13 @@ namespace Modbus.Net
return t;
}
+ ///
+ /// 设置对应数字中相应位置的bit的值
+ ///
+ /// byte数子
+ /// 设置位置
+ /// 设置bit大小,true为1,false为0
+ /// 设置是否成功
public override bool SetBit(byte[] number, int pos, int subPos, bool setBit)
{
return base.SetBit(number, pos, 7 - subPos, setBit);
diff --git a/Tests/Modbus.Net.Tests/MachineMethodTest.cs b/Tests/Modbus.Net.Tests/MachineMethodTest.cs
index e6d980a..29dcea8 100644
--- a/Tests/Modbus.Net.Tests/MachineMethodTest.cs
+++ b/Tests/Modbus.Net.Tests/MachineMethodTest.cs
@@ -15,7 +15,7 @@ namespace Modbus.Net.Tests
public void GetUtility()
{
BaseMachine baseMachine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", null, true, 2, 0);
- var utility = baseMachine.GetUtility();
+ var utility = baseMachine.GetUtility();
var methods = utility.GetType().GetRuntimeMethods();
Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "GetTimeAsync") != null, true);
Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "SetTimeAsync") != null, true);
@@ -25,9 +25,9 @@ namespace Modbus.Net.Tests
public async Task InvokeUtility()
{
BaseMachine baseMachine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", null, true, 2, 0);
- var success = await baseMachine.BaseUtility.InvokeUtilityMethod>("SetTimeAsync", DateTime.Now);
+ var success = await baseMachine.BaseUtility.InvokeUtilityMethod>("SetTimeAsync", DateTime.Now);
Assert.AreEqual(success, true);
- var time = await baseMachine.BaseUtility.InvokeUtilityMethod>("GetTimeAsync");
+ var time = await baseMachine.BaseUtility.InvokeUtilityMethod>("GetTimeAsync");
Assert.AreEqual((time.ToUniversalTime() - DateTime.Now.ToUniversalTime()).Seconds < 10, true);
}
@@ -46,7 +46,7 @@ namespace Modbus.Net.Tests
DataType = typeof(bool)
}
}, true, 2, 0);
- var success = await baseMachine.InvokeMachineMethod>("SetDatasAsync",
+ var success = await baseMachine.InvokeMachineMethod>("SetDatasAsync",
MachineSetDataType.Address,
new Dictionary
{
@@ -55,9 +55,9 @@ namespace Modbus.Net.Tests
}
});
Assert.AreEqual(success, true);
- var datas = await baseMachine.InvokeMachineMethod>>("GetDatasAsync", MachineGetDataType.Address);
+ var datas = await baseMachine.InvokeMachineMethod>>("GetDatasAsync", MachineGetDataType.Address);
Assert.AreEqual(datas["0X 1.0"].PlcValue, 1);
- success = await baseMachine.InvokeMachineMethod>("SetDatasAsync",
+ success = await baseMachine.InvokeMachineMethod>("SetDatasAsync",
MachineSetDataType.Address,
new Dictionary
{