SocketClient.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. 
  2. using System;
  3. using System.Text;
  4. using Curtain.Net.Sockets.PLC.Model;
  5. namespace Curtain.Net.Sockets.PLC
  6. {
  7. /// <summary>
  8. /// Socket 客户端
  9. /// </summary>
  10. public class SocketClient<TClientModel> : PLCSocket
  11. where TClientModel : IClientModel, new()
  12. {
  13. #region 属性
  14. /// <summary>
  15. /// 通信(接收)超时(毫秒)
  16. /// </summary>
  17. public int ReceiveTimeout
  18. {
  19. get; set;
  20. }
  21. /// <summary>
  22. /// 通信(发送)超时(毫秒)
  23. /// </summary>
  24. public int SendTimeout
  25. {
  26. get; set;
  27. }
  28. /// <summary>
  29. /// 主机地址
  30. /// </summary>
  31. public string Host
  32. {
  33. get; protected set;
  34. }
  35. /// <summary>
  36. /// 主机端口
  37. /// </summary>
  38. public int Port
  39. {
  40. get; protected set;
  41. }
  42. /// <summary>
  43. /// PLC通信模型
  44. /// </summary>
  45. public TClientModel PLCModel
  46. {
  47. get; protected set;
  48. }
  49. #endregion
  50. #region 构造
  51. /// <summary>
  52. /// Socket 客户端
  53. /// </summary>
  54. /// <param name="newModel">新实例</param>
  55. public SocketClient(bool newModel = false)
  56. {
  57. if (newModel)
  58. {
  59. PLCModel = new TClientModel();
  60. }
  61. else
  62. {
  63. PLCModel = Model.ClientModel.CreateModel<TClientModel>();
  64. }
  65. this.ReceiveTimeout = this.PLCModel.ReceiveTimeout;
  66. this.SendTimeout = this.PLCModel.SendTimeout;
  67. }
  68. /// <summary>
  69. /// Socket 客户端
  70. /// </summary>
  71. /// <param name="model">新实例</param>
  72. public SocketClient(TClientModel model)
  73. {
  74. if (model == null)
  75. {
  76. throw new NullReferenceException("ClientSocket TPLCModel");
  77. }
  78. PLCModel = model;
  79. this.ReceiveTimeout = this.PLCModel.ReceiveTimeout;
  80. this.SendTimeout = this.PLCModel.SendTimeout;
  81. }
  82. /// <summary>
  83. /// Socket 客户端
  84. /// </summary>
  85. /// <param name="host">主机地址</param>
  86. /// <param name="port">主机端口</param>
  87. /// <param name="newModel">新实例</param>
  88. public SocketClient(string host, int port, bool newModel = false)
  89. : this(newModel)
  90. {
  91. Host = host;
  92. Port = port;
  93. }
  94. /// <summary>
  95. /// Socket 客户端
  96. /// </summary>
  97. /// <param name="host">主机地址</param>
  98. /// <param name="port">主机端口</param>
  99. /// <param name="model">新实例</param>
  100. public SocketClient(string host, int port, TClientModel model)
  101. : this(model)
  102. {
  103. Host = host;
  104. Port = port;
  105. }
  106. #endregion
  107. #region Connect
  108. /// <summary>
  109. /// 连接PLC
  110. /// </summary>
  111. public virtual void Connect()
  112. {
  113. base.Connect(Host, Port);
  114. }
  115. /// <summary>
  116. /// 连接PLC
  117. /// </summary>
  118. /// <param name="host">主机地址</param>
  119. /// <param name="port">主机端口</param>
  120. public override void Connect(string host, int port)
  121. {
  122. Host = host;
  123. Port = port;
  124. this.Connect();
  125. }
  126. /// <summary>
  127. /// Socket连接后PLC初始化
  128. /// </summary>
  129. public override void OnSocketConnect()
  130. {
  131. PLCModel.OnSocketConnect(this);
  132. }
  133. #endregion
  134. #region Command
  135. /// <summary>
  136. /// 发送命令报文
  137. /// </summary>
  138. /// <param name="data"></param>
  139. /// <returns></returns>
  140. protected virtual PLCResult SendByClient(byte[] data)
  141. {
  142. this.Socket.SendTimeout = this.SendTimeout;
  143. //return BeginSend(data);
  144. return Send(data);
  145. }
  146. /// <summary>
  147. /// 接收响应报文
  148. /// </summary>
  149. /// <param name="length"></param>
  150. /// <returns></returns>
  151. protected virtual PLCResult<byte[]> ReceiveByClient(int length)
  152. {
  153. this.Socket.ReceiveTimeout = this.ReceiveTimeout;
  154. //return BeginReceive(length);
  155. return Receive(length);
  156. }
  157. /// <summary>
  158. /// 执行命令(不等待响应)
  159. /// </summary>
  160. /// <param name="plc_m">报文</param>
  161. /// <returns></returns>
  162. public virtual PLCResult<PLCMessage> DoCommandOneWay(PLCMessage plc_m)
  163. {
  164. PLCResult<PLCMessage> plcResult = new PLCResult<PLCMessage>(plc_m);
  165. if (plc_m == null)
  166. {
  167. plcResult.Successed = false;
  168. plcResult.Message = "c:do command oneway no plc message";
  169. return plcResult;
  170. }
  171. plc_m.Host = Host;
  172. plc_m.Port = Port;
  173. try
  174. {
  175. if (plc_m.CommandBytes == null ||
  176. plc_m.CommandBytes.Length == 0)
  177. {
  178. if (string.IsNullOrEmpty(plc_m.Command))
  179. {
  180. plcResult.Successed = false;
  181. plcResult.Message = "c:do command oneway no command";
  182. return plcResult;
  183. }
  184. else
  185. {
  186. plc_m.CommandBytes = PLCModel.ToSendFromCommand(plc_m.Command);
  187. }
  188. }
  189. this.ThreadLock?.Lock();
  190. this.Connect();
  191. PLCResult result = SendByClient(plcResult.Data.CommandBytes);
  192. if (!result.Successed)
  193. {
  194. plcResult.SetValue(result);
  195. return plcResult;
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. //this.Disconnect();
  201. plcResult.Successed = false;
  202. plcResult.Message = "c:do command oneway " + ex.Message;
  203. plcResult.MessageDetail = ex.ToString();
  204. }
  205. finally
  206. {
  207. if (this.ThreadLock != null && this.ThreadLock.Locked)
  208. {
  209. this.ThreadLock.Unlock();
  210. }
  211. }
  212. return plcResult;
  213. }
  214. /// <summary>
  215. /// 执行命令
  216. /// </summary>
  217. /// <param name="plc_m">报文</param>
  218. /// <returns></returns>
  219. public virtual PLCResult<PLCMessage> DoCommand(PLCMessage plc_m)
  220. {
  221. PLCResult<PLCMessage> plcResult = new PLCResult<PLCMessage>(plc_m);
  222. if (plc_m == null)
  223. {
  224. plcResult.Successed = false;
  225. plcResult.Message = "c:do command no plc message";
  226. return plcResult;
  227. }
  228. plc_m.Host = Host;
  229. plc_m.Port = Port;
  230. try
  231. {
  232. if (plc_m.CommandBytes == null ||
  233. plc_m.CommandBytes.Length == 0)
  234. {
  235. if (string.IsNullOrEmpty(plc_m.Command))
  236. {
  237. plcResult.Successed = false;
  238. plcResult.Message = "c:do command no command";
  239. return plcResult;
  240. }
  241. else
  242. {
  243. plc_m.CommandBytes = PLCModel.ToSendFromCommand(plc_m.Command);
  244. }
  245. }
  246. this.ThreadLock?.Lock();
  247. this.Connect();
  248. PLCResult result = SendByClient(plcResult.Data.CommandBytes);
  249. if (!result.Successed)
  250. {
  251. plcResult.SetValue(result);
  252. return plcResult;
  253. }
  254. // 响应报文-头
  255. int headLength = PLCModel.HeadLength;
  256. if (headLength < 1)
  257. {
  258. return plcResult;
  259. }
  260. PLCResult<byte[]> headResult = ReceiveByClient(headLength);
  261. plcResult.Data.HeadBytes = headResult.Data;
  262. plcResult.Data.Head = PLCModel.ToResponseFromReceive(headResult.Data);
  263. plcResult.Message = plcResult.Data.Head;
  264. if (!headResult.Successed)
  265. {
  266. plcResult.SetValue(headResult);
  267. return plcResult;
  268. }
  269. if (!PLCModel.CheckHead(plc_m))
  270. {
  271. plcResult.Successed = false;
  272. plcResult.Message = "c:do command response head error";
  273. return plcResult;
  274. }
  275. // 响应报文-文本
  276. int dataLength = PLCModel.GetContentLength(plcResult.Data);
  277. if (dataLength > 0)
  278. {
  279. PLCResult<byte[]> dataResult = ReceiveByClient(dataLength);
  280. plcResult.Data.ContentBytes = dataResult.Data;
  281. plcResult.Data.Content = PLCModel.ToResponseFromReceive(dataResult.Data);
  282. plcResult.MessageDetail = plcResult.Data.Content;
  283. if (!dataResult.Successed)
  284. {
  285. plcResult.SetValue(dataResult);
  286. return plcResult;
  287. }
  288. //PLCModel.SetValueFromCommand(plcResult.Data);
  289. }
  290. if (plcResult.Data.PLCError)
  291. {
  292. plcResult.Successed = false;
  293. //plcResult.Message = plcResult.Data.Head;
  294. //plcResult.MessageDetail = plcResult.Data.Content;
  295. }
  296. }
  297. catch (Exception ex)
  298. {
  299. //this.Disconnect();
  300. plcResult.Successed = false;
  301. plcResult.Message = "c:do command " + ex.Message;
  302. plcResult.MessageDetail = ex.ToString();
  303. }
  304. finally
  305. {
  306. if (this.ThreadLock != null && this.ThreadLock.Locked)
  307. {
  308. this.ThreadLock.Unlock();
  309. }
  310. }
  311. return plcResult;
  312. }
  313. /// <summary>
  314. /// 执行命令
  315. /// </summary>
  316. /// <param name="func"></param>
  317. /// <returns></returns>
  318. public virtual PLCResult<PLCMessage> DoCommand(Func<PLCMessage> func)
  319. {
  320. return DoCommand(func());
  321. }
  322. #endregion
  323. #region Read
  324. /// <summary>
  325. /// 读取
  326. /// </summary>
  327. /// <param name="code"></param>
  328. /// <param name="number"></param>
  329. /// <param name="length"></param>
  330. /// <returns></returns>
  331. public virtual PLCResult<PLCMessage> Read(char code, int number, int length = 1)
  332. {
  333. return DoCommand(PLCModel.GetReadMessage<object>(code, number, length));
  334. }
  335. /// <summary>
  336. /// 读取
  337. /// </summary>
  338. /// <param name="code"></param>
  339. /// <param name="number"></param>
  340. /// <param name="length"></param>
  341. /// <returns></returns>
  342. public virtual PLCResult<PLCMessage> Read(string code, string number, int length = 1)
  343. {
  344. return DoCommand(PLCModel.GetReadMessage<object>(code, number, length));
  345. }
  346. /// <summary>
  347. /// 读取
  348. /// </summary>
  349. /// <param name="code"></param>
  350. /// <param name="length"></param>
  351. /// <returns></returns>
  352. public virtual PLCResult<PLCMessage> Read(string code, int length = 1)
  353. {
  354. return DoCommand(PLCModel.GetReadMessage<object>(code, length));
  355. }
  356. /// <summary>
  357. /// 读取
  358. /// </summary>
  359. /// <param name="code"></param>
  360. /// <param name="number"></param>
  361. /// <param name="length"></param>
  362. /// <returns></returns>
  363. public virtual PLCResult<TValue> Read<TValue>(char code, int number, int length = 1)
  364. {
  365. //PLCResult<PLCMessage<TValue>> plcResult = ReadValue<TValue>(code, number, length);
  366. //if (plcResult?.Successed ?? false)
  367. //{
  368. // return new PLCResult<TValue>(plcResult, plcResult.Data.Value);
  369. //}
  370. //return new PLCResult<TValue>(plcResult);
  371. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  372. if (plcResult.Successed)
  373. {
  374. PLCMessage<TValue> p_value = (PLCMessage<TValue>)plcResult.Data;
  375. PLCModel.SetReadValueFromBytes<TValue>(p_value);
  376. return new PLCResult<TValue>(plcResult, p_value.Value);
  377. }
  378. return new PLCResult<TValue>(plcResult);
  379. }
  380. /// <summary>
  381. /// 读取
  382. /// </summary>
  383. /// <param name="code"></param>
  384. /// <param name="number"></param>
  385. /// <param name="length"></param>
  386. /// <returns></returns>
  387. public virtual PLCResult<TValue> Read<TValue>(string code, string number, int length = 1)
  388. {
  389. //PLCResult<PLCMessage<TValue>> plcResult = ReadValue<TValue>(code, number, length);
  390. //if (plcResult?.Successed ?? false)
  391. //{
  392. // return new PLCResult<TValue>(plcResult, plcResult.Data.Value);
  393. //}
  394. //return new PLCResult<TValue>(plcResult);
  395. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  396. if (plcResult.Successed)
  397. {
  398. PLCMessage<TValue> p_value = (PLCMessage<TValue>)plcResult.Data;
  399. PLCModel.SetReadValueFromBytes<TValue>(p_value);
  400. return new PLCResult<TValue>(plcResult, p_value.Value);
  401. }
  402. return new PLCResult<TValue>(plcResult);
  403. }
  404. /// <summary>
  405. /// 读取
  406. /// </summary>
  407. /// <param name="code"></param>
  408. /// <param name="number"></param>
  409. /// <param name="encoding"></param>
  410. /// <param name="length"></param>
  411. /// <returns></returns>
  412. public virtual PLCResult<string> ReadString(string code, string number, Encoding encoding, int length = 1)
  413. {
  414. //PLCResult<PLCMessage<TValue>> plcResult = ReadValue<TValue>(code, number, length);
  415. //if (plcResult?.Successed ?? false)
  416. //{
  417. // return new PLCResult<TValue>(plcResult, plcResult.Data.Value);
  418. //}
  419. //return new PLCResult<TValue>(plcResult);
  420. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<string>(code, number, length));
  421. if (plcResult.Successed)
  422. {
  423. PLCMessage<string> p_value = (PLCMessage<string>)plcResult.Data;
  424. PLCModel.SetReadValueFromBytes<string>(p_value, encoding);
  425. return new PLCResult<string>(plcResult, p_value.Value);
  426. }
  427. return new PLCResult<string>(plcResult);
  428. }
  429. /// <summary>
  430. /// 读取
  431. /// </summary>
  432. /// <param name="code"></param>
  433. /// <param name="length"></param>
  434. /// <returns></returns>
  435. public virtual PLCResult<TValue> Read<TValue>(string code, int length = 1)
  436. {
  437. //PLCResult<PLCMessage<TValue>> plcResult = ReadValue<TValue>(code, length);
  438. //if (plcResult?.Successed ?? false)
  439. //{
  440. // return new PLCResult<TValue>(plcResult, plcResult.Data.Value);
  441. //}
  442. //return new PLCResult<TValue>(plcResult);
  443. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, length));
  444. if (plcResult.Successed)
  445. {
  446. PLCMessage<TValue> p_value = (PLCMessage<TValue>)plcResult.Data;
  447. PLCModel.SetReadValueFromBytes<TValue>(p_value);
  448. return new PLCResult<TValue>(plcResult, p_value.Value);
  449. }
  450. return new PLCResult<TValue>(plcResult);
  451. }
  452. /// <summary>
  453. /// 读取
  454. /// </summary>
  455. /// <param name="code"></param>
  456. /// <param name="number"></param>
  457. /// <param name="length"></param>
  458. /// <returns></returns>
  459. public virtual PLCResult<PLCMessage<TValue>> ReadValue<TValue>(char code, int number, int length = 1)
  460. {
  461. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  462. if (plcResult.Successed)
  463. {
  464. PLCModel.SetReadValueFromBytes<TValue>((PLCMessage<TValue>)plcResult.Data);
  465. }
  466. return new PLCResult<PLCMessage<TValue>>(plcResult, (PLCMessage<TValue>)plcResult.Data);
  467. }
  468. /// <summary>
  469. /// 读取
  470. /// </summary>
  471. /// <param name="code"></param>
  472. /// <param name="number"></param>
  473. /// <param name="length"></param>
  474. /// <returns></returns>
  475. public virtual PLCResult<PLCMessage<TValue>> ReadValue<TValue>(string code, string number, int length = 1)
  476. {
  477. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  478. if (plcResult.Successed)
  479. {
  480. PLCModel.SetReadValueFromBytes<TValue>((PLCMessage<TValue>)plcResult.Data);
  481. }
  482. return new PLCResult<PLCMessage<TValue>>(plcResult, (PLCMessage<TValue>)plcResult.Data);
  483. }
  484. /// <summary>
  485. /// 读取
  486. /// </summary>
  487. /// <param name="code"></param>
  488. /// <param name="length"></param>
  489. /// <returns></returns>
  490. public virtual PLCResult<PLCMessage<TValue>> ReadValue<TValue>(string code, int length = 1)
  491. {
  492. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, length));
  493. if (plcResult.Successed)
  494. {
  495. PLCModel.SetReadValueFromBytes<TValue>((PLCMessage<TValue>)plcResult.Data);
  496. }
  497. return new PLCResult<PLCMessage<TValue>>(plcResult, (PLCMessage<TValue>)plcResult.Data);
  498. }
  499. #endregion
  500. #region Write
  501. /// <summary>
  502. /// 写入
  503. /// </summary>
  504. /// <typeparam name="TValue"></typeparam>
  505. /// <param name="code"></param>
  506. /// <param name="number"></param>
  507. /// <param name="value"></param>
  508. /// <param name="length"></param>
  509. /// <returns></returns>
  510. public virtual PLCResult Write<TValue>(char code, int number, TValue value, int length = 0)
  511. {
  512. return DoCommand(PLCModel.GetWriteMessage(code, number, value, length));
  513. }
  514. /// <summary>
  515. /// 写入
  516. /// </summary>
  517. /// <typeparam name="TValue"></typeparam>
  518. /// <param name="code"></param>
  519. /// <param name="number"></param>
  520. /// <param name="value"></param>
  521. /// <param name="length"></param>
  522. /// <returns></returns>
  523. public virtual PLCResult Write<TValue>(string code, string number, TValue value, int length = 0)
  524. {
  525. return DoCommand(PLCModel.GetWriteMessage(code, number, value, length));
  526. }
  527. /// <summary>
  528. /// 写入
  529. /// </summary>
  530. /// <typeparam name="TValue"></typeparam>
  531. /// <param name="code"></param>
  532. /// <param name="value"></param>
  533. /// <param name="length"></param>
  534. /// <returns></returns>
  535. public virtual PLCResult Write<TValue>(string code, TValue value, int length = 0)
  536. {
  537. return DoCommand(PLCModel.GetWriteMessage(code, value, length));
  538. }
  539. /// <summary>
  540. /// 写入
  541. /// </summary>
  542. /// <param name="code"></param>
  543. /// <param name="number"></param>
  544. /// <param name="value"></param>
  545. /// <param name="encoding"></param>
  546. /// <param name="length"></param>
  547. /// <returns></returns>
  548. public virtual PLCResult Write(char code, int number, string value, Encoding encoding, int length = 0)
  549. {
  550. return DoCommand(PLCModel.GetWriteMessage(code, number, value, encoding, length));
  551. }
  552. /// <summary>
  553. /// 写入
  554. /// </summary>
  555. /// <param name="code"></param>
  556. /// <param name="number"></param>
  557. /// <param name="value"></param>
  558. /// <param name="encoding"></param>
  559. /// <param name="length"></param>
  560. /// <returns></returns>
  561. public virtual PLCResult Write(string code, string number, string value, Encoding encoding, int length = 0)
  562. {
  563. return DoCommand(PLCModel.GetWriteMessage(code, number, value, encoding, length));
  564. }
  565. /// <summary>
  566. /// 写入
  567. /// </summary>
  568. /// <param name="code"></param>
  569. /// <param name="value"></param>
  570. /// <param name="encoding"></param>
  571. /// <param name="length"></param>
  572. /// <returns></returns>
  573. public virtual PLCResult Write(string code, string value, Encoding encoding, int length = 0)
  574. {
  575. return DoCommand(PLCModel.GetWriteMessage(code, value, encoding, length));
  576. }
  577. #endregion
  578. #region Send
  579. /// <summary>
  580. /// 发送
  581. /// </summary>
  582. /// <param name="value"></param>
  583. /// <returns></returns>
  584. public virtual PLCResult Send(string value)
  585. {
  586. return DoCommandOneWay(PLCModel.GetSendMessage(value));
  587. }
  588. /// <summary>
  589. /// 发送
  590. /// </summary>
  591. /// <param name="value"></param>
  592. /// <returns></returns>
  593. public virtual PLCResult<PLCMessage> SendByResult(string value)
  594. {
  595. return DoCommand(PLCModel.GetSendMessage(value));
  596. }
  597. #endregion
  598. }
  599. }