PLCNotifier.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using AdsCommunicatorNet8;
  2. using ProtocalGeneral;
  3. namespace MinicsConsole.Connector;
  4. public class PLCNotifier(HardwareAddress address, ILog log) : IMini8DataNotifier, ITcpConnectNority, IDisposable
  5. {
  6. private readonly AdsCommunicator _connector = new(log);
  7. public string Name { get; set; } = "PLC TwinCat Ads";
  8. public bool StartService()
  9. {
  10. if (address.PLCAddress is null)
  11. return false;
  12. if (string.IsNullOrEmpty(address.PLCAddress.IPAddress))
  13. return false;
  14. this._connector.Initialize(this);
  15. this._connector.Open(address.PLCAddress.IPAddress, address.PLCAddress.Port);
  16. return true;
  17. }
  18. public void Dispose()
  19. {
  20. this._connector?.Dispose();
  21. }
  22. void IMini8DataNotifier.ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  23. {
  24. if (this._connector is null)
  25. return;
  26. if (!this._PlcConnected)
  27. return;
  28. if (!address.PLCChannelsAddresses.TryGetSubValue(mini8, channel, out ChannelAddressPLC? channelAddress) || channelAddress is null)
  29. return;
  30. if (string.IsNullOrEmpty(channelAddress.PV))
  31. return;
  32. if (!this._connector.SetValue(channelAddress.PV, channelData.PV))
  33. log.Warning($"PLCNotifier ChannelInfoNotify Send Mini8 - {mini8} channel {channel} PV {channelData.PV} Failed");
  34. }
  35. private bool _PlcConnected = false;
  36. void ITcpConnectNority.Connect(string ip, int port)
  37. {
  38. this._PlcConnected = true;
  39. log.Info($"PLC Connected {ip}:{port}");
  40. }
  41. void ITcpConnectNority.DisConnect(string ip, int port)
  42. {
  43. this._PlcConnected = false;
  44. log.Warning($"PLC Disconnected {ip}:{port}");
  45. }
  46. void IMini8DataNotifier.AlarmNotify(byte mini8, byte channel, float temperature)
  47. {
  48. }
  49. void IMini8DataNotifier.AlarmTcBrockenNotify(byte mini8, byte channel)
  50. {
  51. }
  52. void IMini8DataNotifier.Mini8ConnectNotify(byte mini8, bool connected)
  53. {
  54. }
  55. }