EpsonRobot.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815
  1. using MahApps.Metro.Controls;
  2. using MathNet.Numerics;
  3. using OxyPlot.Axes;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Net;
  9. using System.Runtime.CompilerServices;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using TeamAAS_VP.Enums;
  13. using TeamAAS_VP.Interfaces;
  14. using TeamAAS_VP.Models;
  15. using TeamAAS_VP;
  16. using TouchSocket.Core;
  17. using TouchSocket.Sockets;
  18. using static NPOI.HSSF.Util.HSSFColor;
  19. using static TeamAAS_VP.Core.CoordinateTransformer;
  20. using Opc.Ua;
  21. using TeamAAS_VP.Models.Product;
  22. namespace TeamAAS_VP.Core.Robots
  23. {
  24. public class EpsonRobot:IRobot,INotifyPropertyChanged
  25. {
  26. private IWaitingClient<ITcpClient, IReceiverResult> WaitClient;
  27. private IWaitingClient<ITcpSessionClient, IReceiverResult> WaitClientServer;
  28. //private bool isFirstConnect=true;
  29. private Task bgConnectTask;
  30. public event Action<Guid, object, ConnectedEventArgs> ConnectedEvent;
  31. public event Action<Guid, object, ClosedEventArgs> DisconnectedEvent;
  32. public event Action<Guid, object, ReceivedDataEventArgs> ReceivedEvent;
  33. public event Action<Guid, object, string> SendEvent;
  34. public EpsonRobot(RobotInfo robot)
  35. {
  36. Id = robot.Id;
  37. Name= robot.RobotName;
  38. RobotNo = robot.RobotNo;
  39. RobotIp = robot.IP;
  40. RobotPort = robot.Port;
  41. ConnectType = robot.ConnectType;
  42. Terminator= robot.Terminator;
  43. DataEncoding = robot.DataEncoding;
  44. Brand = robot.RobotBrand;
  45. if (ConnectType== TCPConnectType.Client)
  46. {
  47. TcpClient = new TouchSocket.Sockets.TcpClient();
  48. var config = new TouchSocketConfig();
  49. config.SetRemoteIPHost(new IPHost(IPAddress.Parse(RobotIp), RobotPort));
  50. config.ConfigurePlugins(a => { a.UseTcpReconnection(); }); ////如需永远尝试连接,tryCount设置为-1即可。
  51. ////设置结束符
  52. if (Terminator == Terminator.None)
  53. {
  54. config.SetTcpDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); }); ////亦或者省略\r\n,但此时调用方不能高速调用,会粘包
  55. }
  56. else if (Terminator == Terminator.CR)
  57. {
  58. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r"); }); //命令行中使用\r结尾
  59. }
  60. else if (Terminator == Terminator.LF)
  61. {
  62. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\n"); }); //命令行中使用\n结尾
  63. }
  64. else if (Terminator == Terminator.CRLF)
  65. {
  66. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r\n"); }); //命令行中使用\r\n结尾
  67. }
  68. //载入配置
  69. TcpClient.Setup(config);
  70. ////调用CreateWaitingClient获取到IWaitingClient的对象。
  71. WaitClient = TcpClient.CreateWaitingClient(new WaitingOptions()
  72. {
  73. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  74. {
  75. return true;
  76. //if (response.Data.Length == 1)
  77. //{
  78. // return true;
  79. //}
  80. //return false;
  81. }
  82. });
  83. TcpClient.Connected = ConnectedVoid;//成功连接到服务器
  84. //有客户端断开连接
  85. TcpClient.Closed = DisconnectedVoid;
  86. //从客户端收到信息
  87. TcpClient.Received = ReceivedVoid;
  88. }
  89. else
  90. {
  91. TcpService = new TcpService();
  92. var config = new TouchSocketConfig();
  93. config.SetListenIPHosts(new IPHost[] { new IPHost($"{RobotIp}:{RobotPort}"),new IPHost(RobotPort+1) }); //同时监听两个地址
  94. ////设置结束符
  95. if (Terminator == Terminator.None)
  96. {
  97. config.SetTcpDataHandlingAdapter(() => { return new NormalDataHandlingAdapter(); }); ////亦或者省略\r\n,但此时调用方不能高速调用,会粘包
  98. }
  99. else if (Terminator == Terminator.CR)
  100. {
  101. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r"); }); //命令行中使用\r结尾
  102. }
  103. else if (Terminator == Terminator.LF)
  104. {
  105. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\n"); }); //命令行中使用\n结尾
  106. }
  107. else if (Terminator == Terminator.CRLF)
  108. {
  109. config.SetTcpDataHandlingAdapter(() => { return new TerminatorPackageAdapter("\r\n"); }); //命令行中使用\r\n结尾
  110. }
  111. //载入配置
  112. TcpService.Setup(config);
  113. /////有客户端成功连接
  114. TcpService.Connected = ConnectedVoid;
  115. //有客户端断开连接
  116. TcpService.Closed = DisconnectedVoid;
  117. //从客户端收到信息
  118. TcpService.Received = ReceivedVoid;
  119. }
  120. }
  121. #region 属性
  122. public TcpClient TcpClient { get; private set; }
  123. public TcpService TcpService { get; private set; }
  124. public Guid Id { get; set; }
  125. public string Name { get; set; }
  126. /// <summary>
  127. /// 机器人编号
  128. /// </summary>
  129. public int RobotNo { get; set; }
  130. public int RobotPort { get;private set; }
  131. public string RobotIp { get; private set; }
  132. public TCPConnectType ConnectType { get; private set; }
  133. public Terminator Terminator { get; private set; }
  134. public DataEncoding DataEncoding { get; private set; }
  135. public bool IsConnected { get {
  136. if(ConnectType == TCPConnectType.Client)
  137. {
  138. return TcpClient.Online;
  139. }
  140. else
  141. {
  142. if (TcpService.Count > 0)
  143. return true;
  144. else return false;
  145. }
  146. } }
  147. public int Timeout { get; set; } = 5000;
  148. private bool _CanExecute=false;
  149. public bool CanExecute
  150. {
  151. get { return _CanExecute; }
  152. set { SetProperty(ref _CanExecute, value); }
  153. }
  154. public int SelectedTool { get; private set; } = 0;
  155. public RobotBrand Brand { get;private set; }
  156. /// <summary>
  157. /// 进入调试模式
  158. /// </summary>
  159. /// <returns></returns>
  160. public bool EnterDebugMode { get; set; }
  161. private RPoint _CurrentPosition=new RPoint();
  162. /// <summary>
  163. /// 当前位置
  164. /// </summary>
  165. public RPoint CurrentPosition
  166. {
  167. get { return _CurrentPosition; }
  168. set { SetProperty(ref _CurrentPosition, value); }
  169. }
  170. private double _SafeZ;
  171. /// <summary>
  172. /// Z轴的安全高度
  173. /// </summary>
  174. public double SafeZ
  175. {
  176. get { return _SafeZ; }
  177. set { SetProperty(ref _SafeZ, value); }
  178. }
  179. #endregion
  180. #region 事件接收
  181. //-----------------------------------------------服务器---------------------------------------------
  182. /// <summary>
  183. /// 有客户端成功连接
  184. /// </summary>
  185. /// <param name="client"></param>
  186. /// <param name="e"></param>
  187. /// <returns></returns>
  188. private Task ConnectedVoid(ITcpSessionClient client, ConnectedEventArgs e)
  189. {
  190. CanExecute = true;
  191. //调用CreateWaitingClient获取到IWaitingClient的对象。
  192. WaitClientServer = client.CreateWaitingClient(new WaitingOptions()
  193. {
  194. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  195. {
  196. return true;
  197. //if (response.Data.Length == 1)
  198. //{
  199. // return true;
  200. //}
  201. //return false;
  202. }
  203. });
  204. ConnectedEvent?.Invoke(this.Id, client, e);
  205. return EasyTask.CompletedTask;
  206. }
  207. /// <summary>
  208. /// 有客户端断开连接
  209. /// </summary>
  210. /// <param name="client"></param>
  211. /// <param name="e"></param>
  212. /// <returns></returns>
  213. private Task DisconnectedVoid(ITcpSessionClient client, ClosedEventArgs e)
  214. {
  215. if (TcpService.Count<1)
  216. {
  217. CanExecute = false;
  218. }
  219. DisconnectedEvent?.Invoke(this.Id, client, e);
  220. if (WaitClientServer!=null)
  221. {
  222. if (WaitClientServer.Client.Id== client.Id)
  223. {
  224. if (TcpService.Count>0)
  225. {
  226. WaitClientServer = ((ITcpSessionClient)(TcpService.Clients.First())).CreateWaitingClient(new WaitingOptions()
  227. {
  228. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  229. {
  230. return true;
  231. //if (response.Data.Length == 1)
  232. //{
  233. // return true;
  234. //}
  235. //return false;
  236. }
  237. });
  238. }
  239. }
  240. }
  241. return EasyTask.CompletedTask;
  242. }
  243. /// <summary>
  244. /// 从客户端收到信息
  245. /// </summary>
  246. /// <param name="client"></param>
  247. /// <param name="e"></param>
  248. /// <returns></returns>
  249. private Task ReceivedVoid(ITcpSessionClient client, ReceivedDataEventArgs e)
  250. {
  251. ReceivedEvent?.Invoke(this.Id, client, e);
  252. //调用CreateWaitingClient获取到IWaitingClient的对象。
  253. WaitClientServer = client.CreateWaitingClient(new WaitingOptions()
  254. {
  255. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  256. {
  257. return true;
  258. //if (response.Data.Length == 1)
  259. //{
  260. // return true;
  261. //}
  262. //return false;
  263. }
  264. });
  265. return EasyTask.CompletedTask;
  266. }
  267. //---------------------------------------------客户端--------------------------------------------
  268. /// <summary>
  269. /// 连接到服务器时
  270. /// </summary>
  271. /// <param name="client"></param>
  272. /// <param name="e"></param>
  273. /// <returns></returns>
  274. private Task ConnectedVoid(ITcpClient client, ConnectedEventArgs e)
  275. {
  276. CanExecute = true;
  277. ConnectedEvent?.Invoke(this.Id, client, e);
  278. return EasyTask.CompletedTask;
  279. }
  280. /// <summary>
  281. /// 有客户端断开连接
  282. /// </summary>
  283. /// <param name="client"></param>
  284. /// <param name="e"></param>
  285. /// <returns></returns>
  286. private Task DisconnectedVoid(ITcpClient client, ClosedEventArgs e)
  287. {
  288. CanExecute = false;
  289. DisconnectedEvent?.Invoke(this.Id, client, e);
  290. return EasyTask.CompletedTask;
  291. }
  292. /// <summary>
  293. /// 从客户端收到信息
  294. /// </summary>
  295. /// <param name="client"></param>
  296. /// <param name="e"></param>
  297. /// <returns></returns>
  298. private Task ReceivedVoid(ITcpClient client, ReceivedDataEventArgs e)
  299. {
  300. ReceivedEvent?.Invoke(this.Id, client, e);
  301. return EasyTask.CompletedTask;
  302. }
  303. #endregion
  304. #region 连接
  305. public void Connect()
  306. {
  307. if (ConnectType == TCPConnectType.Client)
  308. {
  309. try
  310. {
  311. TcpClient.Connect();
  312. //isFirstConnect = false;
  313. }
  314. catch (Exception)
  315. {
  316. if (bgConnectTask==null)
  317. {
  318. bgConnectTask = Task.Run(async () =>
  319. {
  320. while (true)
  321. {
  322. try
  323. {
  324. TcpClient.Connect();
  325. //isFirstConnect = false;
  326. return;
  327. }
  328. catch (Exception)
  329. {
  330. await Task.Delay(1000);
  331. }
  332. }
  333. });
  334. }
  335. throw;
  336. }
  337. }
  338. else
  339. {
  340. TcpService.Start();
  341. }
  342. }
  343. public async Task ConnectAsync()
  344. {
  345. if (ConnectType == TCPConnectType.Client)
  346. {
  347. try
  348. {
  349. await TcpClient.ConnectAsync();
  350. //isFirstConnect = false;
  351. }
  352. catch (Exception)
  353. {
  354. if (bgConnectTask == null)
  355. {
  356. bgConnectTask = Task.Run(async () =>
  357. {
  358. while (true)
  359. {
  360. try
  361. {
  362. TcpClient.Connect();
  363. //isFirstConnect = false;
  364. return;
  365. }
  366. catch (Exception)
  367. {
  368. await Task.Delay(1000);
  369. }
  370. }
  371. });
  372. }
  373. throw;
  374. }
  375. }
  376. else
  377. {
  378. await TcpService.StartAsync();
  379. }
  380. }
  381. public void Disconnect()
  382. {
  383. if(TcpClient!=null)
  384. {
  385. TcpClient.Close();
  386. }
  387. if (TcpService != null)
  388. {
  389. TcpService.Stop();
  390. }
  391. }
  392. public void Dispose()
  393. {
  394. if (TcpClient != null)
  395. {
  396. TcpClient.Dispose();
  397. }
  398. if (TcpService != null)
  399. {
  400. TcpService.Dispose();
  401. }
  402. }
  403. #endregion
  404. #region 控制
  405. public bool Reset()
  406. {
  407. try
  408. {
  409. CanExecute = false;
  410. string rev = SendAndReceive($"Reset");
  411. if (rev.Contains("Reset"))
  412. return true;
  413. else
  414. return false;
  415. }
  416. catch (Exception ex)
  417. {
  418. LogHelper.WriteLogError("执行机器人命令时出错!",ex);
  419. CanExecute = true;
  420. throw ex;
  421. }
  422. finally
  423. {
  424. CanExecute = true;
  425. }
  426. }
  427. public async Task<bool> ResetAsync()
  428. {
  429. try
  430. {
  431. CanExecute = false;
  432. string rev = await SendAndReceiveAsync($"Reset");
  433. if (rev.Contains("Reset"))
  434. return true;
  435. else
  436. return false;
  437. }
  438. catch (Exception ex)
  439. {
  440. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  441. CanExecute = true;
  442. throw ex;
  443. }
  444. finally
  445. {
  446. CanExecute = true;
  447. }
  448. }
  449. public bool Motor(bool state)
  450. {
  451. try
  452. {
  453. CanExecute = false;
  454. int v = state ? 1 : 0;
  455. string rev = SendAndReceive($"Motor,{v}");
  456. if (rev.Contains("Motor"))
  457. return true;
  458. else
  459. return false;
  460. }
  461. catch (Exception ex)
  462. {
  463. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  464. CanExecute = true;
  465. throw ex;
  466. }
  467. finally
  468. {
  469. CanExecute = true;
  470. }
  471. }
  472. public async Task<bool> MotorAsync(bool state)
  473. {
  474. try
  475. {
  476. CanExecute = false;
  477. int v = state ? 1 : 0;
  478. string rev = await SendAndReceiveAsync($"Motor,{v}");
  479. if (rev.Contains("Motor"))
  480. return true;
  481. else
  482. return false;
  483. }
  484. catch (Exception ex)
  485. {
  486. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  487. CanExecute = true;
  488. throw ex;
  489. }
  490. finally
  491. {
  492. CanExecute = true;
  493. }
  494. }
  495. public bool Power(bool state)
  496. {
  497. try
  498. {
  499. CanExecute = false;
  500. int v = state ? 1 : 0;
  501. string rev = SendAndReceive($"Power,{v}");
  502. if (rev.Contains("Power"))
  503. return true;
  504. else
  505. return false;
  506. }
  507. catch (Exception ex)
  508. {
  509. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  510. CanExecute = true;
  511. throw ex;
  512. }
  513. finally
  514. {
  515. CanExecute = true;
  516. }
  517. }
  518. public async Task<bool> PowerAsync(bool state)
  519. {
  520. try
  521. {
  522. CanExecute = false;
  523. int v = state ? 1 : 0;
  524. string rev = await SendAndReceiveAsync($"Power,{v}");
  525. if (rev.Contains("Power"))
  526. return true;
  527. else
  528. return false;
  529. }
  530. catch (Exception ex)
  531. {
  532. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  533. CanExecute = true;
  534. throw ex;
  535. }
  536. finally
  537. {
  538. CanExecute = true;
  539. }
  540. }
  541. public bool Speed(int value)
  542. {
  543. try
  544. {
  545. CanExecute = false;
  546. string rev = SendAndReceive($"Speed,{value}");
  547. if (rev.Contains("Speed"))
  548. return true;
  549. else
  550. return false;
  551. }
  552. catch (Exception ex)
  553. {
  554. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  555. CanExecute = true;
  556. throw ex;
  557. }
  558. finally
  559. {
  560. CanExecute = true;
  561. }
  562. }
  563. public async Task<bool> SpeedAsync(int value)
  564. {
  565. try
  566. {
  567. CanExecute = false;
  568. string rev = await SendAndReceiveAsync($"Speed,{value}");
  569. if (rev.Contains("Speed"))
  570. return true;
  571. else
  572. return false;
  573. }
  574. catch (Exception ex)
  575. {
  576. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  577. CanExecute = true;
  578. throw ex;
  579. }
  580. finally
  581. {
  582. CanExecute = true;
  583. }
  584. }
  585. public bool Speedfactor(int value)
  586. {
  587. try
  588. {
  589. CanExecute = false;
  590. string rev = SendAndReceive($"Speedfactor,{value}");
  591. if (rev.Contains("Speedfactor"))
  592. return true;
  593. else
  594. return false;
  595. }
  596. catch (Exception ex)
  597. {
  598. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  599. CanExecute = true;
  600. throw ex;
  601. }
  602. finally
  603. {
  604. CanExecute = true;
  605. }
  606. }
  607. public async Task<bool> SpeedfactorAsync(int value)
  608. {
  609. try
  610. {
  611. CanExecute = false;
  612. string rev = await SendAndReceiveAsync($"Speedfactor,{value}");
  613. if (rev.Contains("Speedfactor"))
  614. return true;
  615. else
  616. return false;
  617. }
  618. catch (Exception ex)
  619. {
  620. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  621. CanExecute = true;
  622. throw ex;
  623. }
  624. finally
  625. {
  626. CanExecute = true;
  627. }
  628. }
  629. public bool Speeds(double value)
  630. {
  631. try
  632. {
  633. CanExecute = false;
  634. string rev = SendAndReceive($"Speeds,{value}");
  635. if (rev.Contains("Speeds"))
  636. return true;
  637. else
  638. return false;
  639. }
  640. catch (Exception ex)
  641. {
  642. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  643. CanExecute = true;
  644. throw ex;
  645. }
  646. finally
  647. {
  648. CanExecute = true;
  649. }
  650. }
  651. public async Task<bool> SpeedsAsync(double value)
  652. {
  653. try
  654. {
  655. CanExecute = false;
  656. string rev = await SendAndReceiveAsync($"Speeds,{value}");
  657. if (rev.Contains("Speeds"))
  658. return true;
  659. else
  660. return false;
  661. }
  662. catch (Exception ex)
  663. {
  664. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  665. CanExecute = true;
  666. throw ex;
  667. }
  668. finally
  669. {
  670. CanExecute = true;
  671. }
  672. }
  673. public bool Accel(int value)
  674. {
  675. try
  676. {
  677. CanExecute = false;
  678. string rev = SendAndReceive($"Accel,{value},{value}");
  679. if (rev.Contains("Accel"))
  680. return true;
  681. else
  682. return false;
  683. }
  684. catch (Exception ex)
  685. {
  686. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  687. CanExecute = true;
  688. throw ex;
  689. }
  690. finally
  691. {
  692. CanExecute = true;
  693. }
  694. }
  695. public async Task<bool> AccelAsync(int value)
  696. {
  697. try
  698. {
  699. CanExecute = false;
  700. string rev = await SendAndReceiveAsync($"Accel,{value},{value}");
  701. if (rev.Contains("Accel"))
  702. return true;
  703. else
  704. return false;
  705. }
  706. catch (Exception ex)
  707. {
  708. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  709. CanExecute = true;
  710. throw ex;
  711. }
  712. finally
  713. {
  714. CanExecute = true;
  715. }
  716. }
  717. public bool Accels(double value)
  718. {
  719. try
  720. {
  721. CanExecute = false;
  722. string rev = SendAndReceive($"Accels,{value},{value}");
  723. if (rev.Contains("Accels"))
  724. return true;
  725. else
  726. return false;
  727. }
  728. catch (Exception ex)
  729. {
  730. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  731. CanExecute = true;
  732. throw ex;
  733. }
  734. finally
  735. {
  736. CanExecute = true;
  737. }
  738. }
  739. public async Task<bool> AccelsAsync(double value)
  740. {
  741. try
  742. {
  743. CanExecute = false;
  744. string rev = await SendAndReceiveAsync($"Accels,{value},{value}");
  745. if (rev.Contains("Accels"))
  746. return true;
  747. else
  748. return false;
  749. }
  750. catch (Exception ex)
  751. {
  752. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  753. CanExecute = true;
  754. throw ex;
  755. }
  756. finally
  757. {
  758. CanExecute = true;
  759. }
  760. }
  761. public RPoint GetRobotPos()
  762. {
  763. try
  764. {
  765. CanExecute = false;
  766. string rev = SendAndReceive($"GetRobotPos");
  767. if (rev.Contains("GetRobotPos"))
  768. {
  769. RPoint point = new RPoint();
  770. var items = rev.Split(',');
  771. point.X = Convert.ToSingle(items[1]);
  772. point.Y = Convert.ToSingle(items[2]);
  773. point.Z = Convert.ToSingle(items[3]);
  774. point.U = Convert.ToSingle(items[4]);
  775. point.V = Convert.ToSingle(items[5]);
  776. point.W = Convert.ToSingle(items[6]);
  777. point.Hand = (RobotHand)Convert.ToInt32(items[7]);
  778. point.Local = Convert.ToInt32(items[8]);
  779. point.Tool = Convert.ToInt32(items[9]);
  780. CurrentPosition = point.Clone();
  781. return point;
  782. }
  783. else
  784. return null;
  785. }
  786. catch (Exception ex)
  787. {
  788. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  789. CanExecute = true;
  790. throw ex;
  791. }
  792. finally
  793. {
  794. CanExecute = true;
  795. }
  796. }
  797. public async Task<RPoint> GetRobotPosAsync()
  798. {
  799. try
  800. {
  801. CanExecute = false;
  802. string rev = await SendAndReceiveAsync($"GetRobotPos");
  803. if (rev.Contains("GetRobotPos"))
  804. {
  805. RPoint point = new RPoint();
  806. var items = rev.Split(',');
  807. point.X = Convert.ToSingle(items[1]);
  808. point.Y = Convert.ToSingle(items[2]);
  809. point.Z = Convert.ToSingle(items[3]);
  810. point.U = Convert.ToSingle(items[4]);
  811. point.V = Convert.ToSingle(items[5]);
  812. point.W = Convert.ToSingle(items[6]);
  813. point.Hand = (RobotHand)Convert.ToInt32(items[7]);
  814. point.Local = Convert.ToInt32(items[8]);
  815. point.Tool = Convert.ToInt32(items[9]);
  816. CurrentPosition = point.Clone();
  817. return point;
  818. }
  819. else
  820. return null;
  821. }
  822. catch (Exception ex)
  823. {
  824. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  825. CanExecute = true;
  826. throw ex;
  827. }
  828. finally
  829. {
  830. CanExecute = true;
  831. }
  832. }
  833. public bool Jog(string axis, double distance)
  834. {
  835. try
  836. {
  837. CanExecute = false;
  838. string rev = SendAndReceive($"Jog,{axis},{distance}");
  839. if (rev.Contains("Jog"))
  840. return true;
  841. else
  842. return false;
  843. }
  844. catch (Exception ex)
  845. {
  846. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  847. CanExecute = true;
  848. throw ex;
  849. }
  850. finally
  851. {
  852. CanExecute = true;
  853. }
  854. }
  855. public async Task<bool> JogAsync(string axis, double distance)
  856. {
  857. try
  858. {
  859. CanExecute = false;
  860. string rev = await SendAndReceiveAsync($"Jog,{axis},{distance}");
  861. if (rev.Contains("Jog"))
  862. return true;
  863. else
  864. return false;
  865. }
  866. catch (Exception ex)
  867. {
  868. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  869. CanExecute = true;
  870. throw ex;
  871. }
  872. finally
  873. {
  874. CanExecute = true;
  875. }
  876. }
  877. public bool Joint(int joint, double distance)
  878. {
  879. try
  880. {
  881. CanExecute = false;
  882. string rev = SendAndReceive($"Joint,{joint},{distance}");
  883. if (rev.Contains("Joint"))
  884. return true;
  885. else
  886. return false;
  887. }
  888. catch (Exception ex)
  889. {
  890. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  891. CanExecute = true;
  892. throw ex;
  893. }
  894. finally
  895. {
  896. CanExecute = true;
  897. }
  898. }
  899. public async Task<bool> JointAsync(int joint, double distance)
  900. {
  901. try
  902. {
  903. CanExecute = false;
  904. string rev = await SendAndReceiveAsync($"Joint,{joint},{distance}");
  905. if (rev.Contains("Joint"))
  906. return true;
  907. else
  908. return false;
  909. }
  910. catch (Exception ex)
  911. {
  912. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  913. CanExecute = true;
  914. throw ex;
  915. }
  916. finally
  917. {
  918. CanExecute = true;
  919. }
  920. }
  921. public bool Go(RPoint position)
  922. {
  923. try
  924. {
  925. CanExecute = false;
  926. string rev = SendAndReceive($"Go,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  927. if (rev.Contains("Go"))
  928. return true;
  929. else
  930. return false;
  931. }
  932. catch (Exception ex)
  933. {
  934. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  935. CanExecute = true;
  936. throw ex;
  937. }
  938. finally
  939. {
  940. CanExecute = true;
  941. }
  942. }
  943. public async Task<bool> GoAsync(RPoint position)
  944. {
  945. try
  946. {
  947. CanExecute = false;
  948. string rev = await SendAndReceiveAsync($"Go,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool}");
  949. if (rev.Contains("Go"))
  950. return true;
  951. else
  952. return false;
  953. }
  954. catch (Exception ex)
  955. {
  956. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  957. CanExecute = true;
  958. throw ex;
  959. }
  960. finally
  961. {
  962. CanExecute = true;
  963. }
  964. }
  965. public bool Jump(RPoint position, double? LimZ)
  966. {
  967. try
  968. {
  969. CanExecute = false;
  970. double limz = LimZ == null ? 0 : (double)LimZ;
  971. string rev = SendAndReceive($"Jump,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool},{Math.Round(limz,3)}");
  972. if (rev.Contains("Jump"))
  973. return true;
  974. else
  975. return false;
  976. }
  977. catch (Exception ex)
  978. {
  979. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  980. CanExecute = true;
  981. throw ex;
  982. }
  983. finally
  984. {
  985. CanExecute = true;
  986. }
  987. }
  988. public async Task<bool> JumpAsync(RPoint position, double? LimZ)
  989. {
  990. try
  991. {
  992. CanExecute = false;
  993. double limz = LimZ == null ? 0 : (double)LimZ;
  994. string rev = await SendAndReceiveAsync($"Jump,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool},{Math.Round(limz, 3)}");
  995. if (rev.Contains("Jump"))
  996. return true;
  997. else
  998. return false;
  999. }
  1000. catch (Exception ex)
  1001. {
  1002. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1003. CanExecute = true;
  1004. throw ex;
  1005. }
  1006. finally
  1007. {
  1008. CanExecute = true;
  1009. }
  1010. }
  1011. public bool Move(RPoint position)
  1012. {
  1013. try
  1014. {
  1015. CanExecute = false;
  1016. string rev = SendAndReceive($"Move,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  1017. if (rev.Contains("Move"))
  1018. return true;
  1019. else
  1020. return false;
  1021. }
  1022. catch (Exception ex)
  1023. {
  1024. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1025. CanExecute = true;
  1026. throw ex;
  1027. }
  1028. finally
  1029. {
  1030. CanExecute = true;
  1031. }
  1032. }
  1033. public async Task<bool> MoveAsync(RPoint position)
  1034. {
  1035. try
  1036. {
  1037. CanExecute = false;
  1038. string rev = await SendAndReceiveAsync($"Move,{Math.Round(position.X, 3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool}");
  1039. if (rev.Contains("Move"))
  1040. return true;
  1041. else
  1042. return false;
  1043. }
  1044. catch (Exception ex)
  1045. {
  1046. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1047. CanExecute = true;
  1048. throw ex;
  1049. }
  1050. finally
  1051. {
  1052. CanExecute = true;
  1053. }
  1054. }
  1055. public bool SFree()
  1056. {
  1057. try
  1058. {
  1059. CanExecute = false;
  1060. string rev = SendAndReceive($"SFree");
  1061. if (rev.Contains("SFree"))
  1062. return true;
  1063. else
  1064. return false;
  1065. }
  1066. catch (Exception ex)
  1067. {
  1068. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1069. CanExecute = true;
  1070. throw ex;
  1071. }
  1072. finally
  1073. {
  1074. CanExecute = true;
  1075. }
  1076. }
  1077. public async Task<bool> SFreeAsync()
  1078. {
  1079. try
  1080. {
  1081. CanExecute = false;
  1082. string rev = await SendAndReceiveAsync($"SFree");
  1083. if (rev.Contains("SFree"))
  1084. return true;
  1085. else
  1086. return false;
  1087. }
  1088. catch (Exception ex)
  1089. {
  1090. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1091. CanExecute = true;
  1092. throw ex;
  1093. }
  1094. finally
  1095. {
  1096. CanExecute = true;
  1097. }
  1098. }
  1099. public bool SLock()
  1100. {
  1101. try
  1102. {
  1103. CanExecute = false;
  1104. string rev = SendAndReceive($"SLock");
  1105. if (rev.Contains("SLock"))
  1106. return true;
  1107. else
  1108. return false;
  1109. }
  1110. catch (Exception ex)
  1111. {
  1112. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1113. CanExecute = true;
  1114. throw ex;
  1115. }
  1116. finally
  1117. {
  1118. CanExecute = true;
  1119. }
  1120. }
  1121. public async Task<bool> SLockAsync()
  1122. {
  1123. try
  1124. {
  1125. CanExecute = false;
  1126. string rev = await SendAndReceiveAsync($"SLock");
  1127. if (rev.Contains("SLock"))
  1128. return true;
  1129. else
  1130. return false;
  1131. }
  1132. catch (Exception ex)
  1133. {
  1134. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1135. CanExecute = true;
  1136. throw ex;
  1137. }
  1138. finally
  1139. {
  1140. CanExecute = true;
  1141. }
  1142. }
  1143. public bool CalibMotion(RPoint position, double? LimZ)
  1144. {
  1145. try
  1146. {
  1147. CanExecute = false;
  1148. double limz = LimZ == null ? 0 : (double)LimZ;
  1149. string rev = SendAndReceive($"CalibMotion,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool},{limz}");
  1150. if (rev.Contains("CalibMotion"))
  1151. return true;
  1152. else
  1153. return false;
  1154. }
  1155. catch (Exception ex)
  1156. {
  1157. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1158. CanExecute = true;
  1159. throw ex;
  1160. }
  1161. finally
  1162. {
  1163. CanExecute = true;
  1164. }
  1165. }
  1166. public async Task<bool> CalibMotionAsync(RPoint position, double? LimZ)
  1167. {
  1168. try
  1169. {
  1170. CanExecute = false;
  1171. double limz = LimZ == null ? 0 : (double)LimZ;
  1172. string rev = await SendAndReceiveAsync($"CalibMotion,{Math.Round(position.X,3)},{Math.Round(position.Y, 3)},{Math.Round(position.Z, 3)},{Math.Round(position.U, 3)},{Math.Round(position.V, 3)},{Math.Round(position.W, 3)},{(int)position.Hand},{position.Local},{position.Tool},{Math.Round(limz,2)}");
  1173. if (rev.Contains("CalibMotion"))
  1174. return true;
  1175. else
  1176. return false;
  1177. }
  1178. catch (Exception ex)
  1179. {
  1180. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1181. CanExecute = true;
  1182. throw ex;
  1183. }
  1184. finally
  1185. {
  1186. CanExecute = true;
  1187. }
  1188. }
  1189. public bool CalibOutIO(bool state)
  1190. {
  1191. try
  1192. {
  1193. CanExecute = false;
  1194. int va = state ? 1 : 0;
  1195. string rev = SendAndReceive($"CalibOutIO,{va}");
  1196. if (rev.Contains("CalibOutIO"))
  1197. return true;
  1198. else
  1199. return false;
  1200. }
  1201. catch (Exception ex)
  1202. {
  1203. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1204. CanExecute = true;
  1205. throw ex;
  1206. }
  1207. finally
  1208. {
  1209. CanExecute = true;
  1210. }
  1211. }
  1212. public async Task<bool> CalibOutIOAsync(bool state)
  1213. {
  1214. try
  1215. {
  1216. CanExecute = false;
  1217. int va = state ? 1 : 0;
  1218. string rev = await SendAndReceiveAsync($"CalibOutIO,{va}");
  1219. if (rev.Contains("CalibOutIO"))
  1220. return true;
  1221. else
  1222. return false;
  1223. }
  1224. catch (Exception ex)
  1225. {
  1226. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1227. CanExecute = true;
  1228. throw ex;
  1229. }
  1230. finally
  1231. {
  1232. CanExecute = true;
  1233. }
  1234. }
  1235. public bool CalibParame(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  1236. {
  1237. try
  1238. {
  1239. CanExecute = false;
  1240. int va = Power ? 1 : 0;
  1241. string rev = SendAndReceive($"CalibParame,{(int)Pick.PickPlaceModel},{Pick.Inhal},{Pick.Blow},{Speed},{Accel},{va},{WaitSuction},{WaitBlow}");
  1242. if (rev.Contains("CalibParame"))
  1243. return true;
  1244. else
  1245. return false;
  1246. }
  1247. catch (Exception ex)
  1248. {
  1249. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1250. CanExecute = true;
  1251. throw ex;
  1252. }
  1253. finally
  1254. {
  1255. CanExecute = true;
  1256. }
  1257. }
  1258. public async Task<bool> CalibParameAsync(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  1259. {
  1260. try
  1261. {
  1262. CanExecute = false;
  1263. int va = Power ? 1 : 0;
  1264. string rev = await SendAndReceiveAsync($"CalibParame,{(int)Pick.PickPlaceModel},{Pick.Inhal},{Pick.Blow},{Speed},{Accel},{va},{WaitSuction},{WaitBlow}");
  1265. if (rev.Contains("CalibParame"))
  1266. return true;
  1267. else
  1268. return false;
  1269. }
  1270. catch (Exception ex)
  1271. {
  1272. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1273. CanExecute = true;
  1274. throw ex;
  1275. }
  1276. finally
  1277. {
  1278. CanExecute = true;
  1279. }
  1280. }
  1281. /// <summary>
  1282. /// 设置工具坐标
  1283. /// </summary>
  1284. /// <param name="index"></param>
  1285. /// <param name="x"></param>
  1286. /// <param name="y"></param>
  1287. /// <returns></returns>
  1288. public bool SetTool(int index, double x, double y)
  1289. {
  1290. try
  1291. {
  1292. CanExecute = false;
  1293. string rev = SendAndReceive($"SetTool,{index},{x},{y}");
  1294. if (rev.Contains("SetTool"))
  1295. return true;
  1296. else
  1297. return false;
  1298. }
  1299. catch (Exception ex)
  1300. {
  1301. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1302. CanExecute = true;
  1303. throw ex;
  1304. }
  1305. finally
  1306. {
  1307. CanExecute = true;
  1308. }
  1309. }
  1310. /// <summary>
  1311. /// 设置工具坐标
  1312. /// </summary>
  1313. /// <param name="index"></param>
  1314. /// <param name="x"></param>
  1315. /// <param name="y"></param>
  1316. /// <returns></returns>
  1317. public async Task<bool> SetToolAsync(int index, double x, double y)
  1318. {
  1319. try
  1320. {
  1321. CanExecute = false;
  1322. string rev = await SendAndReceiveAsync($"SetTool,{index},{Math.Round(x,3)},{Math.Round(y,3)}");
  1323. if (rev.Contains("SetTool"))
  1324. return true;
  1325. else
  1326. return false;
  1327. }
  1328. catch (Exception ex)
  1329. {
  1330. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1331. CanExecute = true;
  1332. throw ex;
  1333. }
  1334. finally
  1335. {
  1336. CanExecute = true;
  1337. }
  1338. }
  1339. /// <summary>
  1340. /// 选择工具坐标
  1341. /// </summary>
  1342. /// <param name="index"></param>
  1343. /// <returns></returns>
  1344. public bool SelectTool(int index)
  1345. {
  1346. try
  1347. {
  1348. SelectedTool = index;
  1349. CanExecute = false;
  1350. string rev = SendAndReceive($"SelectTool,{index}");
  1351. if (rev.Contains("SetTool"))
  1352. return true;
  1353. else
  1354. return false;
  1355. }
  1356. catch (Exception ex)
  1357. {
  1358. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1359. CanExecute = true;
  1360. throw ex;
  1361. }
  1362. finally
  1363. {
  1364. CanExecute = true;
  1365. }
  1366. }
  1367. /// <summary>
  1368. /// 选择工具坐标
  1369. /// </summary>
  1370. /// <param name="index"></param>
  1371. /// <returns></returns>
  1372. public async Task<bool> SelectToolAsync(int index)
  1373. {
  1374. {
  1375. try
  1376. {
  1377. SelectedTool = index;
  1378. CanExecute = false;
  1379. string rev = await SendAndReceiveAsync($"SelectTool,{index}");
  1380. if (rev.Contains("SetTool"))
  1381. return true;
  1382. else
  1383. return false;
  1384. }
  1385. catch (Exception ex)
  1386. {
  1387. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1388. CanExecute = true;
  1389. throw ex;
  1390. }
  1391. finally
  1392. {
  1393. CanExecute = true;
  1394. }
  1395. }
  1396. }
  1397. #endregion
  1398. #region 收发数据
  1399. /// <summary>
  1400. /// 发送并接收数据
  1401. /// </summary>
  1402. /// <param name="send"></param>
  1403. /// <returns></returns>
  1404. public string SendAndReceive(string send)
  1405. {
  1406. if (ConnectType == TCPConnectType.Client)
  1407. {
  1408. Encoding encoding = GetEncoding();
  1409. SendEvent?.Invoke(this.Id,WaitClient.Client, send);
  1410. var returnData = WaitClient.SendThenReturn(encoding.GetBytes(send), Timeout);
  1411. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1412. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1413. return encoding.GetString(returnData);
  1414. }
  1415. else
  1416. {
  1417. Encoding encoding = GetEncoding();
  1418. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1419. var returnData = WaitClientServer.SendThenReturn(encoding.GetBytes(send), Timeout);
  1420. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1421. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1422. return encoding.GetString(returnData);
  1423. }
  1424. }
  1425. /// <summary>
  1426. /// 发送并接收数据
  1427. /// </summary>
  1428. /// <param name="send"></param>
  1429. /// <returns></returns>
  1430. public async Task<string> SendAndReceiveAsync(string send)
  1431. {
  1432. if (ConnectType == TCPConnectType.Client)
  1433. {
  1434. Encoding encoding = GetEncoding();
  1435. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  1436. var returnData = await WaitClient.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1437. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1438. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1439. return encoding.GetString(returnData);
  1440. }
  1441. else
  1442. {
  1443. Encoding encoding = GetEncoding();
  1444. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1445. var returnData = await WaitClientServer.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1446. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1447. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1448. return encoding.GetString(returnData);
  1449. }
  1450. }
  1451. /// <summary>
  1452. /// 发送并接收数据
  1453. /// </summary>
  1454. /// <param name="send"></param>
  1455. /// <returns></returns>
  1456. public string SendAndReceive(ITcpSessionClient client,string send)
  1457. {
  1458. if (ConnectType == TCPConnectType.Client)
  1459. {
  1460. Encoding encoding = GetEncoding();
  1461. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  1462. var returnData = WaitClient.SendThenReturn(encoding.GetBytes(send), Timeout);
  1463. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1464. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1465. return encoding.GetString(returnData);
  1466. }
  1467. else
  1468. {
  1469. //调用CreateWaitingClient获取到IWaitingClient的对象。
  1470. var waitClientServer = client.CreateWaitingClient(new WaitingOptions()
  1471. {
  1472. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  1473. {
  1474. return true;
  1475. //if (response.Data.Length == 1)
  1476. //{
  1477. // return true;
  1478. //}
  1479. //return false;
  1480. }
  1481. });
  1482. Encoding encoding = GetEncoding();
  1483. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1484. var returnData = waitClientServer.SendThenReturn(encoding.GetBytes(send), Timeout);
  1485. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1486. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1487. return encoding.GetString(returnData);
  1488. }
  1489. }
  1490. /// <summary>
  1491. /// 发送并接收数据
  1492. /// </summary>
  1493. /// <param name="send"></param>
  1494. /// <returns></returns>
  1495. public async Task<string> SendAndReceiveAsync(ITcpSessionClient client,string send)
  1496. {
  1497. if (ConnectType == TCPConnectType.Client)
  1498. {
  1499. Encoding encoding = GetEncoding();
  1500. SendEvent?.Invoke(this.Id, WaitClient.Client, send);
  1501. var returnData = await WaitClient.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1502. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1503. ReceivedEvent?.Invoke(this.Id, WaitClient, receivedDataEvent);
  1504. return encoding.GetString(returnData);
  1505. }
  1506. else
  1507. {
  1508. //调用CreateWaitingClient获取到IWaitingClient的对象。
  1509. var waitClientServer = client.CreateWaitingClient(new WaitingOptions()
  1510. {
  1511. FilterFunc = response => //设置用于筛选的fun委托,当返回为true时,才会响应返回
  1512. {
  1513. return true;
  1514. //if (response.Data.Length == 1)
  1515. //{
  1516. // return true;
  1517. //}
  1518. //return false;
  1519. }
  1520. });
  1521. Encoding encoding = GetEncoding();
  1522. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1523. var returnData = await waitClientServer.SendThenReturnAsync(encoding.GetBytes(send), Timeout);
  1524. ReceivedDataEventArgs receivedDataEvent = new ReceivedDataEventArgs(new ByteBlock(returnData), null);
  1525. ReceivedEvent?.Invoke(this.Id, WaitClientServer.Client, receivedDataEvent);
  1526. return encoding.GetString(returnData);
  1527. }
  1528. }
  1529. public void Send(string send)
  1530. {
  1531. if (ConnectType == TCPConnectType.Client)
  1532. {
  1533. Encoding encoding = GetEncoding();
  1534. SendEvent?.Invoke(this.Id, TcpClient, send);
  1535. TcpClient.Send(encoding.GetBytes(send));
  1536. }
  1537. else
  1538. {
  1539. Encoding encoding = GetEncoding();
  1540. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1541. TcpService.Send(WaitClientServer.Client.Id, encoding.GetBytes(send));
  1542. }
  1543. }
  1544. public async Task SendAsync(string send)
  1545. {
  1546. if (ConnectType == TCPConnectType.Client)
  1547. {
  1548. Encoding encoding = GetEncoding();
  1549. SendEvent?.Invoke(this.Id, TcpClient, send);
  1550. await TcpClient.SendAsync(encoding.GetBytes(send));
  1551. }
  1552. else
  1553. {
  1554. Encoding encoding = GetEncoding();
  1555. SendEvent?.Invoke(this.Id, WaitClientServer.Client, send);
  1556. await TcpService.SendAsync(WaitClientServer.Client.Id, encoding.GetBytes(send));
  1557. }
  1558. }
  1559. public void Send(ITcpSessionClient client, string send)
  1560. {
  1561. try
  1562. {
  1563. if (ConnectType == TCPConnectType.Client)
  1564. {
  1565. Encoding encoding = GetEncoding();
  1566. SendEvent?.Invoke(this.Id, TcpClient, send);
  1567. TcpClient.Send(encoding.GetBytes(send));
  1568. }
  1569. else
  1570. {
  1571. Encoding encoding = GetEncoding();
  1572. SendEvent?.Invoke(this.Id, client, send);
  1573. TcpService.Send(client.Id, encoding.GetBytes(send));
  1574. }
  1575. }
  1576. catch (Exception ex)
  1577. {
  1578. LogHelper.WriteLogError("给机器人发送数据时出错", ex);
  1579. }
  1580. }
  1581. public async Task SendAsync(ITcpSessionClient client, string send)
  1582. {
  1583. try
  1584. {
  1585. if (ConnectType == TCPConnectType.Client)
  1586. {
  1587. Encoding encoding = GetEncoding();
  1588. SendEvent?.Invoke(this.Id, TcpClient, send);
  1589. await TcpClient.SendAsync(encoding.GetBytes(send));
  1590. }
  1591. else
  1592. {
  1593. Encoding encoding = GetEncoding();
  1594. SendEvent?.Invoke(this.Id, client, send);
  1595. await TcpService.SendAsync(client.Id, encoding.GetBytes(send));
  1596. }
  1597. }
  1598. catch (Exception ex)
  1599. {
  1600. LogHelper.WriteLogError("给机器人发送数据时出错",ex);
  1601. }
  1602. }
  1603. /// <summary>
  1604. /// 获取编码
  1605. /// </summary>
  1606. /// <param name="dataEncoding"></param>
  1607. /// <returns></returns>
  1608. public Encoding GetEncoding()
  1609. {
  1610. DataEncoding dataEncoding = this.DataEncoding;
  1611. Encoding encoding;
  1612. if (dataEncoding == DataEncoding.Default)
  1613. {
  1614. encoding = Encoding.Default;
  1615. }
  1616. else if (dataEncoding == DataEncoding.ASCII)
  1617. {
  1618. encoding = Encoding.ASCII;
  1619. }
  1620. else if (dataEncoding == DataEncoding.UTF7)
  1621. {
  1622. encoding = Encoding.UTF7;
  1623. }
  1624. else if (dataEncoding == DataEncoding.UTF8)
  1625. {
  1626. encoding = Encoding.UTF8;
  1627. }
  1628. else if (dataEncoding == DataEncoding.UTF32)
  1629. {
  1630. encoding = Encoding.UTF32;
  1631. }
  1632. else if (dataEncoding == DataEncoding.Unicode)
  1633. {
  1634. encoding = Encoding.Unicode;
  1635. }
  1636. else if (dataEncoding == DataEncoding.BigEndianUnicode)
  1637. {
  1638. encoding = Encoding.BigEndianUnicode;
  1639. }
  1640. else if (dataEncoding == DataEncoding.GB2312)
  1641. {
  1642. encoding = Encoding.GetEncoding("gb2312");
  1643. }
  1644. else
  1645. {
  1646. encoding = Encoding.Default;
  1647. }
  1648. return encoding;
  1649. }
  1650. #endregion
  1651. #region 属性通知
  1652. /// <summary>
  1653. /// Occurs when a property value changes.
  1654. /// </summary>
  1655. public event PropertyChangedEventHandler PropertyChanged;
  1656. /// <summary>
  1657. /// Checks if a property already matches a desired value. Sets the property and
  1658. /// notifies listeners only when necessary.
  1659. /// </summary>
  1660. /// <typeparam name="T">Type of the property.</typeparam>
  1661. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1662. /// <param name="value">Desired value for the property.</param>
  1663. /// <param name="propertyName">Name of the property used to notify listeners. This
  1664. /// value is optional and can be provided automatically when invoked from compilers that
  1665. /// support CallerMemberName.</param>
  1666. /// <returns>True if the value was changed, false if the existing value matched the
  1667. /// desired value.</returns>
  1668. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  1669. {
  1670. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1671. storage = value;
  1672. RaisePropertyChanged(propertyName);
  1673. return true;
  1674. }
  1675. /// <summary>
  1676. /// Checks if a property already matches a desired value. Sets the property and
  1677. /// notifies listeners only when necessary.
  1678. /// </summary>
  1679. /// <typeparam name="T">Type of the property.</typeparam>
  1680. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1681. /// <param name="value">Desired value for the property.</param>
  1682. /// <param name="propertyName">Name of the property used to notify listeners. This
  1683. /// value is optional and can be provided automatically when invoked from compilers that
  1684. /// support CallerMemberName.</param>
  1685. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  1686. /// <returns>True if the value was changed, false if the existing value matched the
  1687. /// desired value.</returns>
  1688. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  1689. {
  1690. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1691. storage = value;
  1692. onChanged?.Invoke();
  1693. RaisePropertyChanged(propertyName);
  1694. return true;
  1695. }
  1696. /// <summary>
  1697. /// Raises this object's PropertyChanged event.
  1698. /// </summary>
  1699. /// <param name="propertyName">Name of the property used to notify listeners. This
  1700. /// value is optional and can be provided automatically when invoked from compilers
  1701. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  1702. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  1703. {
  1704. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  1705. }
  1706. /// <summary>
  1707. /// Raises this object's PropertyChanged event.
  1708. /// </summary>
  1709. /// <param name="args">The PropertyChangedEventArgs</param>
  1710. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  1711. {
  1712. PropertyChanged?.Invoke(this, args);
  1713. }
  1714. public List<DataValue> GetNodesValue()
  1715. {
  1716. throw new NotImplementedException();
  1717. }
  1718. #endregion
  1719. }
  1720. }