ConnectionBase.cs 19 KB

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