2022-01-15 16:16:53 +00:00
|
|
|
|
using IoTClient.Clients.PLC;
|
|
|
|
|
using IoTClient.Enums;
|
|
|
|
|
using PluginInterface;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Text;
|
2022-08-06 10:21:00 +00:00
|
|
|
|
using IoTGateway.Model;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
|
|
|
|
|
namespace DriverMitsubishi
|
|
|
|
|
{
|
|
|
|
|
[DriverSupported("A_1E")]
|
|
|
|
|
[DriverSupported("Qna_3E")]
|
2022-06-09 08:11:37 +00:00
|
|
|
|
[DriverInfoAttribute("Mitsubishi", "V1.0.0", "Copyright IoTGateway-IoTClient© 2022-01-15")]
|
2022-01-15 16:16:53 +00:00
|
|
|
|
public class Mitsubishi : IDriver
|
|
|
|
|
{
|
|
|
|
|
private MitsubishiClient plc = null;
|
2022-08-06 10:21:00 +00:00
|
|
|
|
|
|
|
|
|
public ILogger _logger { get; set; }
|
|
|
|
|
private readonly Device _device;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
#region 配置参数
|
|
|
|
|
|
|
|
|
|
[ConfigParameter("设备Id")]
|
|
|
|
|
public Guid 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
|
|
|
|
|
|
2022-08-06 10:21:00 +00:00
|
|
|
|
public Mitsubishi(Device device, ILogger logger)
|
2022-01-15 16:16:53 +00:00
|
|
|
|
{
|
2022-08-06 10:21:00 +00:00
|
|
|
|
_device = device;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
|
2022-01-15 16:16:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsConnected
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return plc != null && plc.Connected;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Connect()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
plc = new MitsubishiClient(MitsubishiVersion.Qna_3E, IpAddress, Port);
|
|
|
|
|
plc.Open();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return IsConnected;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Close()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
plc?.Close();
|
|
|
|
|
return !IsConnected;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
plc = null;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadBoolean(ioarg.Address).Value == true ? 1 : 0;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Bool:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadBoolean(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.UByte:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadByte(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Byte:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = (sbyte)plc.ReadByte(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Uint16:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value =plc.ReadUInt16(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Int16:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadInt16(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Uint32:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value =plc.ReadUInt32(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Int32:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadInt32(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Float:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadFloat(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Double:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadDouble(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Uint64:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadUInt64(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Int64:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadInt64(ioarg.Address).Value;
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.AsciiString:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadString(ioarg.Address);
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
case PluginInterface.DataTypeEnum.Utf8String:
|
2022-01-24 15:10:56 +00:00
|
|
|
|
ret.Value = plc.ReadString(ioarg.Address);
|
2022-01-15 16:16:53 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = $"读取失败,{ex.Message}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
|
|
|
ret.Message = "连接失败";
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2022-04-13 09:01:24 +00:00
|
|
|
|
public async Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg)
|
|
|
|
|
{
|
|
|
|
|
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
|
|
|
|
|
return rpcResponse;
|
|
|
|
|
}
|
2022-01-15 16:16:53 +00:00
|
|
|
|
}
|
|
|
|
|
}
|