FinsTcpPlc.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 JetMainframe.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. { if (omronFinsNet == null)
  68. return false;
  69. return omronFinsNet.IsConnected; ;
  70. }
  71. }
  72. public bool Connect()
  73. {
  74. try
  75. {
  76. LOG.Write(String.Format("试图连接PLC {0}:{1}", _ip, _port));
  77. _isOpened = false;
  78. omronFinsNet = new OmronFinsNet(_ip, _port, _localIp){ConnectTimeOut = 1000,ReceiveTimeOut = 1000};
  79. OperateResult connect = omronFinsNet.ConnectServer();
  80. if (connect.IsSuccess)
  81. {
  82. _isOpened = true;
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. _failedTrigger.CLK = true;
  88. if (_failedTrigger.Q)
  89. {
  90. LOG.Write(ex, String.Format("Communication failed with PLC {0}:{1}", _ip, _port));
  91. EV.PostMessage("System", EventEnum.DefaultWarning, String.Format("Communication failed with PLC {0}:{1}", _ip, _port));
  92. }
  93. return false;
  94. }
  95. _failedTrigger.RST = true;
  96. return true;
  97. }
  98. public bool Disconnect()
  99. {
  100. try
  101. {
  102. if (_isOpened)
  103. {
  104. _isOpened = false;
  105. omronFinsNet.ConnectClose();
  106. }
  107. }
  108. catch (Exception ex)
  109. {
  110. LOG.Write(ex);
  111. }
  112. return true;
  113. }
  114. public AlarmEventItem AlarmConnectFailed { get; set; }
  115. public AlarmEventItem AlarmCommunicationError { get; set; }
  116. public event Action OnConnected;
  117. public event Action OnDisconnected;
  118. public bool CheckIsConnected()
  119. {
  120. if (omronFinsNet == null)
  121. return false;
  122. return omronFinsNet.IsConnected;
  123. }
  124. public bool Read(string variable, out object data, string type, int length, out string reason)
  125. {
  126. throw new NotImplementedException();
  127. }
  128. public bool WriteArrayElement(string variable, int index, object value, out string reason)
  129. {
  130. throw new NotImplementedException();
  131. }
  132. public bool ReadBool(string address, out bool[] data, int length, out string reason)
  133. {
  134. try
  135. {
  136. double interval = _timerRead.GetElapseTime();
  137. if (interval > _averageWriteTime)
  138. {
  139. //LOG.Write(_ip + ":Max read PLC interval : " + interval);
  140. if (_averageReadTime < 0.1)
  141. _averageReadTime = interval;
  142. _averageReadTime = (interval + _averageReadTime) / 2;
  143. }
  144. _timerRead.Start(0);
  145. var read = omronFinsNet.ReadBool(address, (ushort)length);
  146. if (read.IsSuccess)
  147. {
  148. data = (bool[])read.Content.Clone();
  149. }
  150. else
  151. {
  152. data = null;
  153. reason = read.Message;
  154. return false;
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. LOG.Error($"PLC ({_ip}) Read exception.", ex);
  160. reason = $"PLC ReadBool exception, {ex.Message}";
  161. data = null;
  162. return false;
  163. }
  164. reason = string.Empty;
  165. return true;
  166. }
  167. public bool ReadInt16(string address, out short[] data, int length, out string reason)
  168. {
  169. try
  170. {
  171. double interval = _timerRead.GetElapseTime();
  172. if (interval > _averageWriteTime)
  173. {
  174. //LOG.Write(_ip + ":Max read PLC interval : " + interval);
  175. if (_averageReadTime < 0.1)
  176. _averageReadTime = interval;
  177. _averageReadTime = (interval + _averageReadTime) / 2;
  178. }
  179. _timerRead.Start(0);
  180. var read = omronFinsNet.ReadInt16(address, (ushort)length);
  181. if (read.IsSuccess)
  182. {
  183. data = (short[])read.Content.Clone();
  184. }
  185. else
  186. {
  187. data = null;
  188. reason = read.Message;
  189. return false;
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. LOG.Error($"PLC ({_ip}) ReadInt16 exception.", ex);
  195. reason = $"PLC ReadInt16 exception, {ex.Message}";
  196. data = null;
  197. return false;
  198. }
  199. reason = string.Empty;
  200. return true;
  201. }
  202. public bool ReadFloat(string address, out float[] data, int length, out string reason)
  203. {
  204. try
  205. {
  206. double interval = _timerRead.GetElapseTime();
  207. if (interval > _averageWriteTime)
  208. {
  209. //LOG.Write(_ip + ":Max read PLC interval : " + interval);
  210. if (_averageReadTime < 0.1)
  211. _averageReadTime = interval;
  212. _averageReadTime = (interval + _averageReadTime) / 2;
  213. }
  214. _timerRead.Start(0);
  215. var read = omronFinsNet.ReadFloat(address, (ushort)length);
  216. if (read.IsSuccess)
  217. {
  218. data = (float[])read.Content.Clone();
  219. }
  220. else
  221. {
  222. data = null;
  223. reason = read.Message;
  224. return false;
  225. }
  226. }
  227. catch (Exception ex)
  228. {
  229. LOG.Error($"PLC ({_ip}) ReadInt16 exception.", ex);
  230. reason = $"PLC ReadInt16 exception, {ex.Message}";
  231. data = null;
  232. return false;
  233. }
  234. reason = string.Empty;
  235. return true;
  236. }
  237. public bool WriteBool(string address, bool[] data, out string reason)
  238. {
  239. try
  240. {
  241. _timerWrite.Start(0);
  242. var write = omronFinsNet.Write(address, data);
  243. if (!write.IsSuccess)
  244. {
  245. reason = write.Message;
  246. return false;
  247. }
  248. double interval = _timerWrite.GetElapseTime();
  249. if (interval > _averageWriteTime)
  250. {
  251. LOG.Write(_ip + ":Max write PLC interval : " + interval);
  252. if (_averageWriteTime < 0.1)
  253. _averageWriteTime = interval;
  254. _averageWriteTime = (_averageWriteTime + interval) / 2;
  255. }
  256. }
  257. catch (Exception ex)
  258. {
  259. LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
  260. reason = $"PLC WriteBool exception, {ex.Message}";
  261. return false;
  262. }
  263. reason = string.Empty;
  264. return true;
  265. }
  266. public bool WriteInt16(string address, short[] data, out string reason)
  267. {
  268. try
  269. {
  270. _timerWrite.Start(0);
  271. var write = omronFinsNet.Write(address, data);
  272. if (!write.IsSuccess)
  273. {
  274. reason = write.Message;
  275. return false;
  276. }
  277. double interval = _timerWrite.GetElapseTime();
  278. if (interval > _averageWriteTime)
  279. {
  280. LOG.Write(_ip + ":Max write PLC interval : " + interval);
  281. if (_averageWriteTime < 0.1)
  282. _averageWriteTime = interval;
  283. _averageWriteTime = (_averageWriteTime + interval) / 2;
  284. }
  285. }
  286. catch (Exception ex)
  287. {
  288. LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
  289. reason = $"PLC WriteBool exception, {ex.Message}";
  290. return false;
  291. }
  292. reason = string.Empty;
  293. return true;
  294. }
  295. public bool WriteFloat(string address, float[] data, out string reason)
  296. {
  297. try
  298. {
  299. _timerWrite.Start(0);
  300. var write = omronFinsNet.Write(address, data);
  301. if (!write.IsSuccess)
  302. {
  303. reason = write.Message;
  304. return false;
  305. }
  306. double interval = _timerWrite.GetElapseTime();
  307. if (interval > _averageWriteTime)
  308. {
  309. LOG.Write(_ip + ":Max write PLC interval : " + interval);
  310. if (_averageWriteTime < 0.1)
  311. _averageWriteTime = interval;
  312. _averageWriteTime = (_averageWriteTime + interval) / 2;
  313. }
  314. }
  315. catch (Exception ex)
  316. {
  317. LOG.Error($"PLC ({_ip}) WriteBool exception.", ex);
  318. reason = $"PLC WriteBool exception, {ex.Message}";
  319. return false;
  320. }
  321. reason = string.Empty;
  322. return true;
  323. }
  324. }
  325. }