using IoTClient.Enums;
using PluginInterface;
using IoTClient.Clients.PLC;
using Microsoft.Extensions.Logging;
using DataTypeEnum = IoTClient.Enums.DataTypeEnum;
using System.Text;
using System.Linq;
namespace PLC.MelsecMc
{
[DriverSupported("A_1E")]
[DriverSupported("Qna_3E")]
[DriverInfo("MelsecMc", "V1.0.0", "Copyright IoTGateway.net 20230220")]
public class DeviceMelsecMc : IDriver
{
private MitsubishiClient? _plc;
public ILogger _logger { get; set; }
private readonly string _device;
#region 配置参数
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
[ConfigParameter("PLC类型")] public MitsubishiVersion CpuType { get; set; } = MitsubishiVersion.Qna_3E;
[ConfigParameter("IP地址")] public string IpAddress { get; set; } = "127.0.0.1";
[ConfigParameter("端口号")] public int Port { get; set; } = 6000;
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
#endregion
#region 生命周期
///
/// 反射构造函数
///
///
///
public DeviceMelsecMc(string device, ILogger logger)
{
_device = device;
_logger = logger;
_logger.LogInformation($"Device:[{_device}],Create()");
}
///
/// 连接状态
///
public bool IsConnected => _plc is { Connected: true };
///
/// 连接
///
///
public bool Connect()
{
try
{
_logger.LogInformation($"Device:[{_device}],Connect()");
_plc = new MitsubishiClient(CpuType, IpAddress, Port);
_plc.Open();
}
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Connect(),Error");
return false;
}
return IsConnected;
}
///
/// 断开
///
///
public bool Close()
{
try
{
_logger.LogInformation($"Device:[{_device}],Close()");
_plc?.Close();
return !IsConnected;
}
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Close(),Error");
return false;
}
}
///
/// 释放
///
public void Dispose()
{
try
{
_logger.LogInformation($"Device:[{_device}],Dispose()");
// Suppress finalization.
GC.SuppressFinalize(this);
}
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Dispose(),Error");
}
}
#endregion
#region 读写方法
///
/// 读
///
///
///
[Method("读PLC标准地址", description: "读PLC标准地址")]
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (_plc != null && this.IsConnected)
{
try
{
switch (ioArg.ValueType)
{
case PluginInterface.DataTypeEnum.Bit:
ret.Value = _plc.ReadBoolean(ioArg.Address).Value ? 1 : 0;
break;
case PluginInterface.DataTypeEnum.Bool:
ret.Value = _plc.ReadBoolean(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.UByte:
ret.Value = _plc.ReadByte(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Byte:
ret.Value = (sbyte)_plc.ReadByte(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Uint16:
ret.Value = _plc.ReadUInt16(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Int16:
ret.Value = _plc.ReadInt16(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Uint32:
ret.Value = _plc.ReadUInt32(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Int32:
ret.Value = _plc.ReadInt32(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Float:
ret.Value = _plc.ReadFloat(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Double:
ret.Value = _plc.ReadDouble(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Uint64:
ret.Value = _plc.ReadUInt64(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.Int64:
ret.Value = _plc.ReadInt64(ioArg.Address).Value;
break;
case PluginInterface.DataTypeEnum.AsciiString:
ret.Value = _plc.ReadString(ioArg.Address);
break;
case PluginInterface.DataTypeEnum.Utf8String:
ret.Value = _plc.ReadString(ioArg.Address);
break;
default:
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,不支持的类型:{ioArg.ValueType}";
break;
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,{ex.Message}";
}
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败";
}
return ret;
}
private Dictionary _cache = new();
///
/// 读
///
///
///
[Method("批量读PLC标准地址", description: "数据区,开始地址,地址间隔,读取总数")]
public DriverReturnValueModel ReadMultiple(DriverAddressIoArgModel ioArg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
if (_plc != null && this.IsConnected)
{
try
{
var addresStrings = ioArg.Address.Split(',');
string block = addresStrings[0];
ushort start = ushort.Parse(addresStrings[1]);
ushort step = ushort.Parse(addresStrings[2]);
ushort count = ushort.Parse(addresStrings[3]);
var dataType = DataTypeEnum.UInt16;
switch (ioArg.ValueType)
{
case PluginInterface.DataTypeEnum.Bool:
dataType = DataTypeEnum.Bool;
break;
case PluginInterface.DataTypeEnum.Bit:
case PluginInterface.DataTypeEnum.UByte:
case PluginInterface.DataTypeEnum.Byte:
dataType = DataTypeEnum.Byte;
break;
case PluginInterface.DataTypeEnum.Uint16:
dataType = DataTypeEnum.UInt16;
break;
case PluginInterface.DataTypeEnum.Int16:
dataType = DataTypeEnum.Int16;
break;
case PluginInterface.DataTypeEnum.Uint32:
dataType = DataTypeEnum.UInt32;
break;
case PluginInterface.DataTypeEnum.Int32:
dataType = DataTypeEnum.Int32;
break;
case PluginInterface.DataTypeEnum.Float:
dataType = DataTypeEnum.Float;
break;
case PluginInterface.DataTypeEnum.Double:
dataType = DataTypeEnum.Double;
break;
case PluginInterface.DataTypeEnum.Uint64:
dataType = DataTypeEnum.UInt64;
break;
case PluginInterface.DataTypeEnum.Int64:
dataType = DataTypeEnum.Int64;
break;
case PluginInterface.DataTypeEnum.AsciiString:
dataType = DataTypeEnum.String;
break;
case PluginInterface.DataTypeEnum.Utf8String:
dataType = DataTypeEnum.String;
break;
default:
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,不支持的类型:{ioArg.ValueType}";
break;
}
var batchAddress = new Dictionary();
for (int i = 0; i < count; i++)
{
batchAddress[$"{block}{start + i * step}"] = dataType;
}
try
{
var batchResult = _plc.BatchRead(batchAddress, count);
if (batchResult.IsSucceed)
{
foreach (var value in batchResult.Value)
{
_cache[value.Key] = value.Value;
}
ret.Value = batchResult.Value.Select(x => x.Value).ToList();
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
foreach (var address in batchAddress)
{
if (_cache.ContainsKey(address.Key))
_cache.Remove(address.Key);
}
}
}
catch (Exception e)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
foreach (var address in batchAddress)
{
if (_cache.ContainsKey(address.Key))
_cache.Remove(address.Key);
}
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = $"读取失败,{ex.Message}";
}
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = "连接失败";
}
return ret;
}
///
/// 从缓存内读
///
///
///
[Method("从缓存读取", description: "从缓存读取")]
public DriverReturnValueModel ReadFromCache(DriverAddressIoArgModel ioArg)
{
DriverReturnValueModel ret = new();
try
{
if (_cache.ContainsKey(ioArg.Address) && _cache[ioArg.Address] != null)
{
ret.StatusType = VaribaleStatusTypeEnum.Good;
ret.Value = _cache[ioArg.Address];
}
else
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
_logger.LogError(ex, $"Device:[{_device}],ReadFromCache(),Error");
}
return ret;
}
///
/// 写
///
///
///
///
///
public async Task WriteAsync(string requestId, string method, DriverAddressIoArgModel ioArg)
{
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
await Task.CompletedTask;
return rpcResponse;
}
#endregion
}
}