PLCCommunicator.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. }
  36. public void Close()
  37. {
  38. _adsCommunicator.Dispose();
  39. }
  40. public void GetValue(string symbolName, ValueType valueType)
  41. {
  42. if (string.IsNullOrWhiteSpace(symbolName))
  43. {
  44. _log?.Error("Symbol name can not be null");
  45. return;
  46. }
  47. if (!_isConnected)
  48. {
  49. _log?.Error("Client is in disconnected");
  50. return;
  51. }
  52. try
  53. {
  54. if (_adsCommunicator.GetValue(symbolName, out object? value) && value is not null)
  55. {
  56. bool rst = false;
  57. switch (valueType)
  58. {
  59. case ValueType.Boolean:
  60. if (value is bool) rst = true;
  61. break;
  62. case ValueType.Int32:
  63. if (value is Int32) rst = true;
  64. break;
  65. case ValueType.Int64:
  66. if (value is Int64) rst = true;
  67. break;
  68. case ValueType.Float:
  69. if (value is float) rst = true;
  70. break;
  71. case ValueType.Double:
  72. if (value is double) rst = true;
  73. break;
  74. default:
  75. break;
  76. }
  77. if (rst)
  78. {
  79. _log?.Info($"Get value of {symbolName} [{valueType.ToString()} {value}]");
  80. return;
  81. }
  82. }
  83. }
  84. catch (Exception ex)
  85. {
  86. _log?.Error(ex.Message);
  87. }
  88. _log?.Info($"Failed to get value of {symbolName}");
  89. }
  90. public void SetValue(string symbolName, string inputValue, ValueType valueType)
  91. {
  92. if (string.IsNullOrWhiteSpace(symbolName) || string.IsNullOrWhiteSpace(inputValue))
  93. {
  94. _log?.Error("Symbol name or input value can not be null");
  95. return;
  96. }
  97. if (!_isConnected)
  98. {
  99. _log?.Error("Client is in disconnected");
  100. return;
  101. }
  102. try
  103. {
  104. bool rst = false;
  105. switch (valueType)
  106. {
  107. case ValueType.Boolean:
  108. rst = bool.TryParse(inputValue, out bool boolRst) && _adsCommunicator.SetValue(symbolName, boolRst);
  109. break;
  110. case ValueType.Int32:
  111. rst = Int32.TryParse(inputValue, out int int32Rst) && _adsCommunicator.SetValue(symbolName, int32Rst);
  112. break;
  113. case ValueType.Int64:
  114. rst = Int64.TryParse(inputValue, out long int64Rst) && _adsCommunicator.SetValue(symbolName, int64Rst);
  115. break;
  116. case ValueType.Float:
  117. rst = float.TryParse(inputValue, out float floatRst) && _adsCommunicator.SetValue(symbolName, floatRst);
  118. break;
  119. case ValueType.Double:
  120. rst = double.TryParse(inputValue, out double doubleRst) && _adsCommunicator.SetValue(symbolName, doubleRst);
  121. break;
  122. default:
  123. break;
  124. }
  125. if (rst)
  126. {
  127. _log?.Info($"Set value of {symbolName} [{valueType.ToString()} {inputValue}]");
  128. return;
  129. }
  130. }
  131. catch (Exception ex)
  132. {
  133. _log?.Error(ex.Message);
  134. }
  135. _log?.Info($"Failed to set value of {symbolName}");
  136. }
  137. public void Connect(string ip, int port)
  138. {
  139. _isConnected = true;
  140. ConnectionStatusChanged?.Invoke(null, true);
  141. }
  142. public void DisConnect(string ip, int port)
  143. {
  144. _isConnected = false;
  145. ConnectionStatusChanged?.Invoke(null, false);
  146. }
  147. protected virtual void Dispose(bool disposing)
  148. {
  149. if (!disposedValue)
  150. {
  151. if (disposing)
  152. {
  153. // TODO: dispose managed state (managed objects)
  154. _adsCommunicator.Dispose();
  155. }
  156. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  157. // TODO: set large fields to null
  158. disposedValue = true;
  159. }
  160. }
  161. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  162. // ~PLCCommunicator()
  163. // {
  164. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  165. // Dispose(disposing: false);
  166. // }
  167. public void Dispose()
  168. {
  169. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  170. Dispose(disposing: true);
  171. GC.SuppressFinalize(this);
  172. }
  173. }