ConnectionBase.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Threading;
  5. using Aitex.Core.RT.Event;
  6. using Aitex.Core.RT.Log;
  7. using Aitex.Core.Util;
  8. namespace MECF.Framework.Common.Communications
  9. {
  10. public abstract class SerialPortConnectionBase : IConnection
  11. {
  12. public string Address
  13. {
  14. get { return _address; }
  15. }
  16. public bool IsConnected
  17. {
  18. get { return _port.IsOpen(); }
  19. }
  20. public bool Connect()
  21. {
  22. return _port.Open();
  23. }
  24. public bool Disconnect()
  25. {
  26. _port.Close();
  27. return true;
  28. }
  29. public void TerminateCom()
  30. {
  31. _port.Dispose();
  32. }
  33. public bool IsBusy
  34. {
  35. get { return _activeHandler != null; }
  36. }
  37. public bool IsCommunicationError { get; private set; }
  38. public string LastCommunicationError { get; private set; }
  39. private AsyncSerialPort _port;
  40. protected HandlerBase _activeHandler; //set, control,
  41. public HandlerBase HandlerInError;
  42. private object _lockerActiveHandler = new object();
  43. private string _address;
  44. private bool _isAsciiMode;
  45. public int retryTime = 0;
  46. private PeriodicJob _thread;
  47. private object _locker = new object();
  48. private LinkedList<string> _lstAsciiMsgs = new LinkedList<string>();
  49. private LinkedList<byte[]> _lstBinsMsgs = new LinkedList<byte[]>();
  50. private string _newLine;
  51. public SerialPortConnectionBase(string port, int baudRate=9600, int dataBits=8, Parity parity = Parity.None, StopBits stopBits = StopBits.One, string newline = "\r", bool isAsciiMode = true)
  52. {
  53. _address = port;
  54. _isAsciiMode = isAsciiMode;
  55. _newLine = newline;
  56. _port = new AsyncSerialPort(port, baudRate, dataBits, parity, stopBits, newline, isAsciiMode);
  57. _port.OnDataChanged += _port_OnAsciiDataReceived;
  58. _port.OnBinaryDataChanged += _port_OnBinaryDataChanged;
  59. _port.OnErrorHappened += _port_OnErrorHappened;
  60. _thread = new PeriodicJob(2, OnTimer, $"{port}.MonitorHandler", true);
  61. }
  62. private bool OnTimer()
  63. {
  64. lock (_locker)
  65. {
  66. if (_isAsciiMode)
  67. {
  68. while (_lstAsciiMsgs.Count > 0)
  69. {
  70. string asciiMsg = _lstAsciiMsgs.First.Value;
  71. _port_HandleAsciiData(asciiMsg);
  72. _lstAsciiMsgs.RemoveFirst();
  73. }
  74. }
  75. else
  76. {
  77. while (_lstBinsMsgs.Count > 0)
  78. {
  79. byte[] binMsg = _lstBinsMsgs.First.Value;
  80. _port_HandleBinarayData(binMsg);
  81. _lstBinsMsgs.RemoveFirst();
  82. }
  83. }
  84. }
  85. return true;
  86. }
  87. public void SetPortAddress(string portName)
  88. {
  89. _port.PortName = portName;
  90. }
  91. private void _port_OnErrorHappened(string obj)
  92. {
  93. LOG.Error(obj);
  94. }
  95. public virtual bool SendMessage(string message)
  96. {
  97. if (_port != null && _port.IsOpen())
  98. return _port.Write(message);
  99. LOG.Error($"No connection writing message {message}");
  100. return false;
  101. }
  102. public virtual bool SendMessage(byte[] message)
  103. {
  104. if (_port != null && _port.IsOpen())
  105. return _port.Write(message);
  106. LOG.Error($"No connection writing message {string.Join(" ", Array.ConvertAll(message, x => x.ToString("X2")))}");
  107. return false;
  108. }
  109. public void ForceClear()
  110. {
  111. lock (_lockerActiveHandler)
  112. {
  113. IsCommunicationError = false;
  114. _activeHandler = null;
  115. }
  116. }
  117. public void Execute(HandlerBase handler)
  118. {
  119. if (_activeHandler != null)
  120. return;
  121. if (handler == null)
  122. return;
  123. if (_port.IsOpen())
  124. {
  125. lock (_lockerActiveHandler)
  126. {
  127. retryTime = 0;
  128. _activeHandler = handler;
  129. _activeHandler.SetState(EnumHandlerState.Sent);
  130. }
  131. bool sendResult = _isAsciiMode ? SendMessage(handler.SendText) : SendMessage(handler.SendBinary);
  132. if (!sendResult)
  133. {
  134. lock (_lockerActiveHandler)
  135. {
  136. _activeHandler = null;
  137. }
  138. }
  139. }
  140. }
  141. protected virtual MessageBase ParseResponse(string rawMessage)
  142. {
  143. return null;
  144. }
  145. protected virtual MessageBase ParseResponse(byte[] rawMessage)
  146. {
  147. return null;
  148. }
  149. protected virtual void OnEventArrived(MessageBase msg)
  150. {
  151. }
  152. protected virtual void ActiveHandlerProceedMessage(MessageBase msg)
  153. {
  154. lock (_lockerActiveHandler)
  155. {
  156. if (_activeHandler != null)
  157. {
  158. if (msg.IsFormatError || (_activeHandler.HandleMessage(msg, out bool transactionComplete) && transactionComplete))
  159. {
  160. _activeHandler = null;
  161. }
  162. }
  163. }
  164. }
  165. public void EnableLog(bool enable)
  166. {
  167. _port.EnableLog = enable;
  168. }
  169. private void ProceedTransactionMessage(MessageBase msg)
  170. {
  171. if (msg == null || msg.IsFormatError)
  172. {
  173. SetCommunicationError(true, "received invalid response message.");
  174. return;
  175. }
  176. if (msg.IsEvent)
  177. {
  178. OnEventArrived(msg);
  179. //return;
  180. }
  181. //当前活动交互会话,继续执行
  182. ActiveHandlerProceedMessage(msg);
  183. }
  184. private void _port_OnBinaryDataChanged(byte[] binaryData)
  185. {
  186. lock(_locker)
  187. {
  188. _lstBinsMsgs.AddLast(binaryData);
  189. }
  190. }
  191. private void _port_HandleBinarayData(byte[] binaryData)
  192. {
  193. MessageBase msg = ParseResponse(binaryData);
  194. ProceedTransactionMessage(msg);
  195. }
  196. private void _port_OnAsciiDataReceived(string oneLineMessage)
  197. {
  198. lock(_locker)
  199. {
  200. if (string.IsNullOrEmpty(_newLine))
  201. {
  202. _lstAsciiMsgs.AddLast(oneLineMessage);
  203. }
  204. else
  205. {
  206. foreach (var message in oneLineMessage.Split(_newLine.ToCharArray()))
  207. {
  208. if (!string.IsNullOrEmpty(message))
  209. _lstAsciiMsgs.AddLast(message + _newLine);
  210. }
  211. }
  212. }
  213. }
  214. private void _port_HandleAsciiData(string oneLineMessage)
  215. {
  216. MessageBase msg = ParseResponse(oneLineMessage);
  217. ProceedTransactionMessage(msg);
  218. }
  219. public HandlerBase MonitorTimeout()
  220. {
  221. HandlerBase result = null;
  222. lock (_lockerActiveHandler)
  223. {
  224. if (_activeHandler != null && _activeHandler.CheckTimeout())
  225. {
  226. if (retryTime++ < 5) Retry();
  227. else
  228. {
  229. EV.PostWarningLog("System", $"{Address} receive {_activeHandler.Name} timeout");
  230. result = _activeHandler;
  231. HandlerInError = _activeHandler;
  232. _activeHandler = null;
  233. SetCommunicationError(true, "receive response timeout");
  234. }
  235. }
  236. }
  237. return result;
  238. }
  239. public void Retry()
  240. {
  241. if (_activeHandler == null)
  242. return;
  243. if (_port.IsOpen())
  244. {
  245. //_activeHandler = handler;
  246. _activeHandler.SetState(EnumHandlerState.Sent);
  247. bool sendResult = _isAsciiMode ? SendMessage(_activeHandler.SendText) : SendMessage(_activeHandler.SendBinary);
  248. if (!sendResult) _activeHandler = null;
  249. }
  250. }
  251. public void SetCommunicationError(bool isError, string reason)
  252. {
  253. IsCommunicationError = isError;
  254. LastCommunicationError = reason;
  255. }
  256. }
  257. public abstract class TCPPortConnectionBase : IConnection
  258. {
  259. public string Address
  260. {
  261. get { return _address; }
  262. }
  263. public bool IsConnected
  264. {
  265. get { return _socket.IsConnected; }
  266. }
  267. public bool Connect()
  268. {
  269. _socket.Connect();
  270. int iCount = 0;
  271. while(!IsConnected && iCount < 25)
  272. {
  273. Thread.Sleep(200);
  274. iCount++;
  275. }
  276. if(IsConnected)
  277. {
  278. return true;
  279. }
  280. else
  281. {
  282. Disconnect();
  283. return false;
  284. }
  285. }
  286. public bool Disconnect()
  287. {
  288. _socket.Dispose();
  289. return true;
  290. }
  291. public bool IsBusy
  292. {
  293. get { return _activeHandler != null; }
  294. }
  295. public bool IsCommunicationError { get; private set; }
  296. public string LastCommunicationError { get; private set; }
  297. private AsynSocketClient _socket;
  298. protected HandlerBase _activeHandler; //set, control,
  299. public HandlerBase HandlerInError;
  300. protected object _lockerActiveHandler = new object();
  301. private string _address;
  302. private bool _isAsciiMode;
  303. public int retryTime = 0;
  304. private PeriodicJob _thread;
  305. private object _locker = new object();
  306. private LinkedList<string> _lstAsciiMsgs = new LinkedList<string>();
  307. private LinkedList<byte[]> _lstBinsMsgs = new LinkedList<byte[]>();
  308. private string _newLine;
  309. public TCPPortConnectionBase(string address, string newline = "\r", bool isAsciiMode = true)
  310. {
  311. _address = address;
  312. _newLine = newline;
  313. _isAsciiMode = isAsciiMode;
  314. _socket = new AsynSocketClient(address, isAsciiMode, newline);
  315. _socket.OnDataChanged += _port_OnAsciiDataReceived;
  316. _socket.OnBinaryDataChanged += _port_OnBinaryDataChanged;
  317. _socket.OnErrorHappened += _port_OnErrorHappened;
  318. _thread = new PeriodicJob(1,OnTimer, $"{address}.MonitorHandler", true);
  319. }
  320. private bool OnTimer()
  321. {
  322. lock (_locker)
  323. {
  324. if (_isAsciiMode)
  325. {
  326. while (_lstAsciiMsgs.Count > 0)
  327. {
  328. string asciiMsg = _lstAsciiMsgs.First.Value;
  329. if (!string.IsNullOrEmpty(asciiMsg))
  330. {
  331. if(_socket.NeedLog)
  332. LOG.Write($"Start handler message:{asciiMsg}");
  333. _port_HandleAsciiData(asciiMsg);
  334. }
  335. _lstAsciiMsgs.RemoveFirst();
  336. }
  337. }
  338. else
  339. {
  340. while (_lstBinsMsgs.Count > 0)
  341. {
  342. byte[] binMsg = _lstBinsMsgs.First.Value;
  343. _port_HandleBinarayData(binMsg);
  344. _lstBinsMsgs.RemoveFirst();
  345. }
  346. }
  347. }
  348. return true;
  349. }
  350. //public void SetPortAddress(string portName)
  351. //{
  352. // _port.PortName = portName;
  353. //}
  354. private void _port_OnErrorHappened(TCPErrorEventArgs obj)
  355. {
  356. LOG.Error(obj.Reason);
  357. }
  358. public virtual bool SendMessage(string message)
  359. {
  360. if (_socket != null && _socket.IsConnected)
  361. return _socket.Write(message);
  362. LOG.Error($"No connection writing message {message}");
  363. return false;
  364. }
  365. public virtual bool SendMessage(byte[] message)
  366. {
  367. if (_socket != null && _socket.IsConnected)
  368. return _socket.Write(message);
  369. LOG.Error($"No connection writing message {string.Join(" ", Array.ConvertAll(message, x => x.ToString("X2")))}");
  370. return false;
  371. }
  372. public void ForceClear()
  373. {
  374. lock (_lockerActiveHandler)
  375. {
  376. _activeHandler = null;
  377. IsCommunicationError = false;
  378. }
  379. }
  380. public void Execute(HandlerBase handler)
  381. {
  382. if (_activeHandler != null)
  383. return;
  384. if (handler == null)
  385. return;
  386. if (_socket.IsConnected)
  387. {
  388. lock (_lockerActiveHandler)
  389. {
  390. retryTime = 0;
  391. _activeHandler = handler;
  392. _activeHandler.SetState(EnumHandlerState.Sent);
  393. }
  394. bool sendResult = _isAsciiMode ? SendMessage(handler.SendText) : SendMessage(handler.SendBinary);
  395. if (!sendResult)
  396. {
  397. lock (_lockerActiveHandler)
  398. {
  399. _activeHandler = null;
  400. }
  401. }
  402. }
  403. }
  404. protected virtual MessageBase ParseResponse(string rawMessage)
  405. {
  406. return null;
  407. }
  408. protected virtual MessageBase ParseResponse(byte[] rawMessage)
  409. {
  410. return null;
  411. }
  412. protected virtual void OnEventArrived(MessageBase msg)
  413. {
  414. }
  415. protected virtual void ActiveHandlerProceedMessage(MessageBase msg)
  416. {
  417. lock (_lockerActiveHandler)
  418. {
  419. if (_activeHandler != null)
  420. {
  421. if (msg.IsFormatError || (_activeHandler.HandleMessage(msg, out bool transactionComplete) && transactionComplete))
  422. {
  423. _activeHandler = null;
  424. }
  425. }
  426. }
  427. }
  428. public void EnableLog(bool enable)
  429. {
  430. _socket.NeedLog = enable;
  431. }
  432. private void ProceedTransactionMessage(MessageBase msg)
  433. {
  434. if (msg == null || msg.IsFormatError)
  435. {
  436. SetCommunicationError(true, "received invalid response message.");
  437. return;
  438. }
  439. if (msg.IsEvent)
  440. {
  441. OnEventArrived(msg);
  442. //return;
  443. }
  444. //当前活动交互会话,继续执行
  445. ActiveHandlerProceedMessage(msg);
  446. }
  447. private void _port_OnBinaryDataChanged(byte[] binaryData)
  448. {
  449. lock (_locker)
  450. {
  451. _lstBinsMsgs.AddLast(binaryData);
  452. }
  453. }
  454. private void _port_HandleBinarayData(byte[] binaryData)
  455. {
  456. MessageBase msg = ParseResponse(binaryData);
  457. ProceedTransactionMessage(msg);
  458. }
  459. private void _port_OnAsciiDataReceived(string oneLineMessage)
  460. {
  461. lock (_locker)
  462. {
  463. if (string.IsNullOrEmpty(_newLine))
  464. {
  465. _lstAsciiMsgs.AddLast(oneLineMessage);
  466. }
  467. else
  468. {
  469. foreach (var message in oneLineMessage.Split(_newLine.ToCharArray()))
  470. {
  471. if (!string.IsNullOrEmpty(message))
  472. _lstAsciiMsgs.AddLast(message + _newLine);
  473. }
  474. }
  475. }
  476. //lock (_locker)
  477. //{
  478. // _lstAsciiMsgs.AddLast(oneLineMessage);
  479. //}
  480. }
  481. private void _port_HandleAsciiData(string oneLineMessage)
  482. {
  483. MessageBase msg = ParseResponse(oneLineMessage);
  484. ProceedTransactionMessage(msg);
  485. }
  486. public HandlerBase MonitorTimeout()
  487. {
  488. HandlerBase result = null;
  489. lock (_lockerActiveHandler)
  490. {
  491. if (_activeHandler != null && _activeHandler.CheckTimeout())
  492. {
  493. //if (retryTime++ < 3) Retry();
  494. //else
  495. //{
  496. EV.PostWarningLog("System", $"{Address} receive {_activeHandler.Name} timeout");
  497. result = _activeHandler;
  498. HandlerInError = _activeHandler;
  499. _activeHandler = null;
  500. SetCommunicationError(true, "receive response timeout");
  501. //}
  502. }
  503. }
  504. return result;
  505. }
  506. public void Retry()
  507. {
  508. if (_activeHandler == null)
  509. return;
  510. if (_socket.IsConnected)
  511. {
  512. //_activeHandler = handler;
  513. _activeHandler.SetState(EnumHandlerState.Sent);
  514. bool sendResult = _isAsciiMode ? SendMessage(_activeHandler.SendText) : SendMessage(_activeHandler.SendBinary);
  515. if (!sendResult) _activeHandler = null;
  516. }
  517. }
  518. public void SetCommunicationError(bool isError, string reason)
  519. {
  520. IsCommunicationError = isError;
  521. LastCommunicationError = reason;
  522. }
  523. }
  524. }