2017-05-17 update 1 Add Comments and Reformat code.
This commit is contained in:
@@ -12,10 +12,12 @@ namespace Modbus.Net.Modbus
|
||||
/// 读功能码
|
||||
/// </summary>
|
||||
protected Dictionary<string, AreaOutputDef> ReadFunctionCodeDictionary;
|
||||
|
||||
/// <summary>
|
||||
/// 功能码翻译至标准Modbus地址位置
|
||||
/// </summary>
|
||||
protected Dictionary<string, int> TransDictionary;
|
||||
|
||||
/// <summary>
|
||||
/// 写功能码
|
||||
/// </summary>
|
||||
@@ -201,6 +203,7 @@ namespace Modbus.Net.Modbus
|
||||
/// 读功能码
|
||||
/// </summary>
|
||||
protected Dictionary<string, AreaOutputDef> ReadFunctionCodeDictionary;
|
||||
|
||||
/// <summary>
|
||||
/// 写功能码
|
||||
/// </summary>
|
||||
|
||||
@@ -8,7 +8,8 @@ namespace Modbus.Net.Modbus
|
||||
/// </summary>
|
||||
public class ModbusAsciiProtocalLinker : ComProtocalLinker
|
||||
{
|
||||
public ModbusAsciiProtocalLinker(string com, int slaveAddress) : base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress)
|
||||
public ModbusAsciiProtocalLinker(string com, int slaveAddress)
|
||||
: base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -18,14 +19,10 @@ namespace Modbus.Net.Modbus
|
||||
//CRC校验失败
|
||||
var contentString = Encoding.ASCII.GetString(content);
|
||||
if (!Crc16.GetInstance().LrcEfficacy(contentString))
|
||||
{
|
||||
throw new ModbusProtocalErrorException(501);
|
||||
}
|
||||
//Modbus协议错误
|
||||
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
||||
{
|
||||
throw new ModbusProtocalErrorException(byte.Parse(contentString.Substring(5, 2)));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,8 @@ namespace Modbus.Net.Modbus
|
||||
/// </summary>
|
||||
public abstract class ModbusProtocal : BaseProtocal
|
||||
{
|
||||
protected ModbusProtocal(byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian)
|
||||
protected ModbusProtocal(byte slaveAddress, byte masterAddress, Endian endian)
|
||||
: base(slaveAddress, masterAddress, endian)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -73,7 +74,8 @@ namespace Modbus.Net.Modbus
|
||||
var translateAddress = addressTranslator.AddressTranslate(startAddress, true);
|
||||
FunctionCode = (byte) translateAddress.Area;
|
||||
StartAddress = (ushort) translateAddress.Address;
|
||||
GetCount = (ushort) Math.Ceiling(getCount/addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
GetCount =
|
||||
(ushort) Math.Ceiling(getCount / addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
}
|
||||
|
||||
public byte SlaveAddress { get; }
|
||||
@@ -140,7 +142,7 @@ namespace Modbus.Net.Modbus
|
||||
StartAddress = (ushort) translateAddress.Address;
|
||||
var writeByteValue = ValueHelper.GetInstance(endian).ObjectArrayToByteArray(writeValue);
|
||||
WriteCount =
|
||||
(ushort) (writeByteValue.Length/addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
(ushort) (writeByteValue.Length / addressTranslator.GetAreaByteLength(translateAddress.AreaString));
|
||||
WriteByteCount = (byte) writeByteValue.Length;
|
||||
WriteValue = writeByteValue;
|
||||
}
|
||||
|
||||
@@ -61,9 +61,7 @@ namespace Modbus.Net.Modbus
|
||||
var newContent = new List<byte>();
|
||||
newContent.AddRange(Encoding.ASCII.GetBytes(":"));
|
||||
foreach (var number in content)
|
||||
{
|
||||
newContent.AddRange(Encoding.ASCII.GetBytes(number.ToString("X2")));
|
||||
}
|
||||
newContent.AddRange(Encoding.ASCII.GetBytes(Crc16.GetInstance().GetLRC(content)));
|
||||
newContent.Add(0x0d);
|
||||
newContent.Add(0x0a);
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace Modbus.Net.Modbus
|
||||
/// </summary>
|
||||
public class ModbusRtuProtocalLinker : ComProtocalLinker
|
||||
{
|
||||
public ModbusRtuProtocalLinker(string com, int slaveAddress) : base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress)
|
||||
public ModbusRtuProtocalLinker(string com, int slaveAddress)
|
||||
: base(com, 9600, Parity.None, StopBits.One, 8, slaveAddress)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -16,14 +17,10 @@ namespace Modbus.Net.Modbus
|
||||
if (!base.CheckRight(content).Value) return false;
|
||||
//CRC校验失败
|
||||
if (!Crc16.GetInstance().CrcEfficacy(content))
|
||||
{
|
||||
throw new ModbusProtocalErrorException(501);
|
||||
}
|
||||
//Modbus协议错误
|
||||
if (content[1] > 127)
|
||||
{
|
||||
throw new ModbusProtocalErrorException(content[2]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,8 @@ namespace Modbus.Net.Modbus
|
||||
{
|
||||
}
|
||||
|
||||
public ModbusTcpProtocal(string ip, byte slaveAddress, byte masterAddress, Endian endian) : base(slaveAddress, masterAddress, endian)
|
||||
public ModbusTcpProtocal(string ip, byte slaveAddress, byte masterAddress, Endian endian)
|
||||
: base(slaveAddress, masterAddress, endian)
|
||||
{
|
||||
ProtocalLinker = new ModbusTcpProtocalLinker(ip);
|
||||
}
|
||||
|
||||
@@ -7,7 +7,8 @@ namespace Modbus.Net.Modbus
|
||||
/// </summary>
|
||||
public class ModbusTcpProtocalLinker : TcpProtocalLinker
|
||||
{
|
||||
public ModbusTcpProtocalLinker(string ip) : base(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502"))
|
||||
public ModbusTcpProtocalLinker(string ip)
|
||||
: base(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502"))
|
||||
{
|
||||
}
|
||||
|
||||
@@ -20,14 +21,10 @@ namespace Modbus.Net.Modbus
|
||||
if (!base.CheckRight(content).Value) return false;
|
||||
//长度校验失败
|
||||
if (content[5] != content.Length - 6)
|
||||
{
|
||||
throw new ModbusProtocalErrorException(500);
|
||||
}
|
||||
//Modbus协议错误
|
||||
if (content[7] > 127)
|
||||
{
|
||||
throw new ModbusProtocalErrorException(content[2] > 0 ? content[2] : content[8]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,14 +27,15 @@ namespace Modbus.Net.Modbus
|
||||
/// <summary>
|
||||
/// Modbus基础Api入口
|
||||
/// </summary>
|
||||
public class ModbusUtility : BaseUtility, IUtilityMethodTime
|
||||
public class ModbusUtility : BaseUtility, IIUtilityMethodTime
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus协议类型
|
||||
/// </summary>
|
||||
private ModbusType _modbusType;
|
||||
|
||||
public ModbusUtility(int connectionType, byte slaveAddress, byte masterAddress, Endian endian = Endian.BigEndianLsb)
|
||||
public ModbusUtility(int connectionType, byte slaveAddress, byte masterAddress,
|
||||
Endian endian = Endian.BigEndianLsb)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
Endian = endian;
|
||||
@@ -43,7 +44,8 @@ namespace Modbus.Net.Modbus
|
||||
AddressTranslator = new AddressTranslatorModbus();
|
||||
}
|
||||
|
||||
public ModbusUtility(ModbusType connectionType, string connectionString, byte slaveAddress, byte masterAddress, Endian endian = Endian.BigEndianLsb)
|
||||
public ModbusUtility(ModbusType connectionType, string connectionString, byte slaveAddress, byte masterAddress,
|
||||
Endian endian = Endian.BigEndianLsb)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
Endian = endian;
|
||||
@@ -120,6 +122,47 @@ namespace Modbus.Net.Modbus
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读时间
|
||||
/// </summary>
|
||||
/// <returns>设备的时间</returns>
|
||||
public async Task<DateTime> GetTimeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new GetSystemTimeModbusInputStruct(SlaveAddress);
|
||||
var outputStruct =
|
||||
await Wrapper.SendReceiveAsync<GetSystemTimeModbusOutputStruct>(
|
||||
Wrapper[typeof(GetSystemTimeModbusProtocal)], inputStruct);
|
||||
return outputStruct?.Time ?? DateTime.MinValue;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写时间
|
||||
/// </summary>
|
||||
/// <param name="setTime">需要写入的时间</param>
|
||||
/// <returns>写入是否成功</returns>
|
||||
public async Task<bool> SetTimeAsync(DateTime setTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new SetSystemTimeModbusInputStruct(SlaveAddress, setTime);
|
||||
var outputStruct =
|
||||
await Wrapper.SendReceiveAsync<SetSystemTimeModbusOutputStruct>(
|
||||
Wrapper[typeof(SetSystemTimeModbusProtocal)], inputStruct);
|
||||
return outputStruct?.WriteCount > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void SetConnectionType(int connectionType)
|
||||
{
|
||||
ModbusType = (ModbusType) connectionType;
|
||||
@@ -138,7 +181,8 @@ namespace Modbus.Net.Modbus
|
||||
var inputStruct = new ReadDataModbusInputStruct(SlaveAddress, startAddress,
|
||||
(ushort) getByteCount, AddressTranslator);
|
||||
var outputStruct = await
|
||||
Wrapper.SendReceiveAsync<ReadDataModbusOutputStruct>(Wrapper[typeof (ReadDataModbusProtocal)], inputStruct);
|
||||
Wrapper.SendReceiveAsync<ReadDataModbusOutputStruct>(Wrapper[typeof(ReadDataModbusProtocal)],
|
||||
inputStruct);
|
||||
return outputStruct?.DataValue;
|
||||
}
|
||||
catch
|
||||
@@ -160,7 +204,8 @@ namespace Modbus.Net.Modbus
|
||||
var inputStruct = new WriteDataModbusInputStruct(SlaveAddress, startAddress, setContents,
|
||||
AddressTranslator, Endian);
|
||||
var outputStruct = await
|
||||
Wrapper.SendReceiveAsync<WriteDataModbusOutputStruct>(Wrapper[typeof (WriteDataModbusProtocal)], inputStruct);
|
||||
Wrapper.SendReceiveAsync<WriteDataModbusOutputStruct>(Wrapper[typeof(WriteDataModbusProtocal)],
|
||||
inputStruct);
|
||||
return outputStruct?.WriteCount == setContents.Length;
|
||||
}
|
||||
catch
|
||||
@@ -168,44 +213,5 @@ namespace Modbus.Net.Modbus
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读时间
|
||||
/// </summary>
|
||||
/// <returns>设备的时间</returns>
|
||||
public async Task<DateTime> GetTimeAsync()
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new GetSystemTimeModbusInputStruct(SlaveAddress);
|
||||
var outputStruct =
|
||||
await Wrapper.SendReceiveAsync<GetSystemTimeModbusOutputStruct>(Wrapper[typeof(GetSystemTimeModbusProtocal)], inputStruct);
|
||||
return outputStruct?.Time ?? DateTime.MinValue;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写时间
|
||||
/// </summary>
|
||||
/// <param name="setTime">需要写入的时间</param>
|
||||
/// <returns>写入是否成功</returns>
|
||||
public async Task<bool> SetTimeAsync(DateTime setTime)
|
||||
{
|
||||
try
|
||||
{
|
||||
var inputStruct = new SetSystemTimeModbusInputStruct(SlaveAddress, setTime);
|
||||
var outputStruct =
|
||||
await Wrapper.SendReceiveAsync<SetSystemTimeModbusOutputStruct>(Wrapper[typeof(SetSystemTimeModbusProtocal)], inputStruct);
|
||||
return outputStruct?.WriteCount > 0;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user