diff --git a/IoTGateway/IoTGateway.csproj b/IoTGateway/IoTGateway.csproj
index 6a24c64..94e53f9 100644
--- a/IoTGateway/IoTGateway.csproj
+++ b/IoTGateway/IoTGateway.csproj
@@ -23,6 +23,8 @@
+
+
diff --git a/IoTGateway/Program.cs b/IoTGateway/Program.cs
index 21d3105..f770fc8 100644
--- a/IoTGateway/Program.cs
+++ b/IoTGateway/Program.cs
@@ -8,6 +8,8 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MQTTnet.AspNetCore.Extensions;
using WalkingTec.Mvvm.Core;
+using NLog;
+using NLog.Web;
namespace IoTGateway
{
@@ -48,7 +50,8 @@ namespace IoTGateway
option.ListenAnyIP(1888, l => l.UseMqtt());
option.ListenAnyIP(518);
});
- });
+ })
+ .UseNLog();
}
}
}
diff --git a/IoTGateway/nlog.config b/IoTGateway/nlog.config
new file mode 100644
index 0000000..0783d6e
--- /dev/null
+++ b/IoTGateway/nlog.config
@@ -0,0 +1,35 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs b/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs
index 0357910..94e322e 100644
--- a/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs
+++ b/Plugins/Drivers/DriverAllenBradley/AllenBradley.cs
@@ -3,6 +3,8 @@ using IoTClient.Enums;
using PluginInterface;
using System;
using System.Text;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
namespace DriverAllenBradley
{
@@ -11,6 +13,9 @@ namespace DriverAllenBradley
public class AllenBradley : IDriver
{
private AllenBradleyClient plc = null;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
@@ -30,9 +35,12 @@ namespace DriverAllenBradley
#endregion
- public AllenBradley(Guid deviceId)
+ public AllenBradley(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
diff --git a/Plugins/Drivers/DriverAllenBradley/DriverAllenBradley.csproj b/Plugins/Drivers/DriverAllenBradley/DriverAllenBradley.csproj
index d2e828e..b460b4d 100644
--- a/Plugins/Drivers/DriverAllenBradley/DriverAllenBradley.csproj
+++ b/Plugins/Drivers/DriverAllenBradley/DriverAllenBradley.csproj
@@ -14,5 +14,6 @@
+
diff --git a/Plugins/Drivers/DriverFanuc/DriverFanuc.csproj b/Plugins/Drivers/DriverFanuc/DriverFanuc.csproj
index 41dcdda..579346d 100644
--- a/Plugins/Drivers/DriverFanuc/DriverFanuc.csproj
+++ b/Plugins/Drivers/DriverFanuc/DriverFanuc.csproj
@@ -9,5 +9,6 @@
+
diff --git a/Plugins/Drivers/DriverFanuc/Fanuc.cs b/Plugins/Drivers/DriverFanuc/Fanuc.cs
index bbe5125..d1c7d8d 100644
--- a/Plugins/Drivers/DriverFanuc/Fanuc.cs
+++ b/Plugins/Drivers/DriverFanuc/Fanuc.cs
@@ -1,4 +1,6 @@
-using PluginInterface;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
+using PluginInterface;
namespace DriverFaunc
{
@@ -20,6 +22,9 @@ namespace DriverFaunc
private ushort _hndl;
private int _result = -1;
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
+
public bool IsConnected
{
get
@@ -39,9 +44,12 @@ namespace DriverFaunc
}
}
- public Fanuc(Guid deviceId)
+ public Fanuc(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
public bool Close()
diff --git a/Plugins/Drivers/DriverFanucHsl/DriverFanucHsl.csproj b/Plugins/Drivers/DriverFanucHsl/DriverFanucHsl.csproj
index cb96cdd..4f81f30 100644
--- a/Plugins/Drivers/DriverFanucHsl/DriverFanucHsl.csproj
+++ b/Plugins/Drivers/DriverFanucHsl/DriverFanucHsl.csproj
@@ -15,5 +15,6 @@
+
diff --git a/Plugins/Drivers/DriverFanucHsl/FanucHsl.cs b/Plugins/Drivers/DriverFanucHsl/FanucHsl.cs
index 1d0ea86..1acab67 100644
--- a/Plugins/Drivers/DriverFanucHsl/FanucHsl.cs
+++ b/Plugins/Drivers/DriverFanucHsl/FanucHsl.cs
@@ -4,6 +4,8 @@ using System;
using System.Text;
using HslCommunication.CNC.Fanuc;
using HslCommunication;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
namespace DriverFanucHsl
{
@@ -12,6 +14,9 @@ namespace DriverFanucHsl
public class FanucHsl : IDriver
{
private FanucSeries0i fanuc;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
@@ -31,7 +36,7 @@ namespace DriverFanucHsl
#endregion
- public FanucHsl(Guid deviceId)
+ public FanucHsl(Device device, ILogger logger)
{
// 授权示例 Authorization example
if (!Authorization.SetAuthorizationCode("输入你的授权号"))
@@ -39,10 +44,13 @@ namespace DriverFanucHsl
//return; // 激活失败应该退出系统
}
- DeviceId = deviceId;
- }
+ _device = device;
+ _logger = logger;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
+ }
+
public bool IsConnected
{
get
diff --git a/Plugins/Drivers/DriverMTConnect/DriverMTConnect.csproj b/Plugins/Drivers/DriverMTConnect/DriverMTConnect.csproj
index 82c2e3e..17cf3d3 100644
--- a/Plugins/Drivers/DriverMTConnect/DriverMTConnect.csproj
+++ b/Plugins/Drivers/DriverMTConnect/DriverMTConnect.csproj
@@ -14,5 +14,6 @@
+
diff --git a/Plugins/Drivers/DriverMTConnect/MTConnectClient.cs b/Plugins/Drivers/DriverMTConnect/MTConnectClient.cs
index 5f51ab0..04516a3 100644
--- a/Plugins/Drivers/DriverMTConnect/MTConnectClient.cs
+++ b/Plugins/Drivers/DriverMTConnect/MTConnectClient.cs
@@ -1,6 +1,9 @@
using PluginInterface;
using System;
+using Microsoft.Extensions.Logging;
using OpenNETCF.MTConnect;
+using Device = IoTGateway.Model.Device;
+
namespace DriverMTConnect
{
internal class MTConnectClient : IDriver
@@ -26,9 +29,15 @@ namespace DriverMTConnect
EntityClient m_client = null;
- public MTConnectClient(Guid deviceId)
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
+
+ public MTConnectClient(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
public bool Close()
diff --git a/Plugins/Drivers/DriverMitsubishi/DriverMitsubishi.csproj b/Plugins/Drivers/DriverMitsubishi/DriverMitsubishi.csproj
index 42df9f2..e88f3f4 100644
--- a/Plugins/Drivers/DriverMitsubishi/DriverMitsubishi.csproj
+++ b/Plugins/Drivers/DriverMitsubishi/DriverMitsubishi.csproj
@@ -14,5 +14,6 @@
+
diff --git a/Plugins/Drivers/DriverMitsubishi/Mitsubishi.cs b/Plugins/Drivers/DriverMitsubishi/Mitsubishi.cs
index 0bb5842..47b0725 100644
--- a/Plugins/Drivers/DriverMitsubishi/Mitsubishi.cs
+++ b/Plugins/Drivers/DriverMitsubishi/Mitsubishi.cs
@@ -3,6 +3,8 @@ using IoTClient.Enums;
using PluginInterface;
using System;
using System.Text;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
namespace DriverMitsubishi
{
@@ -12,6 +14,9 @@ namespace DriverMitsubishi
public class Mitsubishi : IDriver
{
private MitsubishiClient plc = null;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
@@ -34,9 +39,12 @@ namespace DriverMitsubishi
#endregion
- public Mitsubishi(Guid deviceId)
+ public Mitsubishi(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
diff --git a/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj b/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj
index bca6eb3..68b50e8 100644
--- a/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj
+++ b/Plugins/Drivers/DriverModbusMaster/DriverModbusMaster.csproj
@@ -12,6 +12,7 @@
+
diff --git a/Plugins/Drivers/DriverModbusMaster/ModbusDataConver.cs b/Plugins/Drivers/DriverModbusMaster/ModbusDataConver.cs
new file mode 100644
index 0000000..c6a6fe4
--- /dev/null
+++ b/Plugins/Drivers/DriverModbusMaster/ModbusDataConver.cs
@@ -0,0 +1,211 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DriverModbusMaster
+{
+ public class ModbusDataConvert
+ {
+ ///
+ /// 赋值string
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static void SetString(ushort[] src, int start, string value)
+ {
+ byte[] bytesTemp = Encoding.UTF8.GetBytes(value);
+ ushort[] dest = Bytes2Ushorts(bytesTemp);
+ dest.CopyTo(src, start);
+ }
+
+ ///
+ /// 获取string
+ ///
+ ///
+ ///
+ ///
+ ///
+ public static string GetString(ushort[] src, int start, int len)
+ {
+ ushort[] temp = new ushort[len];
+ for (int i = 0; i < len; i++)
+ {
+ temp[i] = src[i + start];
+ }
+ byte[] bytesTemp = Ushorts2Bytes(temp);
+ string res = Encoding.UTF8.GetString(bytesTemp).Trim(new char[] { '\0' });
+ return res;
+ }
+
+ ///
+ /// 赋值Real类型数据
+ ///
+ ///
+ ///
+ ///
+ public static void SetReal(ushort[] src, int start, float value)
+ {
+ byte[] bytes = BitConverter.GetBytes(value);
+
+ ushort[] dest = Bytes2Ushorts(bytes);
+
+ dest.CopyTo(src, start);
+ }
+
+ ///
+ /// 获取float类型数据
+ ///
+ ///
+ ///
+ ///
+ public static float GetReal(ushort[] src, int start)
+ {
+ ushort[] temp = new ushort[2];
+ for (int i = 0; i < 2; i++)
+ {
+ temp[i] = src[i + start];
+ }
+ byte[] bytesTemp = Ushorts2Bytes(temp);
+ float res = BitConverter.ToSingle(bytesTemp, 0);
+ return res;
+ }
+
+ ///
+ /// 赋值Short类型数据
+ ///
+ ///
+ ///
+ ///
+ public static void SetShort(ushort[] src, int start, short value)
+ {
+ byte[] bytes = BitConverter.GetBytes(value);
+
+ ushort[] dest = Bytes2Ushorts(bytes);
+
+ dest.CopyTo(src, start);
+ }
+
+ ///
+ /// 获取short类型数据
+ ///
+ ///
+ ///
+ ///
+ public static short GetShort(ushort[] src, int start)
+ {
+ ushort[] temp = new ushort[1];
+ temp[0] = src[start];
+ byte[] bytesTemp = Ushorts2Bytes(temp);
+ short res = BitConverter.ToInt16(bytesTemp, 0);
+ return res;
+ }
+
+
+ public static bool[] GetBools(ushort[] src, int start, int num)
+ {
+ ushort[] temp = new ushort[num];
+ for (int i = start; i < start + num; i++)
+ {
+ temp[i] = src[i + start];
+ }
+ byte[] bytes = Ushorts2Bytes(temp);
+
+ bool[] res = Bytes2Bools(bytes);
+
+ return res;
+ }
+
+ private static bool[] Bytes2Bools(byte[] b)
+ {
+ bool[] array = new bool[8 * b.Length];
+
+ for (int i = 0; i < b.Length; i++)
+ {
+ for (int j = 0; j < 8; j++)
+ {
+ array[i * 8 + j] = (b[i] & 1) == 1;//判定byte的最后一位是否为1,若为1,则是true;否则是false
+ b[i] = (byte)(b[i] >> 1);//将byte右移一位
+ }
+ }
+ return array;
+ }
+
+ private static byte Bools2Byte(bool[] array)
+ {
+ if (array != null && array.Length > 0)
+ {
+ byte b = 0;
+ for (int i = 0; i < 8; i++)
+ {
+ if (array[i])
+ {
+ byte nn = (byte)(1 << i);//左移一位,相当于×2
+ b += nn;
+ }
+ }
+ return b;
+ }
+ return 0;
+ }
+
+ private static ushort[] Bytes2Ushorts(byte[] src, bool reverse = false)
+ {
+ int len = src.Length;
+
+ byte[] srcPlus = new byte[len + 1];
+ src.CopyTo(srcPlus, 0);
+ int count = len >> 1;
+
+ if (len % 2 != 0)
+ {
+ count += 1;
+ }
+
+ ushort[] dest = new ushort[count];
+ if (reverse)
+ {
+ for (int i = 0; i < count; i++)
+ {
+ dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
+ }
+ }
+ else
+ {
+ for (int i = 0; i < count; i++)
+ {
+ dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
+ }
+ }
+
+ return dest;
+ }
+
+ private static byte[] Ushorts2Bytes(ushort[] src, bool reverse = false)
+ {
+
+ int count = src.Length;
+ byte[] dest = new byte[count << 1];
+ if (reverse)
+ {
+ for (int i = 0; i < count; i++)
+ {
+ dest[i * 2] = (byte)(src[i] >> 8);
+ dest[i * 2 + 1] = (byte)(src[i] >> 0);
+ }
+ }
+ else
+ {
+ for (int i = 0; i < count; i++)
+ {
+ dest[i * 2] = (byte)(src[i] >> 0);
+ dest[i * 2 + 1] = (byte)(src[i] >> 8);
+ }
+ }
+ return dest;
+ }
+ }
+}
diff --git a/Plugins/Drivers/DriverModbusMaster/ModbusMaster.cs b/Plugins/Drivers/DriverModbusMaster/ModbusMaster.cs
index 8867ce6..60ae502 100644
--- a/Plugins/Drivers/DriverModbusMaster/ModbusMaster.cs
+++ b/Plugins/Drivers/DriverModbusMaster/ModbusMaster.cs
@@ -1,10 +1,9 @@
-using Modbus.Device;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
+using Modbus.Device;
using Modbus.Serial;
using PluginInterface;
-using System;
-using System.Collections.Generic;
using System.IO.Ports;
-using System.Net;
using System.Net.Sockets;
using System.Text;
@@ -14,21 +13,24 @@ namespace DriverModbusMaster
[DriverSupported("ModbusUDP")]
[DriverSupported("ModbusRtu")]
[DriverSupported("ModbusAscii")]
- [DriverInfoAttribute("ModbusMaster", "V1.0.0", "Copyright IoTGateway© 2021-12-19")]
+ [DriverInfoAttribute("ModbusMaster", "V1.1.0", "Copyright IoTGateway© 2022-8-6")]
public class ModbusMaster : IDriver
{
- private TcpClient clientTcp = null;
- private UdpClient clientUdp = null;
- private SerialPort port = null;
- private Modbus.Device.ModbusMaster master = null;
- private SerialPortAdapter adapter = null;
+ private TcpClient? _tcpClient;
+ private UdpClient? _udpClient;
+ private SerialPort? _serialPort;
+ private Modbus.Device.ModbusMaster? _master;
+ private SerialPortAdapter? _adapter;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
public Guid DeviceId { get; set; }
[ConfigParameter("PLC类型")]
- public PLC_TYPE PLCType { get; set; } = PLC_TYPE.S71200;
+ public PLC_TYPE PlcType { get; set; } = PLC_TYPE.S71200;
[ConfigParameter("主站类型")]
public Master_TYPE Master_TYPE { get; set; } = Master_TYPE.Tcp;
@@ -65,11 +67,13 @@ namespace DriverModbusMaster
#endregion
- public ModbusMaster(Guid deviceId)
+ public ModbusMaster(Device device, ILogger logger)
{
- DeviceId = deviceId;
- }
+ _device = device;
+ _logger = logger;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
+ }
public bool IsConnected
{
@@ -80,14 +84,14 @@ namespace DriverModbusMaster
case Master_TYPE.Tcp:
case Master_TYPE.RtuOnTcp:
case Master_TYPE.AsciiOnTcp:
- return clientTcp != null && master != null && clientTcp.Connected;
+ return _tcpClient != null && _master != null && _tcpClient.Connected;
case Master_TYPE.Udp:
case Master_TYPE.RtuOnUdp:
case Master_TYPE.AsciiOnUdp:
- return clientUdp != null && master != null && clientUdp.Client.Connected;
+ return _udpClient != null && _master != null && _udpClient.Client.Connected;
case Master_TYPE.Rtu:
case Master_TYPE.Ascii:
- return port != null && master != null && port.IsOpen;
+ return _serialPort != null && _master != null && _serialPort.IsOpen;
default:
return false;
}
@@ -98,68 +102,68 @@ namespace DriverModbusMaster
{
try
{
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Connect()");
switch (Master_TYPE)
{
case Master_TYPE.Tcp:
- clientTcp = new TcpClient(IpAddress.ToString(), Port);
- clientTcp.ReceiveTimeout = Timeout;
- clientTcp.SendTimeout = Timeout;
- master = ModbusIpMaster.CreateIp(clientTcp);
+ _tcpClient = new TcpClient(IpAddress, Port);
+ _tcpClient.ReceiveTimeout = Timeout;
+ _tcpClient.SendTimeout = Timeout;
+ _master = ModbusIpMaster.CreateIp(_tcpClient);
break;
case Master_TYPE.Udp:
- clientUdp = new UdpClient(IpAddress.ToString(), Port);
- clientUdp.Client.ReceiveTimeout = Timeout;
- clientUdp.Client.SendTimeout = Timeout;
- master = ModbusIpMaster.CreateIp(clientUdp);
+ _udpClient = new UdpClient(IpAddress, Port);
+ _udpClient.Client.ReceiveTimeout = Timeout;
+ _udpClient.Client.SendTimeout = Timeout;
+ _master = ModbusIpMaster.CreateIp(_udpClient);
break;
case Master_TYPE.Rtu:
- port = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
- port.ReadTimeout = Timeout;
- port.WriteTimeout = Timeout;
- port.Open();
- adapter = new SerialPortAdapter(port);
- master = ModbusSerialMaster.CreateRtu(adapter);
+ _serialPort = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
+ _serialPort.ReadTimeout = Timeout;
+ _serialPort.WriteTimeout = Timeout;
+ _serialPort.Open();
+ _adapter = new SerialPortAdapter(_serialPort);
+ _master = ModbusSerialMaster.CreateRtu(_adapter);
break;
case Master_TYPE.RtuOnTcp:
- clientTcp = new TcpClient(IpAddress.ToString(), Port);
- clientTcp.ReceiveTimeout = Timeout;
- clientTcp.SendTimeout = Timeout;
- master = ModbusSerialMaster.CreateRtu(clientTcp);
+ _tcpClient = new TcpClient(IpAddress, Port);
+ _tcpClient.ReceiveTimeout = Timeout;
+ _tcpClient.SendTimeout = Timeout;
+ _master = ModbusSerialMaster.CreateRtu(_tcpClient);
break;
case Master_TYPE.RtuOnUdp:
- clientUdp = new UdpClient(IpAddress.ToString(), Port);
- clientUdp.Client.ReceiveTimeout = Timeout;
- clientUdp.Client.SendTimeout = Timeout;
- master = ModbusSerialMaster.CreateRtu(clientUdp);
+ _udpClient = new UdpClient(IpAddress, Port);
+ _udpClient.Client.ReceiveTimeout = Timeout;
+ _udpClient.Client.SendTimeout = Timeout;
+ _master = ModbusSerialMaster.CreateRtu(_udpClient);
break;
case Master_TYPE.Ascii:
- port = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
- port.ReadTimeout = Timeout;
- port.WriteTimeout = Timeout;
- port.Open();
- adapter = new SerialPortAdapter(port);
- master = ModbusSerialMaster.CreateAscii(adapter);
+ _serialPort = new SerialPort(PortName, BaudRate, Parity, DataBits, StopBits);
+ _serialPort.ReadTimeout = Timeout;
+ _serialPort.WriteTimeout = Timeout;
+ _serialPort.Open();
+ _adapter = new SerialPortAdapter(_serialPort);
+ _master = ModbusSerialMaster.CreateAscii(_adapter);
break;
case Master_TYPE.AsciiOnTcp:
- clientTcp = new TcpClient(IpAddress.ToString(), Port);
- clientTcp.ReceiveTimeout = Timeout;
- clientTcp.SendTimeout = Timeout;
- master = ModbusSerialMaster.CreateAscii(clientTcp);
+ _tcpClient = new TcpClient(IpAddress, Port);
+ _tcpClient.ReceiveTimeout = Timeout;
+ _tcpClient.SendTimeout = Timeout;
+ _master = ModbusSerialMaster.CreateAscii(_tcpClient);
break;
case Master_TYPE.AsciiOnUdp:
- clientUdp = new UdpClient(IpAddress.ToString(), Port);
- clientUdp.Client.ReceiveTimeout = Timeout;
- clientUdp.Client.SendTimeout = Timeout;
- master = ModbusSerialMaster.CreateAscii(clientUdp);
- break;
- default:
+ _udpClient = new UdpClient(IpAddress, Port);
+ _udpClient.Client.ReceiveTimeout = Timeout;
+ _udpClient.Client.SendTimeout = Timeout;
+ _master = ModbusSerialMaster.CreateAscii(_udpClient);
break;
}
- master.Transport.ReadTimeout = Timeout;
- master.Transport.WriteTimeout = Timeout;
+ _master.Transport.ReadTimeout = Timeout;
+ _master.Transport.WriteTimeout = Timeout;
}
catch (Exception ex)
{
+ _logger.LogError($"Device:[{_device.DeviceName}],Connect(),Error", ex);
return false;
}
return IsConnected;
@@ -169,14 +173,15 @@ namespace DriverModbusMaster
{
try
{
- clientTcp?.Close();
- clientUdp?.Close();
- port?.Close();
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Close()");
+ _tcpClient?.Close();
+ _udpClient?.Close();
+ _serialPort?.Close();
return !IsConnected;
}
- catch (Exception)
+ catch (Exception ex)
{
-
+ _logger.LogError($"Device:[{_device.DeviceName}],Close(),Error", ex);
return false;
}
}
@@ -185,14 +190,15 @@ namespace DriverModbusMaster
{
try
{
- clientTcp?.Dispose();
- clientUdp?.Dispose();
- port?.Dispose();
- master?.Dispose();
+ _tcpClient?.Dispose();
+ _udpClient?.Dispose();
+ _serialPort?.Dispose();
+ _master?.Dispose();
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Dispose()");
}
- catch (Exception)
+ catch (Exception ex)
{
-
+ _logger.LogError($"Device:[{_device.DeviceName}],Dispose(),Error", ex);
}
}
@@ -214,6 +220,7 @@ namespace DriverModbusMaster
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],ReadHoldingRegisters(),Error", ex);
}
return ret;
@@ -238,6 +245,7 @@ namespace DriverModbusMaster
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],ReadInputRegisters(),Error", ex);
}
return ret;
@@ -252,7 +260,7 @@ namespace DriverModbusMaster
{
if (IsConnected)
{
- var retBool = master.ReadCoils(SlaveAddress, ushort.Parse(ioarg.Address), 1)[0];
+ var retBool = _master.ReadCoils(SlaveAddress, ushort.Parse(ioarg.Address), 1)[0];
if (ioarg.ValueType == DataTypeEnum.Bit)
{
if (retBool)
@@ -274,6 +282,7 @@ namespace DriverModbusMaster
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],ReadCoil(),Error", ex);
}
return ret;
@@ -287,7 +296,7 @@ namespace DriverModbusMaster
{
if (IsConnected)
{
- var retBool = master.ReadInputs(SlaveAddress, ushort.Parse(ioarg.Address), 1)[0];
+ var retBool = _master.ReadInputs(SlaveAddress, ushort.Parse(ioarg.Address), 1)[0];
if (ioarg.ValueType == DataTypeEnum.Bit)
{
if (retBool)
@@ -309,6 +318,7 @@ namespace DriverModbusMaster
{
ret.StatusType = VaribaleStatusTypeEnum.UnKnow;
ret.Message = ex.Message;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],ReadInput(),Error", ex);
}
return ret;
@@ -336,16 +346,16 @@ namespace DriverModbusMaster
else
{
ushort startAddress, count;
- ret = AnalyseAddress(ioarg, out startAddress, out count);
- if(ret.StatusType!= VaribaleStatusTypeEnum.Good)
+ ret = AnalyzeAddress(ioarg, out startAddress, out count);
+ if (ret.StatusType != VaribaleStatusTypeEnum.Good)
return ret;
try
{
var rawBuffers = new ushort[] { };
if (FunCode == 3)
- rawBuffers = master.ReadHoldingRegisters(SlaveAddress, startAddress, count);
+ rawBuffers = _master.ReadHoldingRegisters(SlaveAddress, startAddress, count);
else if (FunCode == 4)
- rawBuffers = master.ReadInputRegisters(SlaveAddress, startAddress, count);
+ rawBuffers = _master.ReadInputRegisters(SlaveAddress, startAddress, count);
var retBuffers = ChangeBuffersOrder(rawBuffers, ioarg.ValueType);
if (ioarg.ValueType == DataTypeEnum.AsciiString)
@@ -356,9 +366,9 @@ namespace DriverModbusMaster
else if (ioarg.ValueType.ToString().Contains("Int16"))
ret.Value = (short)retBuffers[0];
else if (ioarg.ValueType.ToString().Contains("Uint32"))
- ret.Value = (UInt32)(retBuffers[0] << 16) + retBuffers[1];
+ ret.Value = (uint)(retBuffers[0] << 16) + retBuffers[1];
else if (ioarg.ValueType.ToString().Contains("Int32"))
- ret.Value = (Int32)(retBuffers[0] << 16) + retBuffers[1];
+ ret.Value = (retBuffers[0] << 16) + retBuffers[1];
else if (ioarg.ValueType.ToString().Contains("Float"))
{
var bytes = new byte[] { (byte)(retBuffers[1] & 0xff), (byte)((retBuffers[1] >> 8) & 0xff), (byte)(retBuffers[0] & 0xff), (byte)((retBuffers[0] >> 8) & 0xff) };
@@ -366,7 +376,7 @@ namespace DriverModbusMaster
}
else if (ioarg.ValueType.ToString().Contains("AsciiString"))
{
- var str= Encoding.ASCII.GetString(GetBytes(retBuffers).ToArray());
+ var str = Encoding.ASCII.GetString(GetBytes(retBuffers).ToArray());
if (str.Contains('\0'))
str = str.Split('\0')[0];
ret.Value = str;
@@ -377,6 +387,7 @@ namespace DriverModbusMaster
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = ex.Message;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],ReadRegistersBuffers(),Error", ex);
}
}
@@ -387,10 +398,9 @@ namespace DriverModbusMaster
{
if (dataType.ToString().Contains("32") || dataType.ToString().Contains("Float"))
return 2;
- else if (dataType.ToString().Contains("64") || dataType.ToString().Contains("Double"))
+ if (dataType.ToString().Contains("64") || dataType.ToString().Contains("Double"))
return 4;
- else
- return 1;
+ return 1;
}
//预留了大小端转换的
@@ -454,18 +464,16 @@ namespace DriverModbusMaster
private List GetBytes(ushort[] retBuffers)
{
-
List vs = new();
- for (int i = 0; i < retBuffers.Length; i++)
+ foreach (var retBuffer in retBuffers)
{
- vs.Add((byte)(retBuffers[i] & 0xFF));
- vs.Add((byte)((retBuffers[i] & 0xFF00) >> 8));
+ vs.Add((byte)(retBuffer & 0xFF));
+ vs.Add((byte)((retBuffer & 0xFF00) >> 8));
}
-
return vs;
}
- private DriverReturnValueModel AnalyseAddress(DriverAddressIoArgModel ioarg, out ushort StartAddress, out ushort ReadCount)
+ private DriverReturnValueModel AnalyzeAddress(DriverAddressIoArgModel ioarg, out ushort StartAddress, out ushort ReadCount)
{
DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good };
try
@@ -489,46 +497,70 @@ namespace DriverModbusMaster
ret.Message = ex.Message;
StartAddress = 0;
ReadCount = 0;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],AnalyzeAddress(),Error", ex);
return ret;
}
}
- public async Task WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg)
+ public async Task WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg)
{
RpcResponse rpcResponse = new() { IsSuccess = false };
try
{
- ushort address = ushort.Parse(Ioarg.Address);
if (!IsConnected)
rpcResponse.Description = "设备连接已断开";
else
{
+ DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good };
+ ushort address, count;
+ ret = AnalyzeAddress(ioarg, out address, out count);
+
//功能码01
- if (Method == nameof(ReadCoil))
+ if (method == nameof(ReadCoil))
{
- bool value = Ioarg.Value.ToString() == "1" || Ioarg.Value.ToString().ToLower() == "true";
- master.WriteSingleCoilAsync(SlaveAddress, address, value);
+ var value = ioarg.Value.ToString() == "1" || ioarg.Value.ToString().ToLower() == "true";
+ await _master.WriteSingleCoilAsync(SlaveAddress, address, value);
rpcResponse.IsSuccess = true;
return rpcResponse;
}
//功能码03
- else if (Method == nameof(ReadHoldingRegisters))
+ if (method == nameof(ReadHoldingRegisters))
{
- master.WriteSingleRegisterAsync(SlaveAddress, address, ushort.Parse(Ioarg.Value.ToString()));
+ ushort[] shortArray = new ushort[count];
+
+ switch (ioarg.ValueType)
+ {
+ case DataTypeEnum.AsciiString:
+ ModbusDataConvert.SetString(shortArray, 0, ioarg.Value.ToString());
+ await _master.WriteMultipleRegistersAsync(SlaveAddress, address, shortArray);
+
+ break;
+ case DataTypeEnum.Float:
+ float f = 0;
+ float.TryParse(ioarg.Value.ToString(), out f);
+ ModbusDataConvert.SetReal(shortArray, 0, f);
+ await _master.WriteMultipleRegistersAsync(SlaveAddress, address, shortArray);
+
+ break;
+ default:
+ await _master.WriteSingleRegisterAsync(SlaveAddress, address, ushort.Parse(ioarg.Value.ToString()));
+ break;
+ }
rpcResponse.IsSuccess = true;
return rpcResponse;
}
- else
- rpcResponse.Description = $"不支持写入:{Method}";
+ rpcResponse.Description = $"不支持写入:{method}";
}
}
catch (Exception ex)
{
- rpcResponse.Description = $"写入失败,[Method]:{Method},[Ioarg]:{Ioarg},[ex]:{ex}";
+ rpcResponse.Description = $"写入失败,[Method]:{method},[Ioarg]:{ioarg},[ex]:{ex}";
+ _logger.LogInformation($"Device:[{_device.DeviceName}],WriteAsync(),Error", ex);
}
return rpcResponse;
}
}
+
public enum PLC_TYPE
{
S7200 = 0,
diff --git a/Plugins/Drivers/DriverOPCDaClient/DriverOPCDaClient.csproj b/Plugins/Drivers/DriverOPCDaClient/DriverOPCDaClient.csproj
index ed5f48e..dd205ed 100644
--- a/Plugins/Drivers/DriverOPCDaClient/DriverOPCDaClient.csproj
+++ b/Plugins/Drivers/DriverOPCDaClient/DriverOPCDaClient.csproj
@@ -10,6 +10,7 @@
+
diff --git a/Plugins/Drivers/DriverOPCDaClient/OPCDaClient.cs b/Plugins/Drivers/DriverOPCDaClient/OPCDaClient.cs
index 43622b7..3cd6b1c 100644
--- a/Plugins/Drivers/DriverOPCDaClient/OPCDaClient.cs
+++ b/Plugins/Drivers/DriverOPCDaClient/OPCDaClient.cs
@@ -5,6 +5,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automation.OPCClient;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
namespace DriverOPCDaClient
{
@@ -12,6 +14,9 @@ namespace DriverOPCDaClient
{
OPCClientWrapper opcDaClient = null;
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
+
#region 配置参数
[ConfigParameter("设备Id")] public Guid DeviceId { get; set; }
@@ -28,10 +33,12 @@ namespace DriverOPCDaClient
#endregion
- public OPCDaClient(Guid deviceId)
+ public OPCDaClient(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
diff --git a/Plugins/Drivers/DriverOPCUaClient/DriverOPCUaClient.csproj b/Plugins/Drivers/DriverOPCUaClient/DriverOPCUaClient.csproj
index 037b055..af13809 100644
--- a/Plugins/Drivers/DriverOPCUaClient/DriverOPCUaClient.csproj
+++ b/Plugins/Drivers/DriverOPCUaClient/DriverOPCUaClient.csproj
@@ -14,5 +14,6 @@
+
diff --git a/Plugins/Drivers/DriverOPCUaClient/OPCUaClient.cs b/Plugins/Drivers/DriverOPCUaClient/OPCUaClient.cs
index ef88dd4..43ac181 100644
--- a/Plugins/Drivers/DriverOPCUaClient/OPCUaClient.cs
+++ b/Plugins/Drivers/DriverOPCUaClient/OPCUaClient.cs
@@ -6,6 +6,8 @@ using Opc.Ua;
using Opc.Ua.Client;
using System.Collections.Generic;
using System.Threading.Tasks;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
using Opc.Ua.Configuration;
using OpcUaHelper;
@@ -16,6 +18,9 @@ namespace DriverOPCUaClient
public class OPCUaClient : IDriver
{
OpcUaClientHelper opcUaClient = null;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
@@ -32,10 +37,12 @@ namespace DriverOPCUaClient
#endregion
- public OPCUaClient(Guid deviceId)
+ public OPCUaClient(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
diff --git a/Plugins/Drivers/DriverOmronFins/DriverOmronFins.csproj b/Plugins/Drivers/DriverOmronFins/DriverOmronFins.csproj
index 42df9f2..e88f3f4 100644
--- a/Plugins/Drivers/DriverOmronFins/DriverOmronFins.csproj
+++ b/Plugins/Drivers/DriverOmronFins/DriverOmronFins.csproj
@@ -14,5 +14,6 @@
+
diff --git a/Plugins/Drivers/DriverOmronFins/OmronFins.cs b/Plugins/Drivers/DriverOmronFins/OmronFins.cs
index 0b97f7d..ea78abc 100644
--- a/Plugins/Drivers/DriverOmronFins/OmronFins.cs
+++ b/Plugins/Drivers/DriverOmronFins/OmronFins.cs
@@ -3,6 +3,8 @@ using IoTClient.Enums;
using PluginInterface;
using System;
using System.Text;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
namespace DriverOmronFins
{
@@ -11,6 +13,9 @@ namespace DriverOmronFins
public class OmronFins : IDriver
{
private OmronFinsClient plc = null;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
@@ -30,9 +35,12 @@ namespace DriverOmronFins
#endregion
- public OmronFins(Guid deviceId)
+ public OmronFins(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
diff --git a/Plugins/Drivers/DriverSiemensS7/DriverSiemensS7.csproj b/Plugins/Drivers/DriverSiemensS7/DriverSiemensS7.csproj
index 2617eb5..80504e5 100644
--- a/Plugins/Drivers/DriverSiemensS7/DriverSiemensS7.csproj
+++ b/Plugins/Drivers/DriverSiemensS7/DriverSiemensS7.csproj
@@ -9,5 +9,6 @@
+
diff --git a/Plugins/Drivers/DriverSiemensS7/SiemensS7.cs b/Plugins/Drivers/DriverSiemensS7/SiemensS7.cs
index 288cf8b..085f471 100644
--- a/Plugins/Drivers/DriverSiemensS7/SiemensS7.cs
+++ b/Plugins/Drivers/DriverSiemensS7/SiemensS7.cs
@@ -2,6 +2,8 @@
using S7.Net;
using System;
using System.Text;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
namespace DriverSiemensS7
{
@@ -15,6 +17,9 @@ namespace DriverSiemensS7
public class SiemensS7 : IDriver
{
private Plc plc = null;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
@@ -43,9 +48,12 @@ namespace DriverSiemensS7
#endregion
- public SiemensS7(Guid deviceId)
+ public SiemensS7(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
diff --git a/Plugins/Drivers/DriverSimTcpClient/DriverSimTcpClient.csproj b/Plugins/Drivers/DriverSimTcpClient/DriverSimTcpClient.csproj
index c517682..89ee96d 100644
--- a/Plugins/Drivers/DriverSimTcpClient/DriverSimTcpClient.csproj
+++ b/Plugins/Drivers/DriverSimTcpClient/DriverSimTcpClient.csproj
@@ -11,5 +11,6 @@
+
\ No newline at end of file
diff --git a/Plugins/Drivers/DriverSimTcpClient/SimTcpClient.cs b/Plugins/Drivers/DriverSimTcpClient/SimTcpClient.cs
index 892ed7d..6d67fcc 100644
--- a/Plugins/Drivers/DriverSimTcpClient/SimTcpClient.cs
+++ b/Plugins/Drivers/DriverSimTcpClient/SimTcpClient.cs
@@ -2,6 +2,8 @@
using SimpleTCP;
using System;
using System.Text;
+using IoTGateway.Model;
+using Microsoft.Extensions.Logging;
namespace DriverSimTcpClient
{
@@ -17,6 +19,9 @@ namespace DriverSimTcpClient
/// 缓存最新的服务器返回的原始数据
///
private byte[] latestRcvData;
+
+ public ILogger _logger { get; set; }
+ private readonly Device _device;
#region 配置参数
[ConfigParameter("设备Id")]
@@ -42,9 +47,12 @@ namespace DriverSimTcpClient
#endregion
- public SimTcpClient(Guid deviceId)
+ public SimTcpClient(Device device, ILogger logger)
{
- DeviceId = deviceId;
+ _device = device;
+ _logger = logger;
+
+ _logger.LogInformation($"Device:[{_device.DeviceName}],Create()");
}
diff --git a/Plugins/Plugin/DeviceService.cs b/Plugins/Plugin/DeviceService.cs
index c9ea792..3df256b 100644
--- a/Plugins/Plugin/DeviceService.cs
+++ b/Plugins/Plugin/DeviceService.cs
@@ -3,6 +3,7 @@ using Microsoft.Extensions.Configuration;
using PluginInterface;
using System.Net;
using System.Reflection;
+using System.Runtime.CompilerServices;
using WalkingTec.Mvvm.Core;
using IoTGateway.DataAccess;
using IoTGateway.Model;
@@ -83,8 +84,9 @@ namespace Plugin
else
{
var settings = DC.Set().Where(x => x.DeviceId == Device.ID).AsNoTracking().ToList();
- Type[] types = new Type[1] { typeof(Guid) };
- object[] param = new object[1] { Device.ID };
+
+ Type[] types = new Type[2] { typeof(Device) ,typeof(ILogger) };
+ object[] param = new object[2] { Device , _logger };
ConstructorInfo constructor = driver.Type.GetConstructor(types);
var DeviceObj = constructor.Invoke(param) as IDriver;
diff --git a/Plugins/PluginInterface/IDriver.cs b/Plugins/PluginInterface/IDriver.cs
index 8a5baad..5a8beac 100644
--- a/Plugins/PluginInterface/IDriver.cs
+++ b/Plugins/PluginInterface/IDriver.cs
@@ -1,4 +1,5 @@
using System;
+using Microsoft.Extensions.Logging;
namespace PluginInterface
{
@@ -8,6 +9,10 @@ namespace PluginInterface
public bool IsConnected { get; }
public int Timeout { get; }
public uint MinPeriod { get; }
+
+
+ public ILogger _logger { get; set; }
+
public bool Connect();
public bool Close();
//标准数据读取
diff --git a/Plugins/PluginInterface/PluginInterface.csproj b/Plugins/PluginInterface/PluginInterface.csproj
index 4079917..c042b3a 100644
--- a/Plugins/PluginInterface/PluginInterface.csproj
+++ b/Plugins/PluginInterface/PluginInterface.csproj
@@ -8,6 +8,7 @@
+
diff --git a/README.md b/README.md
index 18da284..415c13d 100644
--- a/README.md
+++ b/README.md
@@ -20,7 +20,7 @@
>
## 交流
-| 公众号:工业物联网网关 | [QQ群:712105424](https://qm.qq.com/cgi-bin/qm/qr?k=e3Y8biyVdhDxx3LPbjvNY3TSNOEAmjp7&jump_from=webapi) |
+| 公众号:工业物联网网关 | 微信群 |
| ------ | ---- |
|  |  |
diff --git a/images/qq.png b/images/qq.png
index 964ae65..fad4598 100644
Binary files a/images/qq.png and b/images/qq.png differ