Files
KinginfoGateway/framework/Plugin/ThingsGateway.Plugin.DLT645/DLT645_2007OverTcp/DLT645_2007OverTcp.cs

104 lines
3.4 KiB
C#
Raw Normal View History

2023-08-25 19:33:08 +08:00
#region copyright
//------------------------------------------------------------------------------
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
// 此代码版权除特别声明外的代码归作者本人Diego所有
// 源代码使用协议遵循本仓库的开源协议及附加协议
// Gitee源代码仓库https://gitee.com/diego2098/ThingsGateway
// Github源代码仓库https://github.com/kimdiego2098/ThingsGateway
// 使用文档https://diego2098.gitee.io/thingsgateway-docs/
// QQ群605534569
//------------------------------------------------------------------------------
#endregion
2023-09-30 23:05:53 +08:00
namespace ThingsGateway.Plugin.DLT645;
2023-08-25 19:33:08 +08:00
/// <inheritdoc/>
public class DLT645_2007OverTcp : CollectBase
{
2023-09-30 23:05:53 +08:00
private readonly DLT645_2007OverTcpProperty driverPropertys = new();
2023-08-25 19:33:08 +08:00
private ThingsGateway.Foundation.Adapter.DLT645.DLT645_2007OverTcp _plc;
2023-09-30 23:05:53 +08:00
2023-08-25 19:33:08 +08:00
/// <inheritdoc/>
2023-09-30 23:05:53 +08:00
public override Type DriverDebugUIType => typeof(DLT645_2007OverTcpDebugPage);
2023-08-25 19:33:08 +08:00
/// <inheritdoc/>
public override CollectDriverPropertyBase DriverPropertys => driverPropertys;
2023-09-30 23:05:53 +08:00
2023-08-25 19:33:08 +08:00
/// <inheritdoc/>
public override bool IsSupportRequest => true;
2023-09-30 23:05:53 +08:00
2023-08-25 19:33:08 +08:00
/// <inheritdoc/>
public override IThingsGatewayBitConverter ThingsGatewayBitConverter { get => _plc?.ThingsGatewayBitConverter; }
2023-09-30 23:05:53 +08:00
/// <inheritdoc/>
protected override IReadWrite PLC => _plc;
2023-08-25 19:33:08 +08:00
/// <inheritdoc/>
public override Task AfterStopAsync()
{
_plc?.Disconnect();
return Task.CompletedTask;
}
/// <inheritdoc/>
2023-09-30 23:05:53 +08:00
public override async Task BeforStartAsync(CancellationToken cancellationToken)
2023-08-25 19:33:08 +08:00
{
2023-09-30 23:05:53 +08:00
await _plc?.ConnectAsync(cancellationToken);
2023-08-25 19:33:08 +08:00
}
/// <inheritdoc/>
public override void InitDataAdapter()
{
_plc.SetDataAdapter();
}
/// <inheritdoc/>
public override bool IsConnected()
{
2023-09-30 23:05:53 +08:00
return _plc?.TcpClient?.CanSend == true;
2023-08-25 19:33:08 +08:00
}
/// <inheritdoc/>
public override List<DeviceVariableSourceRead> LoadSourceRead(List<DeviceVariableRunTime> deviceVariables)
{
2023-09-30 23:05:53 +08:00
return _plc.LoadSourceRead<DeviceVariableSourceRead, DeviceVariableRunTime>(deviceVariables, 0);
2023-08-25 19:33:08 +08:00
}
/// <inheritdoc/>
protected override void Dispose(bool disposing)
{
_plc?.Disconnect();
base.Dispose(disposing);
}
/// <inheritdoc/>
protected override void Init(CollectDeviceRunTime device, object client = null)
{
if (client == null)
{
FoundataionConfig.SetRemoteIPHost(new IPHost($"{driverPropertys.IP}:{driverPropertys.Port}"))
;
2023-09-30 23:05:53 +08:00
client = new TcpClient();
((TcpClient)client).Setup(FoundataionConfig);
2023-08-25 19:33:08 +08:00
}
//载入配置
2023-09-30 23:05:53 +08:00
_plc = new((TcpClient)client)
2023-08-25 19:33:08 +08:00
{
FrameTime = driverPropertys.FrameTime,
CacheTimeout = driverPropertys.CacheTimeout,
DataFormat = driverPropertys.DataFormat,
EnableFEHead = driverPropertys.EnableFEHead,
OperCode = driverPropertys.OperCode,
Password = driverPropertys.Password,
Station = driverPropertys.Station,
TimeOut = driverPropertys.TimeOut
};
}
/// <inheritdoc/>
2023-09-30 23:05:53 +08:00
protected override async Task<OperResult<byte[]>> ReadAsync(string address, int length, CancellationToken cancellationToken)
2023-08-25 19:33:08 +08:00
{
2023-09-30 23:05:53 +08:00
return await _plc.ReadAsync(address, length, cancellationToken);
2023-08-25 19:33:08 +08:00
}
}