PLCNotifier.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using FinsTcp;
  2. using ProtocalGeneral;
  3. namespace MinicsConsole.Connector;
  4. public class PLCNotifier(HardwareAddress address, ILog log) : IMini8DataNotifier, ITcpConnectNority, IDisposable
  5. {
  6. private readonly Fins_Tcp _fins = new();
  7. public string Name { get; set; } = "PLC Fins Tcp";
  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._fins.Initialize(this);
  15. this._fins.Connect(address.PLCAddress.IPAddress, address.PLCAddress.Port, PlcHeartBeatCallBack);
  16. return true;
  17. }
  18. public void Dispose()
  19. {
  20. this._fins?.Dispose();
  21. }
  22. private bool PlcHeartBeatCallBack()
  23. {
  24. if (address.PLCAddress is null)
  25. return true;
  26. if (string.IsNullOrEmpty(address.PLCAddress.HeartBeatAddress))
  27. return true;
  28. try
  29. {
  30. return _fins.SetData<bool>(address.PLCAddress.HeartBeatAddress, true);
  31. }
  32. catch
  33. {
  34. return false;
  35. }
  36. }
  37. void IMini8DataNotifier.ChannelInfoNotify(byte mini8, byte channel, ChannelData channelData)
  38. {
  39. if (this._fins is null)
  40. return;
  41. if (!this._PlcConnected)
  42. return;
  43. if (!address.PLCChannelsAddresses.TryGetSubValue(mini8, channel, out ChannelAddressPLC? channelAddress) || channelAddress is null)
  44. return;
  45. if (string.IsNullOrEmpty(channelAddress.PV))
  46. return;
  47. if (!this._fins.SetData(channelAddress.PV, channelData.PV))
  48. log.Warning($"PLCNotifier ChannelInfoNotify Send Mini8 - {mini8} channel {channel} PV {channelData.PV} Failed");
  49. }
  50. private bool _PlcConnected = false;
  51. void ITcpConnectNority.Connect(string ip, int port)
  52. {
  53. this._PlcConnected = true;
  54. log.Info($"PLC Connected {ip}:{port}");
  55. }
  56. void ITcpConnectNority.DisConnect(string ip, int port)
  57. {
  58. this._PlcConnected = false;
  59. log.Warning($"PLC Disconnected {ip}:{port}");
  60. }
  61. void IMini8DataNotifier.AlarmNotify(byte mini8, byte channel, float temperature)
  62. {
  63. }
  64. void IMini8DataNotifier.AlarmTcBrockenNotify(byte mini8, byte channel)
  65. {
  66. }
  67. void IMini8DataNotifier.Mini8ConnectNotify(byte mini8, bool connected)
  68. {
  69. }
  70. }