EpsonRobot.cs 60 KB

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