SocketClient.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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 = "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 = "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 = 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 = "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 = "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. if (!headResult.Successed)
  264. {
  265. plcResult.SetValue(headResult);
  266. return plcResult;
  267. }
  268. if (!PLCModel.CheckHead(plc_m))
  269. {
  270. plcResult.Successed = false;
  271. plcResult.Message = "response head error";
  272. return plcResult;
  273. }
  274. // 响应报文-文本
  275. int dataLength = PLCModel.GetContentLength(plcResult.Data);
  276. if (dataLength > 0)
  277. {
  278. PLCResult<byte[]> dataResult = ReceiveByClient(dataLength);
  279. plcResult.Data.ContentBytes = dataResult.Data;
  280. plcResult.Data.Content = PLCModel.ToResponseFromReceive(dataResult.Data);
  281. if (!dataResult.Successed)
  282. {
  283. plcResult.SetValue(dataResult);
  284. return plcResult;
  285. }
  286. //PLCModel.SetValueFromCommand(plcResult.Data);
  287. }
  288. if (plcResult.Data.PLCError)
  289. {
  290. plcResult.Successed = false;
  291. plcResult.Message = plcResult.Data.Head;
  292. plcResult.MessageDetail = plcResult.Data.Content;
  293. }
  294. }
  295. catch (Exception ex)
  296. {
  297. //this.Disconnect();
  298. plcResult.Successed = false;
  299. plcResult.Message = ex.Message;
  300. plcResult.MessageDetail = ex.ToString();
  301. }
  302. finally
  303. {
  304. if (this.ThreadLock != null && this.ThreadLock.Locked)
  305. {
  306. this.ThreadLock.Unlock();
  307. }
  308. }
  309. return plcResult;
  310. }
  311. /// <summary>
  312. /// 执行命令
  313. /// </summary>
  314. /// <param name="func"></param>
  315. /// <returns></returns>
  316. public virtual PLCResult<PLCMessage> DoCommand(Func<PLCMessage> func)
  317. {
  318. return DoCommand(func());
  319. }
  320. #endregion
  321. #region Read
  322. /// <summary>
  323. /// 读取
  324. /// </summary>
  325. /// <param name="code"></param>
  326. /// <param name="number"></param>
  327. /// <param name="length"></param>
  328. /// <returns></returns>
  329. public virtual PLCResult<PLCMessage> Read(char code, int number, int length = 1)
  330. {
  331. return DoCommand(PLCModel.GetReadMessage<object>(code, number, length));
  332. }
  333. /// <summary>
  334. /// 读取
  335. /// </summary>
  336. /// <param name="code"></param>
  337. /// <param name="number"></param>
  338. /// <param name="length"></param>
  339. /// <returns></returns>
  340. public virtual PLCResult<PLCMessage> Read(string code, string number, int length = 1)
  341. {
  342. return DoCommand(PLCModel.GetReadMessage<object>(code, number, length));
  343. }
  344. /// <summary>
  345. /// 读取
  346. /// </summary>
  347. /// <param name="code"></param>
  348. /// <param name="length"></param>
  349. /// <returns></returns>
  350. public virtual PLCResult<PLCMessage> Read(string code, int length = 1)
  351. {
  352. return DoCommand(PLCModel.GetReadMessage<object>(code, length));
  353. }
  354. /// <summary>
  355. /// 读取
  356. /// </summary>
  357. /// <param name="code"></param>
  358. /// <param name="number"></param>
  359. /// <param name="length"></param>
  360. /// <returns></returns>
  361. public virtual PLCResult<TValue> Read<TValue>(char code, int number, int length = 1)
  362. {
  363. //PLCResult<PLCMessage<TValue>> plcResult = ReadValue<TValue>(code, number, length);
  364. //if (plcResult?.Successed ?? false)
  365. //{
  366. // return new PLCResult<TValue>(plcResult, plcResult.Data.Value);
  367. //}
  368. //return new PLCResult<TValue>(plcResult);
  369. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  370. if (plcResult.Successed)
  371. {
  372. PLCMessage<TValue> p_value = (PLCMessage<TValue>)plcResult.Data;
  373. PLCModel.SetReadValueFromBytes<TValue>(p_value);
  374. return new PLCResult<TValue>(plcResult, p_value.Value);
  375. }
  376. return new PLCResult<TValue>(plcResult);
  377. }
  378. /// <summary>
  379. /// 读取
  380. /// </summary>
  381. /// <param name="code"></param>
  382. /// <param name="number"></param>
  383. /// <param name="length"></param>
  384. /// <returns></returns>
  385. public virtual PLCResult<TValue> Read<TValue>(string code, string number, int length = 1)
  386. {
  387. //PLCResult<PLCMessage<TValue>> plcResult = ReadValue<TValue>(code, number, length);
  388. //if (plcResult?.Successed ?? false)
  389. //{
  390. // return new PLCResult<TValue>(plcResult, plcResult.Data.Value);
  391. //}
  392. //return new PLCResult<TValue>(plcResult);
  393. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  394. if (plcResult.Successed)
  395. {
  396. PLCMessage<TValue> p_value = (PLCMessage<TValue>)plcResult.Data;
  397. PLCModel.SetReadValueFromBytes<TValue>(p_value);
  398. return new PLCResult<TValue>(plcResult, p_value.Value);
  399. }
  400. return new PLCResult<TValue>(plcResult);
  401. }
  402. /// <summary>
  403. /// 读取
  404. /// </summary>
  405. /// <param name="code"></param>
  406. /// <param name="length"></param>
  407. /// <returns></returns>
  408. public virtual PLCResult<TValue> Read<TValue>(string code, int length = 1)
  409. {
  410. //PLCResult<PLCMessage<TValue>> plcResult = ReadValue<TValue>(code, length);
  411. //if (plcResult?.Successed ?? false)
  412. //{
  413. // return new PLCResult<TValue>(plcResult, plcResult.Data.Value);
  414. //}
  415. //return new PLCResult<TValue>(plcResult);
  416. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, length));
  417. if (plcResult.Successed)
  418. {
  419. PLCMessage<TValue> p_value = (PLCMessage<TValue>)plcResult.Data;
  420. PLCModel.SetReadValueFromBytes<TValue>(p_value);
  421. return new PLCResult<TValue>(plcResult, p_value.Value);
  422. }
  423. return new PLCResult<TValue>(plcResult);
  424. }
  425. /// <summary>
  426. /// 读取
  427. /// </summary>
  428. /// <param name="code"></param>
  429. /// <param name="number"></param>
  430. /// <param name="length"></param>
  431. /// <returns></returns>
  432. public virtual PLCResult<PLCMessage<TValue>> ReadValue<TValue>(char code, int number, int length = 1)
  433. {
  434. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  435. if (plcResult.Successed)
  436. {
  437. PLCModel.SetReadValueFromBytes<TValue>((PLCMessage<TValue>)plcResult.Data);
  438. }
  439. return new PLCResult<PLCMessage<TValue>>(plcResult, (PLCMessage<TValue>)plcResult.Data);
  440. }
  441. /// <summary>
  442. /// 读取
  443. /// </summary>
  444. /// <param name="code"></param>
  445. /// <param name="number"></param>
  446. /// <param name="length"></param>
  447. /// <returns></returns>
  448. public virtual PLCResult<PLCMessage<TValue>> ReadValue<TValue>(string code, string number, int length = 1)
  449. {
  450. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, number, length));
  451. if (plcResult.Successed)
  452. {
  453. PLCModel.SetReadValueFromBytes<TValue>((PLCMessage<TValue>)plcResult.Data);
  454. }
  455. return new PLCResult<PLCMessage<TValue>>(plcResult, (PLCMessage<TValue>)plcResult.Data);
  456. }
  457. /// <summary>
  458. /// 读取
  459. /// </summary>
  460. /// <param name="code"></param>
  461. /// <param name="length"></param>
  462. /// <returns></returns>
  463. public virtual PLCResult<PLCMessage<TValue>> ReadValue<TValue>(string code, int length = 1)
  464. {
  465. PLCResult<PLCMessage> plcResult = DoCommand(PLCModel.GetReadMessage<TValue>(code, length));
  466. if (plcResult.Successed)
  467. {
  468. PLCModel.SetReadValueFromBytes<TValue>((PLCMessage<TValue>)plcResult.Data);
  469. }
  470. return new PLCResult<PLCMessage<TValue>>(plcResult, (PLCMessage<TValue>)plcResult.Data);
  471. }
  472. #endregion
  473. #region Write
  474. /// <summary>
  475. /// 写入
  476. /// </summary>
  477. /// <typeparam name="TValue"></typeparam>
  478. /// <param name="code"></param>
  479. /// <param name="number"></param>
  480. /// <param name="value"></param>
  481. /// <param name="length"></param>
  482. /// <returns></returns>
  483. public virtual PLCResult Write<TValue>(char code, int number, TValue value, int length = 0)
  484. {
  485. return DoCommand(PLCModel.GetWriteMessage(code, number, value, length));
  486. }
  487. /// <summary>
  488. /// 写入
  489. /// </summary>
  490. /// <typeparam name="TValue"></typeparam>
  491. /// <param name="code"></param>
  492. /// <param name="number"></param>
  493. /// <param name="value"></param>
  494. /// <param name="length"></param>
  495. /// <returns></returns>
  496. public virtual PLCResult Write<TValue>(string code, string number, TValue value, int length = 0)
  497. {
  498. return DoCommand(PLCModel.GetWriteMessage(code, number, value, length));
  499. }
  500. /// <summary>
  501. /// 写入
  502. /// </summary>
  503. /// <typeparam name="TValue"></typeparam>
  504. /// <param name="code"></param>
  505. /// <param name="value"></param>
  506. /// <param name="length"></param>
  507. /// <returns></returns>
  508. public virtual PLCResult Write<TValue>(string code, TValue value, int length = 0)
  509. {
  510. return DoCommand(PLCModel.GetWriteMessage(code, value, length));
  511. }
  512. /// <summary>
  513. /// 写入
  514. /// </summary>
  515. /// <param name="code"></param>
  516. /// <param name="number"></param>
  517. /// <param name="value"></param>
  518. /// <param name="encoding"></param>
  519. /// <param name="length"></param>
  520. /// <returns></returns>
  521. public virtual PLCResult Write(char code, int number, string value, Encoding encoding, int length = 0)
  522. {
  523. return DoCommand(PLCModel.GetWriteMessage(code, number, value, encoding, length));
  524. }
  525. /// <summary>
  526. /// 写入
  527. /// </summary>
  528. /// <param name="code"></param>
  529. /// <param name="number"></param>
  530. /// <param name="value"></param>
  531. /// <param name="encoding"></param>
  532. /// <param name="length"></param>
  533. /// <returns></returns>
  534. public virtual PLCResult Write(string code, string number, string value, Encoding encoding, int length = 0)
  535. {
  536. return DoCommand(PLCModel.GetWriteMessage(code, number, value, encoding, length));
  537. }
  538. /// <summary>
  539. /// 写入
  540. /// </summary>
  541. /// <param name="code"></param>
  542. /// <param name="value"></param>
  543. /// <param name="encoding"></param>
  544. /// <param name="length"></param>
  545. /// <returns></returns>
  546. public virtual PLCResult Write(string code, string value, Encoding encoding, int length = 0)
  547. {
  548. return DoCommand(PLCModel.GetWriteMessage(code, value, encoding, length));
  549. }
  550. #endregion
  551. #region Send
  552. /// <summary>
  553. /// 发送
  554. /// </summary>
  555. /// <param name="value"></param>
  556. /// <returns></returns>
  557. public virtual PLCResult Send(string value)
  558. {
  559. return DoCommandOneWay(PLCModel.GetSendMessage(value));
  560. }
  561. /// <summary>
  562. /// 发送
  563. /// </summary>
  564. /// <param name="value"></param>
  565. /// <returns></returns>
  566. public virtual PLCResult<PLCMessage> SendByResult(string value)
  567. {
  568. return DoCommand(PLCModel.GetSendMessage(value));
  569. }
  570. #endregion
  571. }
  572. }