FanucRobot.cs 62 KB

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