FinsTcpPlc.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.Util;
  10. using MECF.Framework.Common.Event;
  11. using MECF.Framework.Common.PLC;
  12. using MECF.Framework.RT.Core.IoProviders.Common;
  13. using MECF.Framework.RT.Core.IoProviders.Common.Transfer;
  14. using MECF.Framework.RT.Core.IoProviders.Omron;
  15. namespace JetVirgoPM.Devices
  16. {
  17. public class FinsTcpPlc : IPlc
  18. {
  19. private OmronFinsNet omronFinsNet = null;
  20. private bool _isOpened = false;
  21. private string _ip = "192.168.10.10";
  22. private string _localIp = "192.168.10.199";
  23. private int _port = 9600;
  24. private int _aoBlockStartPosition = 1000;
  25. private int _aiBlockStartPosition = 2000;
  26. private int _doBlockStartPosition = 0;
  27. private int _diBlockStartPosition = 20;
  28. R_TRIG _failedTrigger = new R_TRIG();
  29. DeviceTimer _timerWrite = new DeviceTimer();
  30. DeviceTimer _timerRead = new DeviceTimer();
  31. private double _averageWriteTime = 0;
  32. private double _averageReadTime = 0;
  33. public FinsTcpPlc(string ip, int port, string localIp)
  34. {
  35. _ip = ip;
  36. _port = port;
  37. _localIp = localIp;
  38. }
  39. private void RaiseException(string p)
  40. {
  41. _isOpened = false;
  42. }
  43. public event Action<string, AlarmEventItem> OnDeviceAlarmStateChanged;
  44. public string Module { get; set; }
  45. public string Name { get; set; }
  46. public bool HasAlarm { get; }
  47. public bool Initialize()
  48. {
  49. throw new NotImplementedException();
  50. }
  51. public void Monitor()
  52. {
  53. throw new NotImplementedException();
  54. }
  55. public void Terminate()
  56. {
  57. throw new NotImplementedException();
  58. }
  59. public void Reset()
  60. {
  61. throw new NotImplementedException();
  62. }
  63. public string Address { get; }
  64. public bool IsConnected
  65. {
  66. get
  67. {
  68. if (omronFinsNet == null)
  69. return false;
  70. return omronFinsNet.IsConnected; ;
  71. }
  72. }
  73. public bool Connect()
  74. {
  75. try
  76. {
  77. LOG.Write(String.Format("试图连接PLC {0}:{1}", _ip, _port));
  78. _isOpened = false;
  79. omronFinsNet = new OmronFinsNet(_ip, _port, _localIp);
  80. OperateResult connect = omronFinsNet.ConnectServer();
  81. if (connect.IsSuccess)
  82. {
  83. _isOpened = true;
  84. }
  85. }
  86. catch (Exception ex)
  87. {
  88. _failedTrigger.CLK = true;
  89. if (_failedTrigger.Q)
  90. {
  91. LOG.Write(ex, String.Format("Communication failed with PLC {0}:{1}", _ip, _port));
  92. EV.PostMessage("System", EventEnum.DefaultWarning, String.Format("Communication failed with PLC {0}:{1}", _ip, _port));
  93. }
  94. return false;
  95. }
  96. _failedTrigger.RST = true;
  97. return true;
  98. }
  99. public bool Disconnect()
  100. {
  101. try
  102. {
  103. if (_isOpened)
  104. {
  105. _isOpened = false;
  106. omronFinsNet.ConnectClose();
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. LOG.Write(ex);
  112. }
  113. return true;
  114. }
  115. public AlarmEventItem AlarmConnectFailed { get; set; }
  116. public AlarmEventItem AlarmCommunicationError { get; set; }
  117. public event Action OnConnected;
  118. public event Action OnDisconnected;
  119. public bool CheckIsConnected()
  120. {
  121. if (omronFinsNet == null)
  122. return false;
  123. return omronFinsNet.IsConnected;
  124. }
  125. public bool Read(string variable, out object data, string type, int length, out string reason)
  126. {
  127. throw new NotImplementedException();
  128. }
  129. public bool WriteArrayElement(string variable, int index, object value, out string reason)
  130. {
  131. throw new NotImplementedException();
  132. }
  133. public bool ReadBool(string address, out bool[] data, int length, out string reason)
  134. {
  135. try
  136. {
  137. double interval = _timerRead.GetElapseTime();
  138. if (interval > _averageWriteTime)
  139. {
  140. //LOG.Write(_ip + ":Max read PLC interval : " + interval);
  141. if (_averageReadTime < 0.1)
  142. _averageReadTime = interval;
  143. _averageReadTime = (interval + _averageReadTime) / 2;
  144. }
  145. _timerRead.Start(0);
  146. var read = omronFinsNet.ReadBool(address, (ushort)length);
  147. if (read.IsSuccess)
  148. {
  149. data = (bool[])read.Content.Clone();
  150. }
  151. else
  152. {
  153. data = null;
  154. reason = read.Message;
  155. return false;
  156. }
  157. }
  158. catch (Exception ex)
  159. {
  160. LOG.Error($"PLC ({_ip}) Read exception.", ex);
  161. reason = $"PLC ReadBool exception, {ex.Message}";
  162. data = null;
  163. return false;
  164. }
  165. reason = string.Empty;
  166. return true;
  167. }
  168. public bool ReadInt16(string address, out short[] data, int length, out string reason)
  169. {
  170. try
  171. {
  172. double interval = _timerRead.GetElapseTime();
  173. if (interval > _averageWriteTime)
  174. {
  175. //LOG.Write(_ip + ":Max read PLC interval : " + interval);
  176. if (_averageReadTime < 0.1)
  177. _averageReadTime = interval;
  178. _averageReadTime = (interval + _averageReadTime) / 2;
  179. }
  180. _timerRead.Start(0);
  181. var read = omronFinsNet.ReadInt16(address, (ushort)length);
  182. if (read.IsSuccess)
  183. {
  184. data = (short[])read.Content.Clone();
  185. }
  186. else
  187. {
  188. data = null;
  189. reason = read.Message;
  190. return false;
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. LOG.Error($"PLC ({_ip}) ReadInt16 exception.", ex);
  196. reason = $"PLC ReadInt16 exception, {ex.Message}";
  197. data = null;
  198. return false;
  199. }
  200. reason = string.Empty;
  201. return true;
  202. }
  203. public bool ReadFloat(string address, out float[] data, int length, out string reason)
  204. {
  205. try
  206. {
  207. double interval = _timerRead.GetElapseTime();
  208. if (interval > _averageWriteTime)
  209. {
  210. //LOG.Write(_ip + ":Max read PLC interval : " + interval);
  211. if (_averageReadTime < 0.1)
  212. _averageReadTime = interval;
  213. _averageReadTime = (interval + _averageReadTime) / 2;
  214. }
  215. _timerRead.Start(0);
  216. var read = omronFinsNet.ReadFloat(address, (ushort)length);
  217. if (read.IsSuccess)
  218. {
  219. data = (float[])read.Content.Clone();
  220. }
  221. else
  222. {
  223. data = null;
  224. reason = read.Message;
  225. return false;
  226. }
  227. }
  228. catch (Exception ex)
  229. {
  230. LOG.Error($"PLC ({_ip}) ReadInt16 exception.", ex);
  231. reason = $"PLC ReadInt16 exception, {ex.Message}";
  232. data = null;
  233. return false;
  234. }
  235. reason = string.Empty;
  236. return true;
  237. }
  238. public bool WriteBool(string address, bool[] data, out string reason)
  239. {
  240. try
  241. {
  242. _timerWrite.Start(0);
  243. var write = omronFinsNet.Write(address, data);
  244. if (!write.IsSuccess)
  245. {
  246. reason = write.Message;
  247. return false;
  248. }
  249. double interval = _timerWrite.GetElapseTime();
  250. if (interval > _averageWriteTime)
  251. {
  252. LOG.Write(_ip + ":Max write PLC interval : " + interval);
  253. if (_averageWriteTime < 0.1)
  254. _averageWriteTime = interval;
  255. _averageWriteTime = (_averageWriteTime + interval) / 2;
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
  261. reason = $"PLC WriteBool exception, {ex.Message}";
  262. return false;
  263. }
  264. reason = string.Empty;
  265. return true;
  266. }
  267. public bool WriteInt16(string address, short[] data, out string reason)
  268. {
  269. try
  270. {
  271. _timerWrite.Start(0);
  272. var write = omronFinsNet.Write(address, data);
  273. if (!write.IsSuccess)
  274. {
  275. reason = write.Message;
  276. return false;
  277. }
  278. double interval = _timerWrite.GetElapseTime();
  279. if (interval > _averageWriteTime)
  280. {
  281. LOG.Write(_ip + ":Max write PLC interval : " + interval);
  282. if (_averageWriteTime < 0.1)
  283. _averageWriteTime = interval;
  284. _averageWriteTime = (_averageWriteTime + interval) / 2;
  285. }
  286. }
  287. catch (Exception ex)
  288. {
  289. LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
  290. reason = $"PLC WriteBool exception, {ex.Message}";
  291. return false;
  292. }
  293. reason = string.Empty;
  294. return true;
  295. }
  296. public bool WriteFloat(string address, float[] data, out string reason)
  297. {
  298. try
  299. {
  300. _timerWrite.Start(0);
  301. var write = omronFinsNet.Write(address, data);
  302. if (!write.IsSuccess)
  303. {
  304. reason = write.Message;
  305. return false;
  306. }
  307. double interval = _timerWrite.GetElapseTime();
  308. if (interval > _averageWriteTime)
  309. {
  310. LOG.Write(_ip + ":Max write PLC interval : " + interval);
  311. if (_averageWriteTime < 0.1)
  312. _averageWriteTime = interval;
  313. _averageWriteTime = (_averageWriteTime + interval) / 2;
  314. }
  315. }
  316. catch (Exception ex)
  317. {
  318. LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
  319. reason = $"PLC WriteBool exception, {ex.Message}";
  320. return false;
  321. }
  322. reason = string.Empty;
  323. return true;
  324. }
  325. }
  326. }