FinsPlc.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.IOCore;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using HslCommunication;
  9. using HslCommunication.Profinet.Omron;
  10. using MECF.Framework.Common.Communications;
  11. using MECF.Framework.Common.Event;
  12. using MECF.Framework.Common.PLC;
  13. using MECF.Framework.RT.Core.IoProviders;
  14. using MECF.Framework.RT.Core.IoProviders.Mitsubishis;
  15. using System;
  16. using System.Collections;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Linq;
  20. using System.Net;
  21. using System.Net.Sockets;
  22. using System.Text;
  23. using System.Threading;
  24. using System.Threading.Tasks;
  25. using System.Xml;
  26. namespace FurnaceRT.Equipments.Systems
  27. {
  28. public class FinsPlc : IPlc
  29. {
  30. public AlarmEventItem AlarmConnectFailed { get; set; }
  31. public AlarmEventItem AlarmCommunicationError { get; set; }
  32. public string Module { get; set; }
  33. public string Name { get; set; }
  34. public bool HasAlarm { get; set; }
  35. public string Address { get; set; }
  36. public bool IsConnected
  37. {
  38. get
  39. {
  40. if (_omronFins == null)
  41. return false;
  42. return _isOpened;
  43. }
  44. }
  45. public event Action OnConnected = null;
  46. public event Action OnDisconnected = null;
  47. public event Action<string, AlarmEventItem> OnDeviceAlarmStateChanged = null;
  48. private OmronFinsNet _omronFins = null;
  49. private bool _isOpened = false;
  50. private string _ip;
  51. private int _port;
  52. private R_TRIG _failedTrigger = new R_TRIG();
  53. //private List<IoBlockItem> _blockSectionsDemand;
  54. public FinsPlc(string ip, int port)
  55. {
  56. _port = port;
  57. _ip = ip;
  58. }
  59. #region IDevice interface
  60. public bool Initialize()
  61. {
  62. return true;
  63. }
  64. public void Monitor()
  65. {
  66. }
  67. public void Reset()
  68. {
  69. }
  70. public void Terminate()
  71. {
  72. }
  73. #endregion
  74. public bool CheckIsConnected()
  75. {
  76. if (_omronFins == null)
  77. return false;
  78. return _isOpened;
  79. }
  80. public bool Read(string variable, out object data, string type, int length, out string reason)
  81. {
  82. throw new NotImplementedException();
  83. }
  84. public bool WriteArrayElement(string variable, int index, object value, out string reason)
  85. {
  86. throw new NotImplementedException();
  87. }
  88. public bool ReadBool(string address, out bool[] data, int length, out string reason)
  89. {
  90. OperateResult<bool[]> result = _omronFins.ReadBool(address, (ushort)length);
  91. if (!result.IsSuccess)
  92. {
  93. reason = result.Message;
  94. data = null;
  95. return false;
  96. }
  97. data = result.Content;
  98. reason = "";
  99. return true;
  100. }
  101. public bool ReadInt16(string address, out short[] data, int length, out string reason)
  102. {
  103. OperateResult<short[]> result = _omronFins.ReadInt16(address, (ushort)length);
  104. if (!result.IsSuccess)
  105. {
  106. reason = result.Message;
  107. data = null;
  108. return false;
  109. }
  110. data = result.Content;
  111. reason = "";
  112. return true;
  113. }
  114. public bool ReadFloat(string address, out float[] data, int length, out string reason)
  115. {
  116. OperateResult<float[]> result = _omronFins.ReadFloat(address, (ushort)length);
  117. if (!result.IsSuccess)
  118. {
  119. reason = result.Message;
  120. data = null;
  121. return false;
  122. }
  123. data = result.Content;
  124. reason = "";
  125. return true;
  126. }
  127. public bool WriteBool(string address, bool[] data, out string reason)
  128. {
  129. OperateResult result = _omronFins.Write(address, data);
  130. if (!result.IsSuccess)
  131. {
  132. reason = result.Message;
  133. return false;
  134. }
  135. reason = "";
  136. return true;
  137. }
  138. public bool WriteInt16(string address, short[] data, out string reason)
  139. {
  140. OperateResult result = _omronFins.Write(address, data);
  141. if (!result.IsSuccess)
  142. {
  143. reason = result.Message;
  144. return false;
  145. }
  146. reason = "";
  147. return true;
  148. }
  149. public bool WriteFloat(string address, float[] data, out string reason)
  150. {
  151. OperateResult result = _omronFins.Write(address, data);
  152. if (!result.IsSuccess)
  153. {
  154. reason = result.Message;
  155. return false;
  156. }
  157. reason = "";
  158. return true;
  159. }
  160. public bool Write(string variable, object value, out string reason)
  161. {
  162. throw new NotImplementedException();
  163. }
  164. public bool ReadInt32(string address, out int[] data, int length, out string reason)
  165. {
  166. throw new NotImplementedException();
  167. }
  168. public bool Connect()
  169. {
  170. try
  171. {
  172. LOG.Write(String.Format("试图连接PLC {0}:{1}", _ip, _port));
  173. _isOpened = false;
  174. _omronFins = new OmronFinsNet(_ip, _port);
  175. OperateResult connect = _omronFins.ConnectServer();
  176. if (connect.IsSuccess)
  177. {
  178. _isOpened = true;
  179. }
  180. }
  181. catch (Exception ex)
  182. {
  183. _failedTrigger.CLK = true;
  184. if (_failedTrigger.Q)
  185. {
  186. LOG.Write(ex, String.Format("Communication failed with PLC {0}:{1}", _ip, _port));
  187. EV.PostMessage("System", EventEnum.DefaultWarning, String.Format("Communication failed with PLC {0}:{1}", _ip, _port));
  188. }
  189. return false;
  190. }
  191. _failedTrigger.RST = true;
  192. return _isOpened;
  193. }
  194. public bool Disconnect()
  195. {
  196. try
  197. {
  198. if (_isOpened)
  199. {
  200. _isOpened = false;
  201. _omronFins.ConnectClose();
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. LOG.Write(ex);
  207. }
  208. return true;
  209. }
  210. }
  211. }