PLCCommunicator.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using AdsCommunicatorNet8;
  2. using Log;
  3. using ProtocalGeneral;
  4. namespace PLCIOPointTool.Services;
  5. public class PLCCommunicator : ICommunicator, ITcpConnectNority, IDisposable
  6. {
  7. private readonly ILog? _log;
  8. private readonly AdsCommunicator _adsCommunicator;
  9. private const int _retryCount = 10;
  10. private bool _isConnected;
  11. private bool disposedValue;
  12. public PLCCommunicator(ILog? log)
  13. {
  14. _log = log;
  15. _adsCommunicator = new(_log);
  16. }
  17. public event EventHandler<bool>? ConnectionStatusChanged;
  18. public void Open(string netID, int port)
  19. {
  20. if (netID is null || port < 0)
  21. {
  22. _log?.Error("Invalid connection parameters");
  23. return;
  24. }
  25. if (!_adsCommunicator.IsInitialized && !_adsCommunicator.Initialize(this))
  26. {
  27. _log?.Error("Failed to initialize Tcp Client");
  28. return;
  29. }
  30. if (!_adsCommunicator.Open(netID, port, _retryCount))
  31. {
  32. _log?.Error("Failed to open Tcp Client");
  33. return;
  34. }
  35. _log?.Info("Communicator started");
  36. }
  37. public void Close()
  38. {
  39. _adsCommunicator.Dispose();
  40. _log?.Info("Communicator Closed");
  41. }
  42. public void GetValue(string symbolName, ValueType valueType)
  43. {
  44. if (string.IsNullOrWhiteSpace(symbolName))
  45. {
  46. _log?.Error("Symbol name can not be null");
  47. return;
  48. }
  49. if (!_isConnected)
  50. {
  51. _log?.Error("Client is in disconnected");
  52. return;
  53. }
  54. try
  55. {
  56. if (_adsCommunicator.GetValue(symbolName, out object? value) && value is not null)
  57. {
  58. bool rst = false;
  59. switch (valueType)
  60. {
  61. case ValueType.Boolean:
  62. if (value is bool) rst = true;
  63. break;
  64. case ValueType.Int32:
  65. if (value is Int32) rst = true;
  66. break;
  67. case ValueType.Int64:
  68. if (value is Int64) rst = true;
  69. break;
  70. case ValueType.Float:
  71. if (value is float) rst = true;
  72. break;
  73. case ValueType.Double:
  74. if (value is double) rst = true;
  75. break;
  76. default:
  77. break;
  78. }
  79. if (rst)
  80. {
  81. _log?.Info($"Get value of {symbolName} [{valueType.ToString()} {value}]");
  82. return;
  83. }
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. _log?.Error(ex.Message);
  89. }
  90. _log?.Info($"Failed to get value of {symbolName}");
  91. }
  92. public void SetValue(string symbolName, string inputValue, ValueType valueType)
  93. {
  94. if (string.IsNullOrWhiteSpace(symbolName) || string.IsNullOrWhiteSpace(inputValue))
  95. {
  96. _log?.Error("Symbol name or input value can not be null");
  97. return;
  98. }
  99. if (!_isConnected)
  100. {
  101. _log?.Error("Client is in disconnected");
  102. return;
  103. }
  104. try
  105. {
  106. bool rst = false;
  107. switch (valueType)
  108. {
  109. case ValueType.Boolean:
  110. rst = bool.TryParse(inputValue, out bool boolRst) && _adsCommunicator.SetValue(symbolName, boolRst);
  111. break;
  112. case ValueType.Int32:
  113. rst = Int32.TryParse(inputValue, out int int32Rst) && _adsCommunicator.SetValue(symbolName, int32Rst);
  114. break;
  115. case ValueType.Int64:
  116. rst = Int64.TryParse(inputValue, out long int64Rst) && _adsCommunicator.SetValue(symbolName, int64Rst);
  117. break;
  118. case ValueType.Float:
  119. rst = float.TryParse(inputValue, out float floatRst) && _adsCommunicator.SetValue(symbolName, floatRst);
  120. break;
  121. case ValueType.Double:
  122. rst = double.TryParse(inputValue, out double doubleRst) && _adsCommunicator.SetValue(symbolName, doubleRst);
  123. break;
  124. default:
  125. break;
  126. }
  127. if (rst)
  128. {
  129. _log?.Info($"Set value of {symbolName} [{valueType.ToString()} {inputValue}]");
  130. return;
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. _log?.Error(ex.Message);
  136. }
  137. _log?.Info($"Failed to set value of {symbolName}");
  138. }
  139. public void Connect(string ip, int port)
  140. {
  141. _log?.Info($"Connect to {ip}:{port}");
  142. _isConnected = true;
  143. ConnectionStatusChanged?.Invoke(null, true);
  144. }
  145. public void DisConnect(string ip, int port)
  146. {
  147. _log?.Info($"Disconnect from {ip}:{port}");
  148. _isConnected = false;
  149. ConnectionStatusChanged?.Invoke(null, false);
  150. }
  151. protected virtual void Dispose(bool disposing)
  152. {
  153. if (!disposedValue)
  154. {
  155. if (disposing)
  156. {
  157. // TODO: dispose managed state (managed objects)
  158. _adsCommunicator.Dispose();
  159. }
  160. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  161. // TODO: set large fields to null
  162. disposedValue = true;
  163. }
  164. }
  165. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  166. // ~PLCCommunicator()
  167. // {
  168. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  169. // Dispose(disposing: false);
  170. // }
  171. public void Dispose()
  172. {
  173. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  174. Dispose(disposing: true);
  175. GC.SuppressFinalize(this);
  176. }
  177. }