#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
// QQ群:605534569
//------------------------------------------------------------------------------
#endregion
using System.ComponentModel;
namespace ThingsGateway.Foundation.Adapter.Modbus;
///
public class ModbusRtuOverTcp : ReadWriteDevicesTcpClientBase
{
///
public ModbusRtuOverTcp(TcpClient tcpClient) : base(tcpClient)
{
ThingsGatewayBitConverter = new ThingsGatewayBitConverter(EndianType.Big);
RegisterByteLength = 2;
}
///
/// Crc校验
///
[Description("Crc校验")]
public bool Crc16CheckEnable { get; set; } = true;
///
/// 站号
///
[Description("站号")]
public byte Station { get; set; } = 1;
///
public override string GetAddressDescription()
{
return base.GetAddressDescription() + Environment.NewLine + ModbusHelper.GetAddressDescription();
}
///
public override OperResult Read(string address, int length, CancellationToken cancellationToken = default)
{
try
{
Connect(cancellationToken);
var commandResult = ModbusHelper.GetReadModbusCommand(address, length, Station);
return SendThenReturn(commandResult, cancellationToken);
}
catch (Exception ex)
{
return new OperResult(ex);
}
}
///
public override async Task> ReadAsync(string address, int length, CancellationToken cancellationToken = default)
{
try
{
await ConnectAsync(cancellationToken);
var commandResult = ModbusHelper.GetReadModbusCommand(address, length, Station);
return await SendThenReturnAsync(commandResult, cancellationToken);
}
catch (Exception ex)
{
return new OperResult(ex);
}
}
///
public override List LoadSourceRead(List deviceVariables, int maxPack)
{
return PackHelper.LoadSourceRead(this, deviceVariables, maxPack);
}
///
public override void SetDataAdapter(object socketClient = null)
{
ModbusRtuDataHandleAdapter dataHandleAdapter = new()
{
Crc16CheckEnable = Crc16CheckEnable,
CacheTimeout = TimeSpan.FromMilliseconds(CacheTimeout)
};
TcpClient.SetDataHandlingAdapter(dataHandleAdapter);
}
///
public override OperResult Write(string address, byte[] value, CancellationToken cancellationToken = default)
{
try
{
Connect(cancellationToken);
var commandResult = ModbusHelper.GetWriteModbusCommand(address, value, Station);
return SendThenReturn(commandResult, cancellationToken);
}
catch (Exception ex)
{
return new OperResult(ex);
}
}
///
public override OperResult Write(string address, bool[] value, CancellationToken cancellationToken = default)
{
try
{
Connect(cancellationToken);
var commandResult = ModbusHelper.GetWriteBoolModbusCommand(address, value, Station);
return SendThenReturn(commandResult, cancellationToken);
}
catch (Exception ex)
{
return new OperResult(ex);
}
}
///
public override async Task WriteAsync(string address, byte[] value, CancellationToken cancellationToken = default)
{
try
{
await ConnectAsync(cancellationToken);
var commandResult = ModbusHelper.GetWriteModbusCommand(address, value, Station);
return await SendThenReturnAsync(commandResult, cancellationToken);
}
catch (Exception ex)
{
return new OperResult(ex);
}
}
///
public override async Task WriteAsync(string address, bool[] value, CancellationToken cancellationToken = default)
{
try
{
await ConnectAsync(cancellationToken);
var commandResult = ModbusHelper.GetWriteBoolModbusCommand(address, value, Station);
return await SendThenReturnAsync(commandResult, cancellationToken);
}
catch (Exception ex)
{
return new OperResult(ex);
}
}
private OperResult SendThenReturn(OperResult commandResult, CancellationToken cancellationToken)
{
if (commandResult.IsSuccess)
{
var item = commandResult.Content;
if (FrameTime != 0)
Thread.Sleep(FrameTime);
var result = WaitingClientEx.SendThenResponse(item, TimeOut, cancellationToken);
return (MessageBase)result.RequestInfo;
}
else
{
return new OperResult(commandResult.Message);
}
}
private async Task> SendThenReturnAsync(OperResult commandResult, CancellationToken cancellationToken)
{
if (commandResult.IsSuccess)
{
var item = commandResult.Content;
await Task.Delay(FrameTime, cancellationToken);
var result = await WaitingClientEx.SendThenResponseAsync(item, TimeOut, cancellationToken);
return (MessageBase)result.RequestInfo;
}
else
{
return new OperResult(commandResult.Message);
}
}
}