Explorar el Código

Create ADS PLC Communicator

Zixuan hace 1 semana
padre
commit
1a0751d5b9

+ 5 - 5
CommunicationProtocols/AdsCommunicatorNet8/AdsCommunicator.cs

@@ -7,7 +7,7 @@ using TwinCAT.TypeSystem;
 namespace AdsCommunicatorNet8;
 
 //this Implementation has removed notification function, only get value and set value available
-public class AdsCommunicator(ILog log):IDisposable
+public class AdsCommunicator(ILog log) : IDisposable
 {
     private bool _isConnected;
     private string? _netID = null;
@@ -36,7 +36,7 @@ public class AdsCommunicator(ILog log):IDisposable
         return true;
     }
 
-    public bool Open(string netID, int port, int retry)
+    public bool Open(string netID, int port, int retry=10)
     {
         log?.Info($"Try Open {netID} {port}");
         if (this._adsClient is null)
@@ -96,7 +96,7 @@ public class AdsCommunicator(ILog log):IDisposable
 
         for (; ; )
         {
-            log?    .Info($"Try ReConnected");
+            log?.Info($"Try ReConnected");
 
             try
             {
@@ -127,7 +127,7 @@ public class AdsCommunicator(ILog log):IDisposable
 
         if (this._adsClient is null)
         {
-            log?    .Error("PLC not connected");
+            log?.Error("PLC not connected");
             return false;
         }
 
@@ -284,7 +284,7 @@ public class AdsCommunicator(ILog log):IDisposable
         }
     }
 
-     void IDisposable.Dispose()
+    public void Dispose()
     {
         Dispose(disposing: true);
         GC.SuppressFinalize(this);

+ 8 - 26
MinicsConsole/Connector/PLCNotifier.cs

@@ -1,12 +1,12 @@
-using FinsTcp;
+using AdsCommunicatorNet8;
 using ProtocalGeneral;
 
 namespace MinicsConsole.Connector;
 
 public class PLCNotifier(HardwareAddress address, ILog log) : IMini8DataNotifier, ITcpConnectNority, IDisposable
 {
-    private readonly Fins_Tcp _fins = new();
-    public string Name { get; set; } = "PLC Fins Tcp";
+    private readonly AdsCommunicator _connector = new(log);
+    public string Name { get; set; } = "PLC TwinCat Ads";
 
     public bool StartService()
     {
@@ -15,8 +15,8 @@ public class PLCNotifier(HardwareAddress address, ILog log) : IMini8DataNotifier
         if (string.IsNullOrEmpty(address.PLCAddress.IPAddress))
             return false;
 
-        this._fins.Initialize(this);
-        this._fins.Connect(address.PLCAddress.IPAddress, address.PLCAddress.Port, PlcHeartBeatCallBack);
+        this._connector.Initialize(this);
+        this._connector.Open(address.PLCAddress.IPAddress, address.PLCAddress.Port);
 
         return true;
     }
@@ -24,30 +24,12 @@ public class PLCNotifier(HardwareAddress address, ILog log) : IMini8DataNotifier
 
     public void Dispose()
     {
-        this._fins?.Dispose();
-    }
-
-    private bool PlcHeartBeatCallBack()
-    {
-        if (address.PLCAddress is null)
-            return true;
-
-        if (string.IsNullOrEmpty(address.PLCAddress.HeartBeatAddress))
-            return true;
-
-        try
-        {
-            return _fins.SetData<bool>(address.PLCAddress.HeartBeatAddress, true);
-        }
-        catch
-        {
-            return false;
-        }
+        this._connector?.Dispose();
     }
 
     void IMini8DataNotifier.ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
     {
-        if (this._fins is null)
+        if (this._connector is null)
             return;
 
         if (!this._PlcConnected)
@@ -60,7 +42,7 @@ public class PLCNotifier(HardwareAddress address, ILog log) : IMini8DataNotifier
             return;
 
 
-        if (!this._fins.SetData(channelAddress.PV, channelData.PV))
+        if (!this._connector.SetValue(channelAddress.PV, channelData.PV))
             log.Warning($"PLCNotifier ChannelInfoNotify Send Mini8 - {mini8} channel {channel} PV {channelData.PV} Failed");
     }
     private bool _PlcConnected = false;

+ 1 - 0
MinicsConsole/MinicsConsole.csproj

@@ -23,6 +23,7 @@
 	</ItemGroup>
 
 	<ItemGroup>
+	  <ProjectReference Include="..\CommunicationProtocols\AdsCommunicatorNet8\AdsCommunicatorNet8.csproj" />
 	  <ProjectReference Include="..\CommunicationProtocols\FinsTcp\FinsTcp.csproj" />
 	  <ProjectReference Include="..\Communicators\Mini8Communicator\IMini8Communicator\Mini8Communicator.csproj" />
 	  <ProjectReference Include="..\Communicators\Mini8Communicator\Mini8CommunicatorModbus\Mini8CommunicatorModbus.csproj" />