diff --git a/Modbus.Net/Modbus.Net.Modbus.NA200H/AddressTranslatorNA200H.cs b/Modbus.Net/Modbus.Net.Modbus.NA200H/AddressTranslatorNA200H.cs
index de520d5..cc11251 100644
--- a/Modbus.Net/Modbus.Net.Modbus.NA200H/AddressTranslatorNA200H.cs
+++ b/Modbus.Net/Modbus.Net.Modbus.NA200H/AddressTranslatorNA200H.cs
@@ -46,7 +46,7 @@ namespace Modbus.Net.Modbus.NA200H
"Q",
new AreaOutputDef
{
- Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus,
+ Code = (int) ModbusProtocolFunctionCode.ReadCoilStatus,
AreaWidth = 0.125
}
},
@@ -54,7 +54,7 @@ namespace Modbus.Net.Modbus.NA200H
"M",
new AreaOutputDef
{
- Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus,
+ Code = (int) ModbusProtocolFunctionCode.ReadCoilStatus,
AreaWidth = 0.125
}
},
@@ -62,7 +62,7 @@ namespace Modbus.Net.Modbus.NA200H
"N",
new AreaOutputDef
{
- Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus,
+ Code = (int) ModbusProtocolFunctionCode.ReadCoilStatus,
AreaWidth = 0.125
}
},
@@ -70,7 +70,7 @@ namespace Modbus.Net.Modbus.NA200H
"I",
new AreaOutputDef
{
- Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputStatus,
+ Code = (int) ModbusProtocolFunctionCode.ReadInputStatus,
AreaWidth = 0.125
}
},
@@ -78,29 +78,29 @@ namespace Modbus.Net.Modbus.NA200H
"S",
new AreaOutputDef
{
- Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputStatus,
+ Code = (int) ModbusProtocolFunctionCode.ReadInputStatus,
AreaWidth = 0.125
}
},
{
"IW",
- new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}
+ new AreaOutputDef {Code = (int) ModbusProtocolFunctionCode.ReadInputRegister, AreaWidth = 2}
},
{
"SW",
- new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}
+ new AreaOutputDef {Code = (int) ModbusProtocolFunctionCode.ReadInputRegister, AreaWidth = 2}
},
{
"MW",
- new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
+ new AreaOutputDef {Code = (int) ModbusProtocolFunctionCode.ReadHoldRegister, AreaWidth = 2}
},
{
"NW",
- new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
+ new AreaOutputDef {Code = (int) ModbusProtocolFunctionCode.ReadHoldRegister, AreaWidth = 2}
},
{
"QW",
- new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
+ new AreaOutputDef {Code = (int) ModbusProtocolFunctionCode.ReadHoldRegister, AreaWidth = 2}
}
};
WriteFunctionCodeDictionary = new Dictionary<(string, bool), AreaOutputDef>
@@ -109,7 +109,7 @@ namespace Modbus.Net.Modbus.NA200H
("Q", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiCoil,
AreaWidth = 0.125
}
},
@@ -117,7 +117,7 @@ namespace Modbus.Net.Modbus.NA200H
("M", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiCoil,
AreaWidth = 0.125
}
},
@@ -125,7 +125,7 @@ namespace Modbus.Net.Modbus.NA200H
("N", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiCoil,
AreaWidth = 0.125
}
},
@@ -133,7 +133,7 @@ namespace Modbus.Net.Modbus.NA200H
("MW", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiRegister,
AreaWidth = 2
}
},
@@ -141,7 +141,7 @@ namespace Modbus.Net.Modbus.NA200H
("NW", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiRegister,
AreaWidth = 2
}
},
@@ -149,7 +149,7 @@ namespace Modbus.Net.Modbus.NA200H
("QW", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiRegister,
AreaWidth = 2
}
},
@@ -157,7 +157,7 @@ namespace Modbus.Net.Modbus.NA200H
("Q", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleCoil,
AreaWidth = 0.125
}
},
@@ -165,7 +165,7 @@ namespace Modbus.Net.Modbus.NA200H
("M", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleCoil,
AreaWidth = 0.125
}
},
@@ -173,7 +173,7 @@ namespace Modbus.Net.Modbus.NA200H
("N", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleCoil,
AreaWidth = 0.125
}
},
@@ -181,7 +181,7 @@ namespace Modbus.Net.Modbus.NA200H
("MW", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleRegister,
AreaWidth = 2
}
},
@@ -189,7 +189,7 @@ namespace Modbus.Net.Modbus.NA200H
("NW", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleRegister,
AreaWidth = 2
}
},
@@ -197,7 +197,7 @@ namespace Modbus.Net.Modbus.NA200H
("QW", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleRegister,
AreaWidth = 2
}
}
diff --git a/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/IUtilityMethodTime.cs b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/IUtilityMethodTime.cs
new file mode 100644
index 0000000..0e57309
--- /dev/null
+++ b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/IUtilityMethodTime.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Modbus.Net.Modbus.SelfDefinedSample
+{
+ ///
+ /// Utility的时间读写接口
+ ///
+ public interface IUtilityMethodTime : IUtilityMethod
+ {
+ ///
+ /// 获取PLC时间
+ ///
+ /// PLC时间
+ Task> GetTimeAsync();
+
+ ///
+ /// 设置PLC时间
+ ///
+ /// 设置PLC时间
+ /// 设置是否成功
+ Task> SetTimeAsync(DateTime setTime);
+ }
+}
diff --git a/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/Modbus.Net.Modbus.SelfDefinedSample.csproj b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/Modbus.Net.Modbus.SelfDefinedSample.csproj
new file mode 100644
index 0000000..15a05bb
--- /dev/null
+++ b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/Modbus.Net.Modbus.SelfDefinedSample.csproj
@@ -0,0 +1,34 @@
+
+
+
+ net6.0
+ Modbus.Net.Modbus.SelfDefinedSample
+ Modbus.Net.Modbus.SelfDefinedSample
+ Modbus.Net.Modbus.SelfDefinedSample
+ 1.4.1-beta05
+ Chris L.(Luo Sheng)
+ Hangzhou Delian Science Technology Co.,Ltd.
+ Modbus.Net.Modbus
+ Modbus.Net Modbus Implementation
+ Copyright 2023 Hangzhou Delian Science Technology Co.,Ltd.
+ https://github.com/parallelbgls/Modbus.Net/tree/master/Modbus.Net/Modbus.Net.Modbus.NA200H
+ https://github.com/parallelbgls/Modbus.Net
+ git
+ hardware communicate protocol modbus Delian
+ False
+ True
+ True
+ True
+ MIT
+ README.md
+ snupkg
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusProtocolTime.cs b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusProtocolTime.cs
new file mode 100644
index 0000000..1f63549
--- /dev/null
+++ b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusProtocolTime.cs
@@ -0,0 +1,319 @@
+using System;
+
+namespace Modbus.Net.Modbus.SelfDefinedSample
+{
+ ///
+ /// 跟时间有关的功能码
+ ///
+ public enum ModbusProtocolTimeFunctionCode : byte
+ {
+ ///
+ /// 读时间
+ ///
+ GetSystemTime = 3,
+
+ ///
+ /// 写时间
+ ///
+ SetSystemTime = 16
+ }
+
+ #region 读PLC时间
+
+ ///
+ /// 读时间输入
+ ///
+ public class GetSystemTimeModbusInputStruct : IInputStruct
+ {
+ ///
+ /// 构造函数
+ ///
+ /// 从站号
+ public GetSystemTimeModbusInputStruct(byte slaveAddress)
+ {
+ SlaveAddress = slaveAddress;
+ FunctionCode = (byte)ModbusProtocolTimeFunctionCode.GetSystemTime;
+ StartAddress = 30000;
+ GetCount = 5;
+ }
+
+ ///
+ /// 从站号
+ ///
+ public byte SlaveAddress { get; }
+
+ ///
+ /// 功能码
+ ///
+ public byte FunctionCode { get; }
+
+ ///
+ /// 开始地址
+ ///
+ public ushort StartAddress { get; }
+
+ ///
+ /// 获取个数
+ ///
+ public ushort GetCount { get; }
+ }
+
+ ///
+ /// 读时间输出
+ ///
+ public class GetSystemTimeModbusOutputStruct : IOutputStruct
+ {
+ ///
+ /// 构造函数
+ ///
+ /// 从站号
+ /// 功能码
+ /// 写入个数
+ /// 年
+ /// 日
+ /// 月
+ /// 时
+ /// 秒
+ /// 分
+ /// 毫秒
+ public GetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode,
+ byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute,
+ ushort millisecond)
+ {
+ SlaveAddress = slaveAddress;
+ FunctionCode = functionCode;
+ WriteByteCount = writeByteCount;
+ Time = new DateTime(year, month, day, hour, minute, second, millisecond);
+ }
+
+ ///
+ /// 从站号
+ ///
+ public byte SlaveAddress { get; private set; }
+
+ ///
+ /// 功能码
+ ///
+ public byte FunctionCode { get; private set; }
+
+ ///
+ /// 写入个数
+ ///
+ public byte WriteByteCount { get; private set; }
+
+ ///
+ /// 时间
+ ///
+ public DateTime Time { get; private set; }
+ }
+
+ ///
+ /// 读系统时间协议
+ ///
+ public class GetSystemTimeModbusProtocol : ProtocolUnit
+ {
+ ///
+ /// 格式化
+ ///
+ /// 写系统时间参数
+ /// 写系统时间的核心
+ public override byte[] Format(IInputStruct message)
+ {
+ var r_message = (GetSystemTimeModbusInputStruct)message;
+ return Format(r_message.SlaveAddress, r_message.FunctionCode,
+ r_message.StartAddress, r_message.GetCount);
+ }
+
+ ///
+ /// 反格式化
+ ///
+ /// 获取的信息
+ /// 当前反格式化的位置
+ /// 反格式化的信息
+ public override IOutputStruct Unformat(byte[] messageBytes, ref int flag)
+ {
+ var slaveAddress = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var functionCode = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var writeByteCount = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var year = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
+ var day = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var month = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var hour = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
+ var second = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var minute = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var millisecond = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
+ return new GetSystemTimeModbusOutputStruct(slaveAddress, functionCode, writeByteCount, year, day,
+ month, hour, second, minute, millisecond);
+ }
+ }
+
+ #endregion
+
+ #region 写PLC时间
+
+ ///
+ /// 写时间输入
+ ///
+ public class SetSystemTimeModbusInputStruct : IInputStruct
+ {
+ ///
+ /// 构造函数
+ ///
+ /// 从站号
+ /// 时间
+ public SetSystemTimeModbusInputStruct(byte slaveAddress, DateTime time)
+ {
+ SlaveAddress = slaveAddress;
+ FunctionCode = (byte)ModbusProtocolTimeFunctionCode.SetSystemTime;
+ StartAddress = 30000;
+ WriteCount = 5;
+ WriteByteCount = 10;
+ Year = (ushort)time.Year;
+ Day = (byte)time.Day;
+ Month = (byte)time.Month;
+ Hour = (ushort)time.Hour;
+ Second = (byte)time.Second;
+ Minute = (byte)time.Minute;
+ Millisecond = (ushort)time.Millisecond;
+ }
+
+ ///
+ /// 从站号
+ ///
+ public byte SlaveAddress { get; }
+
+ ///
+ /// 功能码
+ ///
+ public byte FunctionCode { get; }
+
+ ///
+ /// 开始地址
+ ///
+ public ushort StartAddress { get; }
+
+ ///
+ /// 写入个数
+ ///
+ public ushort WriteCount { get; }
+
+ ///
+ /// 写入字节个数
+ ///
+ public byte WriteByteCount { get; }
+
+ ///
+ /// 年
+ ///
+ public ushort Year { get; }
+
+ ///
+ /// 日
+ ///
+ public byte Day { get; }
+
+ ///
+ /// 月
+ ///
+ public byte Month { get; }
+
+ ///
+ /// 时
+ ///
+ public ushort Hour { get; }
+
+ ///
+ /// 秒
+ ///
+ public byte Second { get; }
+
+ ///
+ /// 分
+ ///
+ public byte Minute { get; }
+
+ ///
+ /// 毫秒
+ ///
+ public ushort Millisecond { get; }
+ }
+
+ ///
+ /// 写时间输出
+ ///
+ public class SetSystemTimeModbusOutputStruct : IOutputStruct
+ {
+ ///
+ /// 构造函数
+ ///
+ /// 从站号
+ /// 功能码
+ /// 开始地址
+ /// 写入个数
+ public SetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode,
+ ushort startAddress, ushort writeCount)
+ {
+ SlaveAddress = slaveAddress;
+ FunctionCode = functionCode;
+ StartAddress = startAddress;
+ WriteCount = writeCount;
+ }
+
+ ///
+ /// 从站号
+ ///
+ public byte SlaveAddress { get; private set; }
+
+ ///
+ /// 功能码
+ ///
+ public byte FunctionCode { get; private set; }
+
+ ///
+ /// 开始地址
+ ///
+ public ushort StartAddress { get; private set; }
+
+ ///
+ /// 写入个数
+ ///
+ public ushort WriteCount { get; private set; }
+ }
+
+ ///
+ /// 写系统时间协议
+ ///
+ public class SetSystemTimeModbusProtocol : ProtocolUnit
+ {
+ ///
+ /// 格式化
+ ///
+ /// 写系统时间的参数
+ /// 写系统时间的核心
+ public override byte[] Format(IInputStruct message)
+ {
+ var r_message = (SetSystemTimeModbusInputStruct)message;
+ return Format(r_message.SlaveAddress, r_message.FunctionCode,
+ r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.Year,
+ r_message.Day,
+ r_message.Month, r_message.Hour, r_message.Second, r_message.Minute, r_message.Millisecond);
+ }
+
+ ///
+ /// 反格式化
+ ///
+ /// 获取的信息
+ /// 当前反格式化的位置
+ /// 反格式化的信息
+ public override IOutputStruct Unformat(byte[] messageBytes, ref int flag)
+ {
+ var slaveAddress = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var functionCode = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
+ var startAddress = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
+ var writeCount = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
+ return new SetSystemTimeModbusOutputStruct(slaveAddress, functionCode, startAddress, writeCount);
+ }
+ }
+
+ #endregion
+}
\ No newline at end of file
diff --git a/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusUtilityTime.cs b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusUtilityTime.cs
new file mode 100644
index 0000000..8aa0b1c
--- /dev/null
+++ b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusUtilityTime.cs
@@ -0,0 +1,108 @@
+using Microsoft.Extensions.Logging;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Modbus.Net.Modbus.SelfDefinedSample
+{
+ public class ModbusUtilityTime : ModbusUtility, IUtilityMethodTime
+ {
+ private static readonly ILogger logger = LogProvider.CreateLogger();
+
+ ///
+ /// 构造函数
+ ///
+ /// 协议类型
+ /// 从站号
+ /// 主站号
+ /// 端格式
+ public ModbusUtilityTime(int connectionType, byte slaveAddress, byte masterAddress,
+ Endian endian = Endian.BigEndianLsb)
+ : base(connectionType, slaveAddress, masterAddress, endian)
+ {
+ }
+
+ ///
+ /// 构造函数
+ ///
+ /// 协议类型
+ /// 连接地址
+ /// 从站号
+ /// 主站号
+ /// 端格式
+ public ModbusUtilityTime(ModbusType connectionType, string connectionString, byte slaveAddress, byte masterAddress,
+ Endian endian = Endian.BigEndianLsb)
+ : base(connectionType, connectionString, slaveAddress, masterAddress, endian)
+ {
+ }
+
+ ///
+ /// 读时间
+ ///
+ /// 设备的时间
+ public async Task> GetTimeAsync()
+ {
+ try
+ {
+ var inputStruct = new GetSystemTimeModbusInputStruct(SlaveAddress);
+ var outputStruct =
+ await Wrapper.SendReceiveAsync(
+ Wrapper[typeof(GetSystemTimeModbusProtocol)], inputStruct);
+ return new ReturnStruct
+ {
+ Datas = outputStruct?.Time ?? DateTime.MinValue,
+ IsSuccess = true,
+ ErrorCode = 0,
+ ErrorMsg = ""
+ };
+ }
+ catch (ModbusProtocolErrorException e)
+ {
+ logger.LogError(e, $"ModbusUtility -> GetTime: {ConnectionString} error: {e.Message}");
+ return new ReturnStruct
+ {
+ Datas = DateTime.MinValue,
+ IsSuccess = false,
+ ErrorCode = e.ErrorMessageNumber,
+ ErrorMsg = e.Message
+ };
+ }
+ }
+
+ ///
+ /// 写时间
+ ///
+ /// 需要写入的时间
+ /// 写入是否成功
+ public async Task> SetTimeAsync(DateTime setTime)
+ {
+ try
+ {
+ var inputStruct = new SetSystemTimeModbusInputStruct(SlaveAddress, setTime);
+ var outputStruct =
+ await Wrapper.SendReceiveAsync(
+ Wrapper[typeof(SetSystemTimeModbusProtocol)], inputStruct);
+ return new ReturnStruct()
+ {
+ Datas = outputStruct?.WriteCount > 0,
+ IsSuccess = outputStruct?.WriteCount > 0,
+ ErrorCode = outputStruct?.WriteCount > 0 ? 0 : -2,
+ ErrorMsg = outputStruct?.WriteCount > 0 ? "" : "Data length zero"
+ };
+ }
+ catch (ModbusProtocolErrorException e)
+ {
+ logger.LogError(e, $"ModbusUtility -> SetTime: {ConnectionString} error: {e.Message}");
+ return new ReturnStruct
+ {
+ Datas = false,
+ IsSuccess = false,
+ ErrorCode = e.ErrorMessageNumber,
+ ErrorMsg = e.Message
+ };
+ }
+ }
+ }
+}
diff --git a/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/README.md b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/README.md
new file mode 100644
index 0000000..6195267
--- /dev/null
+++ b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/README.md
@@ -0,0 +1,7 @@
+Modbus.Net.Modbus
+===================
+[](https://www.nuget.org/packages/Modbus.Net.Modbus.SelfDefinedSample/)
+
+Self defined function get time and set time implementation of Modbus.Net
+
+Doc has been moved to wiki.
\ No newline at end of file
diff --git a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs
index e925fe5..34edefc 100644
--- a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs
+++ b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs
@@ -54,7 +54,7 @@ namespace Modbus.Net.Modbus
"0X",
new AreaOutputDef
{
- Code = (int) ModbusProtocolReadDataFunctionCode.ReadCoilStatus,
+ Code = (int) ModbusProtocolFunctionCode.ReadCoilStatus,
AreaWidth = 0.125
}
},
@@ -62,17 +62,17 @@ namespace Modbus.Net.Modbus
"1X",
new AreaOutputDef
{
- Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputStatus,
+ Code = (int) ModbusProtocolFunctionCode.ReadInputStatus,
AreaWidth = 0.125
}
},
{
"3X",
- new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadInputRegister, AreaWidth = 2}
+ new AreaOutputDef {Code = (int) ModbusProtocolFunctionCode.ReadInputRegister, AreaWidth = 2}
},
{
"4X",
- new AreaOutputDef {Code = (int) ModbusProtocolReadDataFunctionCode.ReadHoldRegister, AreaWidth = 2}
+ new AreaOutputDef {Code = (int) ModbusProtocolFunctionCode.ReadHoldRegister, AreaWidth = 2}
}
};
WriteFunctionCodeDictionary = new Dictionary<(string, bool), AreaOutputDef>
@@ -81,7 +81,7 @@ namespace Modbus.Net.Modbus
("0X", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiCoil,
AreaWidth = 0.125
}
},
@@ -89,7 +89,7 @@ namespace Modbus.Net.Modbus
("4X", false),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteMultiRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteMultiRegister,
AreaWidth = 2
}
},
@@ -97,7 +97,7 @@ namespace Modbus.Net.Modbus
("0X", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleCoil,
AreaWidth = 0.125
}
},
@@ -105,7 +105,7 @@ namespace Modbus.Net.Modbus
("4X", true),
new AreaOutputDef
{
- Code = (int) ModbusProtocolWriteDataFunctionCode.WriteSingleRegister,
+ Code = (int) ModbusProtocolFunctionCode.WriteSingleRegister,
AreaWidth = 2
}
}
diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs
index d834ff9..e37d4e5 100644
--- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs
+++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs
@@ -4,42 +4,10 @@ using System.Threading.Tasks;
namespace Modbus.Net.Modbus
{
- ///
- /// 变量功能码
- ///
- internal enum ModbusProtocolVariableFunctionCode : byte
- {
- ///
- /// 读变量
- ///
- ReadVariable = 20,
-
- ///
- /// 写变量
- ///
- WriteVariable = 21
- }
-
- ///
- /// 跟时间有关的功能码
- ///
- public enum ModbusProtocolTimeFunctionCode : byte
- {
- ///
- /// 读时间
- ///
- GetSystemTime = 3,
-
- ///
- /// 写时间
- ///
- SetSystemTime = 16
- }
-
///
/// 跟读数据有关的功能码
///
- public enum ModbusProtocolReadDataFunctionCode : byte
+ public enum ModbusProtocolFunctionCode : byte
{
///
/// 读线圈
@@ -59,14 +27,8 @@ namespace Modbus.Net.Modbus
///
/// 读输入寄存器
///
- ReadInputRegister = 4
- }
-
- ///
- /// 跟写数据有关的功能码
- ///
- public enum ModbusProtocolWriteDataFunctionCode : byte
- {
+ ReadInputRegister = 4,
+
///
/// 写单个线圈
///
@@ -85,7 +47,80 @@ namespace Modbus.Net.Modbus
///
/// 写多个寄存器
///
- WriteMultiRegister = 16
+ WriteMultiRegister = 16,
+
+ ///
+ /// 读文件记录
+ ///
+ ReadFileRecord = 20,
+
+ ///
+ /// 写文件记录
+ ///
+ WriteFileRecord = 21,
+
+ ///
+ /// 写寄存器掩码
+ ///
+ MaskWriteRegister = 22,
+
+ ///
+ /// 读写多个寄存器
+ ///
+ ReadWriteMultipleRegister = 23,
+
+ ///
+ /// 读队列
+ ///
+ ReadFIFOQueue = 24,
+ }
+
+ ///
+ /// Modbus MEI方式
+ ///
+ public enum ModbusMEIProtocolFunctionCode : ushort
+ {
+ ///
+ ///
+ ///
+ CANopenGeneralReferenceRequestandResponsePDU = 0x2B0D,
+
+ ///
+ /// 读设备信息
+ ///
+ ReadDeviceIdentification = 0x2B0E
+ }
+
+ ///
+ /// 只能在串口通信中使用的Modbus方法
+ /// 不能在TCP和UDP通信中使用
+ ///
+ public enum ModbusSerialPortOnlyFunctionCode : byte
+ {
+ ///
+ /// 读错误状态
+ ///
+ ReadExceptionStatus = 7,
+
+ ///
+ /// 诊断
+ ///
+ Diagnostics = 8,
+
+ ///
+ /// 读通讯事件计数器
+ ///
+ GetCommEventCounter = 11,
+
+ ///
+ /// 读日志
+ ///
+ GetCommEventLog = 12,
+
+ ///
+ /// 读从站ID
+ ///
+ ReportSlaveID = 17
}
///
@@ -378,7 +413,7 @@ namespace Modbus.Net.Modbus
///
/// 写数据输入
///
- public class WriteSingleDataModbusInputStruct : IInputStruct
+ public class WriteSingleCoilModbusInputStruct : IInputStruct
{
///
/// 构造函数
@@ -388,7 +423,7 @@ namespace Modbus.Net.Modbus
/// 写入的数据
/// 地址翻译器
/// 端格式
- public WriteSingleDataModbusInputStruct(byte slaveAddress, string startAddress, object writeValue,
+ public WriteSingleCoilModbusInputStruct(byte slaveAddress, string startAddress, object writeValue,
ModbusTranslatorBase addressTranslator, Endian endian)
{
SlaveAddress = slaveAddress;
@@ -396,7 +431,7 @@ namespace Modbus.Net.Modbus
FunctionCode = (byte)translateAddress.Area;
StartAddress = (ushort)translateAddress.Address;
var writeByteValue =
- FunctionCode == (byte)ModbusProtocolWriteDataFunctionCode.WriteSingleCoil
+ FunctionCode == (byte)ModbusProtocolFunctionCode.WriteSingleCoil
? ((bool)writeValue
? new byte[] { 0xFF, 0x00 }
: new byte[] { 0x00, 0x00 })
@@ -429,7 +464,7 @@ namespace Modbus.Net.Modbus
///
/// 写数据输出
///
- public class WriteSingleDataModbusOutputStruct : IOutputStruct
+ public class WriteSingleCoilModbusOutputStruct : IOutputStruct
{
///
/// 构造函数
@@ -438,7 +473,7 @@ namespace Modbus.Net.Modbus
/// 功能码
/// 开始地址
/// 写入的数据
- public WriteSingleDataModbusOutputStruct(byte slaveAddress, byte functionCode,
+ public WriteSingleCoilModbusOutputStruct(byte slaveAddress, byte functionCode,
ushort startAddress, object writeValue)
{
SlaveAddress = slaveAddress;
@@ -471,7 +506,7 @@ namespace Modbus.Net.Modbus
///
/// 写多个寄存器协议
///
- public class WriteSingleDataModbusProtocol : ProtocolUnit
+ public class WriteSingleCoilModbusProtocol : ProtocolUnit
{
///
/// 格式化
@@ -480,7 +515,7 @@ namespace Modbus.Net.Modbus
/// 写寄存器协议核心
public override byte[] Format(IInputStruct message)
{
- var r_message = (WriteSingleDataModbusInputStruct)message;
+ var r_message = (WriteSingleCoilModbusInputStruct)message;
var dataValue = Format(r_message.WriteValue);
var formattingBytes = Format(r_message.SlaveAddress, r_message.FunctionCode,
r_message.StartAddress, dataValue);
@@ -499,314 +534,15 @@ namespace Modbus.Net.Modbus
var functionCode = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
var startAddress = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
var writeValue = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
- var returnValue = functionCode == (byte)ModbusProtocolWriteDataFunctionCode.WriteSingleCoil
+ var returnValue = functionCode == (byte)ModbusProtocolFunctionCode.WriteSingleCoil
? (object)(writeValue == 0xFF00) : writeValue;
- return new WriteSingleDataModbusOutputStruct(slaveAddress, functionCode, startAddress,
+ return new WriteSingleCoilModbusOutputStruct(slaveAddress, functionCode, startAddress,
returnValue);
}
}
#endregion
- #region 读PLC时间
-
- ///
- /// 读时间输入
- ///
- public class GetSystemTimeModbusInputStruct : IInputStruct
- {
- ///
- /// 构造函数
- ///
- /// 从站号
- public GetSystemTimeModbusInputStruct(byte slaveAddress)
- {
- SlaveAddress = slaveAddress;
- FunctionCode = (byte)ModbusProtocolTimeFunctionCode.GetSystemTime;
- StartAddress = 30000;
- GetCount = 5;
- }
-
- ///
- /// 从站号
- ///
- public byte SlaveAddress { get; }
-
- ///
- /// 功能码
- ///
- public byte FunctionCode { get; }
-
- ///
- /// 开始地址
- ///
- public ushort StartAddress { get; }
-
- ///
- /// 获取个数
- ///
- public ushort GetCount { get; }
- }
-
- ///
- /// 读时间输出
- ///
- public class GetSystemTimeModbusOutputStruct : IOutputStruct
- {
- ///
- /// 构造函数
- ///
- /// 从站号
- /// 功能码
- /// 写入个数
- /// 年
- /// 日
- /// 月
- /// 时
- /// 秒
- /// 分
- /// 毫秒
- public GetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode,
- byte writeByteCount, ushort year, byte day, byte month, ushort hour, byte second, byte minute,
- ushort millisecond)
- {
- SlaveAddress = slaveAddress;
- FunctionCode = functionCode;
- WriteByteCount = writeByteCount;
- Time = new DateTime(year, month, day, hour, minute, second, millisecond);
- }
-
- ///
- /// 从站号
- ///
- public byte SlaveAddress { get; private set; }
-
- ///
- /// 功能码
- ///
- public byte FunctionCode { get; private set; }
-
- ///
- /// 写入个数
- ///
- public byte WriteByteCount { get; private set; }
-
- ///
- /// 时间
- ///
- public DateTime Time { get; private set; }
- }
-
- ///
- /// 读系统时间协议
- ///
- public class GetSystemTimeModbusProtocol : ProtocolUnit
- {
- ///
- /// 格式化
- ///
- /// 写系统时间参数
- /// 写系统时间的核心
- public override byte[] Format(IInputStruct message)
- {
- var r_message = (GetSystemTimeModbusInputStruct)message;
- return Format(r_message.SlaveAddress, r_message.FunctionCode,
- r_message.StartAddress, r_message.GetCount);
- }
-
- ///
- /// 反格式化
- ///
- /// 获取的信息
- /// 当前反格式化的位置
- /// 反格式化的信息
- public override IOutputStruct Unformat(byte[] messageBytes, ref int flag)
- {
- var slaveAddress = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var functionCode = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var writeByteCount = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var year = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
- var day = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var month = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var hour = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
- var second = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var minute = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var millisecond = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
- return new GetSystemTimeModbusOutputStruct(slaveAddress, functionCode, writeByteCount, year, day,
- month, hour, second, minute, millisecond);
- }
- }
-
- #endregion
-
- #region 写PLC时间
-
- ///
- /// 写时间输入
- ///
- public class SetSystemTimeModbusInputStruct : IInputStruct
- {
- ///
- /// 构造函数
- ///
- /// 从站号
- /// 时间
- public SetSystemTimeModbusInputStruct(byte slaveAddress, DateTime time)
- {
- SlaveAddress = slaveAddress;
- FunctionCode = (byte)ModbusProtocolTimeFunctionCode.SetSystemTime;
- StartAddress = 30000;
- WriteCount = 5;
- WriteByteCount = 10;
- Year = (ushort)time.Year;
- Day = (byte)time.Day;
- Month = (byte)time.Month;
- Hour = (ushort)time.Hour;
- Second = (byte)time.Second;
- Minute = (byte)time.Minute;
- Millisecond = (ushort)time.Millisecond;
- }
-
- ///
- /// 从站号
- ///
- public byte SlaveAddress { get; }
-
- ///
- /// 功能码
- ///
- public byte FunctionCode { get; }
-
- ///
- /// 开始地址
- ///
- public ushort StartAddress { get; }
-
- ///
- /// 写入个数
- ///
- public ushort WriteCount { get; }
-
- ///
- /// 写入字节个数
- ///
- public byte WriteByteCount { get; }
-
- ///
- /// 年
- ///
- public ushort Year { get; }
-
- ///
- /// 日
- ///
- public byte Day { get; }
-
- ///
- /// 月
- ///
- public byte Month { get; }
-
- ///
- /// 时
- ///
- public ushort Hour { get; }
-
- ///
- /// 秒
- ///
- public byte Second { get; }
-
- ///
- /// 分
- ///
- public byte Minute { get; }
-
- ///
- /// 毫秒
- ///
- public ushort Millisecond { get; }
- }
-
- ///
- /// 写时间输出
- ///
- public class SetSystemTimeModbusOutputStruct : IOutputStruct
- {
- ///
- /// 构造函数
- ///
- /// 从站号
- /// 功能码
- /// 开始地址
- /// 写入个数
- public SetSystemTimeModbusOutputStruct(byte slaveAddress, byte functionCode,
- ushort startAddress, ushort writeCount)
- {
- SlaveAddress = slaveAddress;
- FunctionCode = functionCode;
- StartAddress = startAddress;
- WriteCount = writeCount;
- }
-
- ///
- /// 从站号
- ///
- public byte SlaveAddress { get; private set; }
-
- ///
- /// 功能码
- ///
- public byte FunctionCode { get; private set; }
-
- ///
- /// 开始地址
- ///
- public ushort StartAddress { get; private set; }
-
- ///
- /// 写入个数
- ///
- public ushort WriteCount { get; private set; }
- }
-
- ///
- /// 写系统时间协议
- ///
- public class SetSystemTimeModbusProtocol : ProtocolUnit
- {
- ///
- /// 格式化
- ///
- /// 写系统时间的参数
- /// 写系统时间的核心
- public override byte[] Format(IInputStruct message)
- {
- var r_message = (SetSystemTimeModbusInputStruct)message;
- return Format(r_message.SlaveAddress, r_message.FunctionCode,
- r_message.StartAddress, r_message.WriteCount, r_message.WriteByteCount, r_message.Year,
- r_message.Day,
- r_message.Month, r_message.Hour, r_message.Second, r_message.Minute, r_message.Millisecond);
- }
-
- ///
- /// 反格式化
- ///
- /// 获取的信息
- /// 当前反格式化的位置
- /// 反格式化的信息
- public override IOutputStruct Unformat(byte[] messageBytes, ref int flag)
- {
- var slaveAddress = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var functionCode = ValueHelper.GetInstance(Endian).GetByte(messageBytes, ref flag);
- var startAddress = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
- var writeCount = ValueHelper.GetInstance(Endian).GetUShort(messageBytes, ref flag);
- return new SetSystemTimeModbusOutputStruct(slaveAddress, functionCode, startAddress, writeCount);
- }
- }
-
- #endregion
-
///
/// Modbus协议错误表
///
@@ -820,6 +556,9 @@ namespace Modbus.Net.Modbus
{4, "SLAVE_DEVICE_FAILURE"},
{5, "ACKNOWLWDGE"},
{6, "SLAVE_DEVICE_BUSY"},
+ {8, "MEMORY_PARITY_ERROR" },
+ {10, "GATEWAY_PATH_UNAVAILABLE"},
+ {11, "GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND"},
{500, "TCP_ILLEGAL_LENGTH"},
{501, "RTU_ILLEGAL_CRC"}
};
diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs
index 3b8b0e6..cc06be2 100644
--- a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs
+++ b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs
@@ -53,7 +53,7 @@ namespace Modbus.Net.Modbus
///
/// 写单个数据方法接口
///
- public interface IUtilityMethodWriteSingle : IUtilityMethod
+ public interface IUtilityMethodWriteSingleCoil : IUtilityMethod
{
///
/// 写数据
@@ -61,13 +61,13 @@ namespace Modbus.Net.Modbus
/// 起始地址
/// 需要设置的数据
/// 设置是否成功
- Task> SetSingleDataAsync(string startAddress, object setContent);
+ Task> SetSingleCoilAsync(string startAddress, object setContent);
}
///
/// Modbus基础Api入口
///
- public class ModbusUtility : BaseUtility, IUtilityMethodTime, IUtilityMethodWriteSingle
+ public class ModbusUtility : BaseUtility, IUtilityMethodWriteSingleCoil
{
private static readonly ILogger logger = LogProvider.CreateLogger();
@@ -247,73 +247,6 @@ namespace Modbus.Net.Modbus
}
}
- ///
- /// 读时间
- ///
- /// 设备的时间
- public async Task> GetTimeAsync()
- {
- try
- {
- var inputStruct = new GetSystemTimeModbusInputStruct(SlaveAddress);
- var outputStruct =
- await Wrapper.SendReceiveAsync(
- Wrapper[typeof(GetSystemTimeModbusProtocol)], inputStruct);
- return new ReturnStruct
- {
- Datas = outputStruct?.Time ?? DateTime.MinValue,
- IsSuccess = true,
- ErrorCode = 0,
- ErrorMsg = ""
- };
- }
- catch (ModbusProtocolErrorException e)
- {
- logger.LogError(e, $"ModbusUtility -> GetTime: {ConnectionString} error: {e.Message}");
- return new ReturnStruct
- {
- Datas = DateTime.MinValue,
- IsSuccess = false,
- ErrorCode = e.ErrorMessageNumber,
- ErrorMsg = e.Message
- };
- }
- }
-
- ///
- /// 写时间
- ///
- /// 需要写入的时间
- /// 写入是否成功
- public async Task> SetTimeAsync(DateTime setTime)
- {
- try
- {
- var inputStruct = new SetSystemTimeModbusInputStruct(SlaveAddress, setTime);
- var outputStruct =
- await Wrapper.SendReceiveAsync(
- Wrapper[typeof(SetSystemTimeModbusProtocol)], inputStruct);
- return new ReturnStruct()
- {
- Datas = outputStruct?.WriteCount > 0,
- IsSuccess = outputStruct?.WriteCount > 0,
- ErrorCode = outputStruct?.WriteCount > 0 ? 0 : -2,
- ErrorMsg = outputStruct?.WriteCount > 0 ? "" : "Data length zero"
- };
- }
- catch (ModbusProtocolErrorException e)
- {
- logger.LogError(e, $"ModbusUtility -> SetTime: {ConnectionString} error: {e.Message}");
- return new ReturnStruct
- {
- Datas = false,
- IsSuccess = false,
- ErrorCode = e.ErrorMessageNumber,
- ErrorMsg = e.Message
- };
- }
- }
-
///
/// 设置协议类型
///
@@ -401,14 +334,14 @@ namespace Modbus.Net.Modbus
/// 起始地址
/// 需要设置的数据
/// 设置是否成功
- public async Task> SetSingleDataAsync(string startAddress, object setContent)
+ public async Task> SetSingleCoilAsync(string startAddress, object setContent)
{
try
{
- var inputStruct = new WriteSingleDataModbusInputStruct(SlaveAddress, startAddress, setContent,
+ var inputStruct = new WriteSingleCoilModbusInputStruct(SlaveAddress, startAddress, setContent,
(ModbusTranslatorBase)AddressTranslator, Endian);
var outputStruct = await
- Wrapper.SendReceiveAsync(Wrapper[typeof(WriteSingleDataModbusProtocol)],
+ Wrapper.SendReceiveAsync(Wrapper[typeof(WriteSingleCoilModbusProtocol)],
inputStruct);
return new ReturnStruct()
{
diff --git a/Modbus.Net/Modbus.Net.sln b/Modbus.Net/Modbus.Net.sln
index b1e9fb1..0e88db8 100644
--- a/Modbus.Net/Modbus.Net.sln
+++ b/Modbus.Net/Modbus.Net.sln
@@ -34,6 +34,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CrossLamp", "..\Samples\Cro
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TripleAdd", "..\Samples\TripleAdd\TripleAdd.csproj", "{414956B8-DBD4-414C-ABD3-565580739646}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modbus.Net.Modbus.SelfDefinedSample", "Modbus.Net.Modbus.SelfDefinedSample\Modbus.Net.Modbus.SelfDefinedSample.csproj", "{C4FA55AF-80ED-4467-948F-8EF865C8A5A5}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -84,6 +86,10 @@ Global
{414956B8-DBD4-414C-ABD3-565580739646}.Debug|Any CPU.Build.0 = Debug|Any CPU
{414956B8-DBD4-414C-ABD3-565580739646}.Release|Any CPU.ActiveCfg = Release|Any CPU
{414956B8-DBD4-414C-ABD3-565580739646}.Release|Any CPU.Build.0 = Release|Any CPU
+ {C4FA55AF-80ED-4467-948F-8EF865C8A5A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {C4FA55AF-80ED-4467-948F-8EF865C8A5A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {C4FA55AF-80ED-4467-948F-8EF865C8A5A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {C4FA55AF-80ED-4467-948F-8EF865C8A5A5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs b/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs
index 384567f..907c4e8 100644
--- a/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs
+++ b/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs
@@ -97,23 +97,4 @@ namespace Modbus.Net
/// 是否设置成功
Task> SetDatasAsync(string startAddress, object[] setContents);
}
-
- ///
- /// Utility的时间读写接口
- ///
- public interface IUtilityMethodTime : IUtilityMethod
- {
- ///
- /// 获取PLC时间
- ///
- /// PLC时间
- Task> GetTimeAsync();
-
- ///
- /// 设置PLC时间
- ///
- /// 设置PLC时间
- /// 设置是否成功
- Task> SetTimeAsync(DateTime setTime);
- }
}
\ No newline at end of file
diff --git a/Tests/Modbus.Net.Tests/MachineMethodTest.cs b/Tests/Modbus.Net.Tests/MachineMethodTest.cs
index 3f78a13..641555b 100644
--- a/Tests/Modbus.Net.Tests/MachineMethodTest.cs
+++ b/Tests/Modbus.Net.Tests/MachineMethodTest.cs
@@ -11,10 +11,10 @@ namespace Modbus.Net.Tests
public void GetUtility()
{
BaseMachine baseMachine = new ModbusMachine(1, 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);
+ Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "GetDataAsync") != null, true);
+ Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "SetDataAsync") != null, true);
baseMachine.Disconnect();
}
@@ -22,10 +22,10 @@ namespace Modbus.Net.Tests
public async Task InvokeUtility()
{
BaseMachine baseMachine = new ModbusMachine(1, ModbusType.Tcp, "192.168.3.12", null, true, 2, 0);
- var success = await baseMachine.BaseUtility.GetUtilityMethods().SetTimeAsync(DateTime.Now);
+ var success = await baseMachine.BaseUtility.GetUtilityMethods().SetDatasAsync("4X 1", new object[] {(byte)11});
Assert.AreEqual(success, true);
- var time = await baseMachine.BaseUtility.GetUtilityMethods().GetTimeAsync();
- Assert.AreEqual((time.Datas.ToUniversalTime() - DateTime.Now.ToUniversalTime()).Seconds < 10, true);
+ var datas = await baseMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1);
+ Assert.AreEqual(datas.Datas[0], 11);
baseMachine.Disconnect();
}
diff --git a/Tests/Modbus.Net.Tests/ModbusTest.cs b/Tests/Modbus.Net.Tests/ModbusTest.cs
index 799850d..baa2a04 100644
--- a/Tests/Modbus.Net.Tests/ModbusTest.cs
+++ b/Tests/Modbus.Net.Tests/ModbusTest.cs
@@ -289,18 +289,18 @@ namespace Modbus.Net.Tests
}
};
- await _modbusTcpMachine!.BaseUtility.GetUtilityMethods().SetSingleDataAsync("4X 1", dic1["4X 1"]);
- await _modbusAsciiMachine!.BaseUtility.GetUtilityMethods().SetSingleDataAsync("4X 1", dic1["4X 1"]);
- await _modbusRtuMachine!.BaseUtility.GetUtilityMethods().SetSingleDataAsync("4X 1", dic1["4X 1"]);
+ await _modbusTcpMachine!.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("4X 1", dic1["4X 1"]);
+ await _modbusAsciiMachine!.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("4X 1", dic1["4X 1"]);
+ await _modbusRtuMachine!.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("4X 1", dic1["4X 1"]);
var ans = await _modbusTcpMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1);
var ans2 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1);
var ans3 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1);
Assert.AreEqual(ans.Datas[0], dic1["4X 1"]);
Assert.AreEqual(ans2.Datas[0], dic1["4X 1"]);
Assert.AreEqual(ans3.Datas[0], dic1["4X 1"]);
- await _modbusTcpMachine.BaseUtility.GetUtilityMethods().SetSingleDataAsync("0X 1", dic2["0X 1"] >= 1);
- await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().SetSingleDataAsync("0X 1", dic2["0X 1"] >= 1);
- await _modbusRtuMachine.BaseUtility.GetUtilityMethods().SetSingleDataAsync("0X 1", dic2["0X 1"] >= 1);
+ await _modbusTcpMachine.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("0X 1", dic2["0X 1"] >= 1);
+ await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("0X 1", dic2["0X 1"] >= 1);
+ await _modbusRtuMachine.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("0X 1", dic2["0X 1"] >= 1);
var ans21 = await _modbusTcpMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1);
var ans22 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1);
var ans23 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1);