iotgateway/Plugins/Drivers/PLC.MelsecMc/DeviceMelsecMc.cs

375 lines
14 KiB
C#
Raw Normal View History

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
{
2022-08-10 08:52:26 +00:00
private MitsubishiClient? _plc;
public ILogger _logger { get; set; }
2022-08-08 07:15:09 +00:00
private readonly string _device;
2022-08-10 08:52:26 +00:00
#region
2022-08-10 08:52:26 +00:00
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
2022-08-10 08:52:26 +00:00
[ConfigParameter("PLC类型")] public MitsubishiVersion CpuType { get; set; } = MitsubishiVersion.Qna_3E;
2022-08-10 08:52:26 +00:00
[ConfigParameter("IP地址")] public string IpAddress { get; set; } = "127.0.0.1";
2022-08-10 08:52:26 +00:00
[ConfigParameter("端口号")] public int Port { get; set; } = 6000;
2022-08-10 08:52:26 +00:00
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 3000;
2022-08-10 08:52:26 +00:00
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
#endregion
#region
/// <summary>
/// 反射构造函数
/// </summary>
/// <param name="device"></param>
/// <param name="logger"></param>
public DeviceMelsecMc(string device, ILogger logger)
{
_device = device;
_logger = logger;
2022-08-08 07:15:09 +00:00
_logger.LogInformation($"Device:[{_device}],Create()");
}
/// <summary>
/// 连接状态
/// </summary>
public bool IsConnected => _plc is { Connected: true };
/// <summary>
/// 连接
/// </summary>
/// <returns></returns>
public bool Connect()
{
try
{
_logger.LogInformation($"Device:[{_device}],Connect()");
_plc = new MitsubishiClient(CpuType, IpAddress, Port);
2022-08-10 08:52:26 +00:00
_plc.Open();
}
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Connect(),Error");
return false;
}
2022-08-10 08:52:26 +00:00
return IsConnected;
}
/// <summary>
/// 断开
/// </summary>
/// <returns></returns>
public bool Close()
{
try
{
_logger.LogInformation($"Device:[{_device}],Close()");
2022-08-10 08:52:26 +00:00
_plc?.Close();
return !IsConnected;
}
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Close(),Error");
return false;
}
}
/// <summary>
/// 释放
/// </summary>
public void Dispose()
{
try
{
_logger.LogInformation($"Device:[{_device}],Dispose()");
// Suppress finalization.
GC.SuppressFinalize(this);
}
2022-08-10 08:52:26 +00:00
catch (Exception ex)
{
_logger.LogError(ex, $"Device:[{_device}],Dispose(),Error");
}
}
#endregion
#region
/// <summary>
/// 读
/// </summary>
/// <param name="ioArg"></param>
/// <returns></returns>
[Method("读PLC标准地址", description: "读PLC标准地址")]
public DriverReturnValueModel Read(DriverAddressIoArgModel ioArg)
{
var ret = new DriverReturnValueModel { StatusType = VaribaleStatusTypeEnum.Good };
2022-08-10 08:52:26 +00:00
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:
2022-08-10 08:52:26 +00:00
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 = "连接失败";
}
2022-08-10 08:52:26 +00:00
return ret;
}
2022-08-10 08:52:26 +00:00
private Dictionary<string, object> _cache = new();
/// <summary>
/// 读
/// </summary>
/// <param name="ioArg"></param>
/// <returns></returns>
[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<string, DataTypeEnum>();
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;
}
/// <summary>
/// 从缓存内读
/// </summary>
/// <param name="ioArg"></param>
/// <returns></returns>
[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;
}
/// <summary>
/// 写
/// </summary>
/// <param name="requestId"></param>
/// <param name="method"></param>
/// <param name="ioArg"></param>
/// <returns></returns>
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioArg)
2022-04-13 09:01:24 +00:00
{
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
await Task.CompletedTask;
2022-04-13 09:01:24 +00:00
return rpcResponse;
}
#endregion
}
}