FanucRobot.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. using MathNet.Numerics;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using TeamAAS.Robot.Core.Robots;
  10. using TeamAAS.Robot.Enums;
  11. using TeamAAS.Robot.Interfaces;
  12. using TeamAAS.Robot.Models;
  13. using TeamAAS.Robot.Attributes;
  14. using TeamAAS.Communication.Enums;
  15. using static TeamAAS.Robot.Core.CoordinateTransformer;
  16. namespace TeamAAS.Robot.Core.Robots
  17. {
  18. [Robot("FANUC", RobotBrand.FANUC)]
  19. public class FanucRobot : IRobot, INotifyPropertyChanged
  20. {
  21. private RobotTcpClient _tcp;
  22. public event Action<Guid, object, byte[]> ConnectedEvent;
  23. public event Action<Guid, object, byte[]> DisconnectedEvent;
  24. public event Action<Guid, object, byte[]> ReceivedEvent;
  25. public event Action<Guid, object, string> SendEvent;
  26. public FanucRobot(RobotInfo robot)
  27. {
  28. Id = robot.Id;
  29. Name = robot.RobotName;
  30. RobotNo = robot.RobotNo;
  31. RobotIp = robot.IP;
  32. RobotPort = robot.Port;
  33. ConnectType = robot.ConnectType;
  34. Terminator = robot.Terminator;
  35. DataEncoding = robot.DataEncoding;
  36. Brand = robot.RobotBrand;
  37. _tcp = new RobotTcpClient(RobotIp, RobotPort, ConnectType, Terminator, DataEncoding);
  38. _tcp.OnConnected += Tcp_OnConnected;
  39. _tcp.OnDisconnected += Tcp_OnDisconnected;
  40. _tcp.OnReceived += Tcp_OnReceived;
  41. _tcp.OnSent += Tcp_OnSent;
  42. }
  43. #region 属性
  44. public RobotTcpClient Tcp => _tcp;
  45. public Guid Id { get; set; }
  46. public string Name { get; set; }
  47. /// <summary>
  48. /// 机器人编号
  49. /// </summary>
  50. public int RobotNo { get; set; }
  51. public int RobotPort { get; private set; }
  52. public string RobotIp { get; private set; }
  53. public TCPConnectType ConnectType { get; private set; }
  54. public Terminator Terminator { get; private set; }
  55. public DataEncoding DataEncoding { get; private set; }
  56. public bool IsConnected { get { return _tcp.IsConnected; } }
  57. public int Timeout { get; set; } = 5000;
  58. private bool _CanExecute = false;
  59. public bool CanExecute
  60. {
  61. get { return _CanExecute; }
  62. set { SetProperty(ref _CanExecute, value); }
  63. }
  64. public int SelectedTool { get; private set; } = 0;
  65. public RobotBrand Brand { get; private set; }
  66. /// <summary>
  67. /// 进入调试模式
  68. /// </summary>
  69. /// <returns></returns>
  70. public bool EnterDebugMode { get; set; }
  71. #endregion
  72. #region 事件接收
  73. private void Tcp_OnConnected(byte[] data)
  74. {
  75. CanExecute = true;
  76. ConnectedEvent?.Invoke(this.Id, _tcp, data);
  77. }
  78. private void Tcp_OnDisconnected(byte[] data)
  79. {
  80. CanExecute = false;
  81. DisconnectedEvent?.Invoke(this.Id, _tcp, data);
  82. }
  83. private void Tcp_OnReceived(byte[] data)
  84. {
  85. ReceivedEvent?.Invoke(this.Id, _tcp, data);
  86. }
  87. private void Tcp_OnSent(string data)
  88. {
  89. SendEvent?.Invoke(this.Id, _tcp, data);
  90. }
  91. #endregion
  92. #region 连接
  93. public void Connect()
  94. {
  95. _tcp.Connect();
  96. }
  97. public async Task ConnectAsync()
  98. {
  99. await _tcp.ConnectAsync();
  100. }
  101. public void Disconnect()
  102. {
  103. _tcp.Disconnect();
  104. }
  105. public void Dispose()
  106. {
  107. _tcp?.Dispose();
  108. }
  109. #endregion
  110. #region 控制
  111. public bool Reset()
  112. {
  113. try
  114. {
  115. CanExecute = false;
  116. string rev = SendAndReceive($"Reset");
  117. if (rev.Contains("Reset"))
  118. return true;
  119. else
  120. return false;
  121. }
  122. catch (Exception ex)
  123. {
  124. CanExecute = true;
  125. throw ex;
  126. }
  127. finally
  128. {
  129. CanExecute = true;
  130. }
  131. }
  132. public async Task<bool> ResetAsync()
  133. {
  134. try
  135. {
  136. CanExecute = false;
  137. string rev = await SendAndReceiveAsync($"Reset");
  138. if (rev.Contains("Reset"))
  139. return true;
  140. else
  141. return false;
  142. }
  143. catch (Exception ex)
  144. {
  145. CanExecute = true;
  146. throw ex;
  147. }
  148. finally
  149. {
  150. CanExecute = true;
  151. }
  152. }
  153. public bool Motor(bool state)
  154. {
  155. try
  156. {
  157. CanExecute = false;
  158. int v = state ? 1 : 0;
  159. string rev = SendAndReceive($"Motor,{v}");
  160. if (rev.Contains("Motor"))
  161. return true;
  162. else
  163. return false;
  164. }
  165. catch (Exception ex)
  166. {
  167. CanExecute = true;
  168. throw ex;
  169. }
  170. finally
  171. {
  172. CanExecute = true;
  173. }
  174. }
  175. public async Task<bool> MotorAsync(bool state)
  176. {
  177. try
  178. {
  179. CanExecute = false;
  180. int v = state ? 1 : 0;
  181. string rev = await SendAndReceiveAsync($"Motor,{v}");
  182. if (rev.Contains("Motor"))
  183. return true;
  184. else
  185. return false;
  186. }
  187. catch (Exception ex)
  188. {
  189. CanExecute = true;
  190. throw ex;
  191. }
  192. finally
  193. {
  194. CanExecute = true;
  195. }
  196. }
  197. public bool Power(bool state)
  198. {
  199. try
  200. {
  201. CanExecute = false;
  202. int v = state ? 1 : 0;
  203. string rev = SendAndReceive($"Power,{v}");
  204. if (rev.Contains("Power"))
  205. return true;
  206. else
  207. return false;
  208. }
  209. catch (Exception ex)
  210. {
  211. CanExecute = true;
  212. throw ex;
  213. }
  214. finally
  215. {
  216. CanExecute = true;
  217. }
  218. }
  219. public async Task<bool> PowerAsync(bool state)
  220. {
  221. try
  222. {
  223. CanExecute = false;
  224. int v = state ? 1 : 0;
  225. string rev = await SendAndReceiveAsync($"Power,{v}");
  226. if (rev.Contains("Power"))
  227. return true;
  228. else
  229. return false;
  230. }
  231. catch (Exception ex)
  232. {
  233. CanExecute = true;
  234. throw ex;
  235. }
  236. finally
  237. {
  238. CanExecute = true;
  239. }
  240. }
  241. public bool Speed(int value)
  242. {
  243. try
  244. {
  245. CanExecute = false;
  246. string rev = SendAndReceive($"Speed,{value}");
  247. if (rev.Contains("Speed"))
  248. return true;
  249. else
  250. return false;
  251. }
  252. catch (Exception ex)
  253. {
  254. CanExecute = true;
  255. throw ex;
  256. }
  257. finally
  258. {
  259. CanExecute = true;
  260. }
  261. }
  262. public async Task<bool> SpeedAsync(int value)
  263. {
  264. try
  265. {
  266. CanExecute = false;
  267. string rev = await SendAndReceiveAsync($"Speed,{value}");
  268. if (rev.Contains("Speed"))
  269. return true;
  270. else
  271. return false;
  272. }
  273. catch (Exception ex)
  274. {
  275. CanExecute = true;
  276. throw ex;
  277. }
  278. finally
  279. {
  280. CanExecute = true;
  281. }
  282. }
  283. public bool Speedfactor(int value)
  284. {
  285. try
  286. {
  287. CanExecute = false;
  288. string rev = SendAndReceive($"Speedfactor,{value}");
  289. if (rev.Contains("Speedfactor"))
  290. return true;
  291. else
  292. return false;
  293. }
  294. catch (Exception ex)
  295. {
  296. CanExecute = true;
  297. throw ex;
  298. }
  299. finally
  300. {
  301. CanExecute = true;
  302. }
  303. }
  304. public async Task<bool> SpeedfactorAsync(int value)
  305. {
  306. try
  307. {
  308. CanExecute = false;
  309. string rev = await SendAndReceiveAsync($"Speedfactor,{value}");
  310. if (rev.Contains("Speedfactor"))
  311. return true;
  312. else
  313. return false;
  314. }
  315. catch (Exception ex)
  316. {
  317. CanExecute = true;
  318. throw ex;
  319. }
  320. finally
  321. {
  322. CanExecute = true;
  323. }
  324. }
  325. public bool Speeds(double value)
  326. {
  327. try
  328. {
  329. CanExecute = false;
  330. string rev = SendAndReceive($"Speeds,{value}");
  331. if (rev.Contains("Speeds"))
  332. return true;
  333. else
  334. return false;
  335. }
  336. catch (Exception ex)
  337. {
  338. CanExecute = true;
  339. throw ex;
  340. }
  341. finally
  342. {
  343. CanExecute = true;
  344. }
  345. }
  346. public async Task<bool> SpeedsAsync(double value)
  347. {
  348. try
  349. {
  350. CanExecute = false;
  351. string rev = await SendAndReceiveAsync($"Speeds,{value}");
  352. if (rev.Contains("Speeds"))
  353. return true;
  354. else
  355. return false;
  356. }
  357. catch (Exception ex)
  358. {
  359. CanExecute = true;
  360. throw ex;
  361. }
  362. finally
  363. {
  364. CanExecute = true;
  365. }
  366. }
  367. public bool Accel(int value)
  368. {
  369. try
  370. {
  371. CanExecute = false;
  372. string rev = SendAndReceive($"Accel,{value},{value}");
  373. if (rev.Contains("Accel"))
  374. return true;
  375. else
  376. return false;
  377. }
  378. catch (Exception ex)
  379. {
  380. CanExecute = true;
  381. throw ex;
  382. }
  383. finally
  384. {
  385. CanExecute = true;
  386. }
  387. }
  388. public async Task<bool> AccelAsync(int value)
  389. {
  390. try
  391. {
  392. CanExecute = false;
  393. string rev = await SendAndReceiveAsync($"Accel,{value},{value}");
  394. if (rev.Contains("Accel"))
  395. return true;
  396. else
  397. return false;
  398. }
  399. catch (Exception ex)
  400. {
  401. CanExecute = true;
  402. throw ex;
  403. }
  404. finally
  405. {
  406. CanExecute = true;
  407. }
  408. }
  409. public bool Accels(double value)
  410. {
  411. try
  412. {
  413. CanExecute = false;
  414. string rev = SendAndReceive($"Accels,{value},{value}");
  415. if (rev.Contains("Accels"))
  416. return true;
  417. else
  418. return false;
  419. }
  420. catch (Exception ex)
  421. {
  422. CanExecute = true;
  423. throw ex;
  424. }
  425. finally
  426. {
  427. CanExecute = true;
  428. }
  429. }
  430. public async Task<bool> AccelsAsync(double value)
  431. {
  432. try
  433. {
  434. CanExecute = false;
  435. string rev = await SendAndReceiveAsync($"Accels,{value},{value}");
  436. if (rev.Contains("Accels"))
  437. return true;
  438. else
  439. return false;
  440. }
  441. catch (Exception ex)
  442. {
  443. CanExecute = true;
  444. throw ex;
  445. }
  446. finally
  447. {
  448. CanExecute = true;
  449. }
  450. }
  451. public RPoint GetRobotPos()
  452. {
  453. try
  454. {
  455. CanExecute = false;
  456. RPoint point = new RPoint();
  457. string rev = SendAndReceive($"GetRobotPos,1");
  458. if (rev.Contains("GetRobotPos"))
  459. {
  460. var items = rev.Split(',');
  461. point.X = Convert.ToSingle(items[1]);
  462. point.Y = Convert.ToSingle(items[2]);
  463. point.Z = Convert.ToSingle(items[3]);
  464. point.U = Convert.ToSingle(items[4]);
  465. }
  466. else
  467. return null;
  468. rev = SendAndReceive($"GetRobotPos,2");
  469. if (rev.Contains("GetRobotPos"))
  470. {
  471. var items = rev.Split(',');
  472. point.V = Convert.ToSingle(items[1]);
  473. point.W = Convert.ToSingle(items[2]);
  474. point.Hand = (RobotHand)Convert.ToInt32(items[3]);
  475. point.Local = Convert.ToInt32(items[4]);
  476. point.Tool = Convert.ToInt32(items[5]);
  477. return point;
  478. }
  479. else
  480. return null;
  481. }
  482. catch (Exception ex)
  483. {
  484. CanExecute = true;
  485. throw ex;
  486. }
  487. finally
  488. {
  489. CanExecute = true;
  490. }
  491. }
  492. public async Task<RPoint> GetRobotPosAsync()
  493. {
  494. try
  495. {
  496. CanExecute = false;
  497. RPoint point = new RPoint();
  498. string rev = await SendAndReceiveAsync($"GetRobotPos,1");
  499. if (rev.Contains("GetRobotPos"))
  500. {
  501. var items = rev.Split(',');
  502. point.X = Convert.ToSingle(items[1]);
  503. point.Y = Convert.ToSingle(items[2]);
  504. point.Z = Convert.ToSingle(items[3]);
  505. point.U = Convert.ToSingle(items[4]);
  506. }
  507. else
  508. return null;
  509. await Task.Delay(100);
  510. rev = await SendAndReceiveAsync($"GetRobotPos,2");
  511. if (rev.Contains("GetRobotPos"))
  512. {
  513. var items = rev.Split(',');
  514. point.V = Convert.ToSingle(items[1]);
  515. point.W = Convert.ToSingle(items[2]);
  516. point.Hand = (RobotHand)Convert.ToInt32(items[3]);
  517. point.Local = Convert.ToInt32(items[4]);
  518. point.Tool = Convert.ToInt32(items[5]);
  519. return point;
  520. }
  521. else
  522. return null;
  523. }
  524. catch (Exception ex)
  525. {
  526. CanExecute = true;
  527. throw ex;
  528. }
  529. finally
  530. {
  531. CanExecute = true;
  532. }
  533. }
  534. public bool Jog(string axis, double distance)
  535. {
  536. try
  537. {
  538. CanExecute = false;
  539. string rev = SendAndReceive($"Jog,{axis},{distance}");
  540. if (rev.Contains("Jog"))
  541. return true;
  542. else
  543. return false;
  544. }
  545. catch (Exception ex)
  546. {
  547. CanExecute = true;
  548. throw ex;
  549. }
  550. finally
  551. {
  552. CanExecute = true;
  553. }
  554. }
  555. public async Task<bool> JogAsync(string axis, double distance)
  556. {
  557. try
  558. {
  559. CanExecute = false;
  560. string rev = await SendAndReceiveAsync($"Jog,{axis},{distance}");
  561. if (rev.Contains("Jog"))
  562. return true;
  563. else
  564. return false;
  565. }
  566. catch (Exception ex)
  567. {
  568. CanExecute = true;
  569. throw ex;
  570. }
  571. finally
  572. {
  573. CanExecute = true;
  574. }
  575. }
  576. public bool Joint(int joint, double distance)
  577. {
  578. try
  579. {
  580. CanExecute = false;
  581. string rev = SendAndReceive($"Joint,{joint},{distance}");
  582. if (rev.Contains("Joint"))
  583. return true;
  584. else
  585. return false;
  586. }
  587. catch (Exception ex)
  588. {
  589. CanExecute = true;
  590. throw ex;
  591. }
  592. finally
  593. {
  594. CanExecute = true;
  595. }
  596. }
  597. public async Task<bool> JointAsync(int joint, double distance)
  598. {
  599. try
  600. {
  601. CanExecute = false;
  602. string rev = await SendAndReceiveAsync($"Joint,{joint},{distance}");
  603. if (rev.Contains("Joint"))
  604. return true;
  605. else
  606. return false;
  607. }
  608. catch (Exception ex)
  609. {
  610. CanExecute = true;
  611. throw ex;
  612. }
  613. finally
  614. {
  615. CanExecute = true;
  616. }
  617. }
  618. public bool Go(RPoint position)
  619. {
  620. try
  621. {
  622. CanExecute = false;
  623. string rev = SendAndReceive($"Go,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  624. if (rev.Contains("Go"))
  625. return true;
  626. else
  627. return false;
  628. }
  629. catch (Exception ex)
  630. {
  631. CanExecute = true;
  632. throw ex;
  633. }
  634. finally
  635. {
  636. CanExecute = true;
  637. }
  638. }
  639. public async Task<bool> GoAsync(RPoint position)
  640. {
  641. try
  642. {
  643. CanExecute = false;
  644. string rev = await SendAndReceiveAsync($"Go,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  645. if (rev.Contains("Go"))
  646. return true;
  647. else
  648. return false;
  649. }
  650. catch (Exception ex)
  651. {
  652. CanExecute = true;
  653. throw ex;
  654. }
  655. finally
  656. {
  657. CanExecute = true;
  658. }
  659. }
  660. public bool Jump(RPoint position, double? LimZ)
  661. {
  662. try
  663. {
  664. CanExecute = false;
  665. double limz = LimZ == null ? 0 : (double)LimZ;
  666. string rev = SendAndReceive($"Jump,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool},{limz}");
  667. if (rev.Contains("Jump"))
  668. return true;
  669. else
  670. return false;
  671. }
  672. catch (Exception ex)
  673. {
  674. CanExecute = true;
  675. throw ex;
  676. }
  677. finally
  678. {
  679. CanExecute = true;
  680. }
  681. }
  682. public async Task<bool> JumpAsync(RPoint position, double? LimZ)
  683. {
  684. try
  685. {
  686. CanExecute = false;
  687. double limz = LimZ == null ? 0 : (double)LimZ;
  688. 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}");
  689. if (rev.Contains("Jump"))
  690. return true;
  691. else
  692. return false;
  693. }
  694. catch (Exception ex)
  695. {
  696. CanExecute = true;
  697. throw ex;
  698. }
  699. finally
  700. {
  701. CanExecute = true;
  702. }
  703. }
  704. public bool Move(RPoint position)
  705. {
  706. try
  707. {
  708. CanExecute = false;
  709. string rev = SendAndReceive($"Move,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  710. if (rev.Contains("Move"))
  711. return true;
  712. else
  713. return false;
  714. }
  715. catch (Exception ex)
  716. {
  717. CanExecute = true;
  718. throw ex;
  719. }
  720. finally
  721. {
  722. CanExecute = true;
  723. }
  724. }
  725. public async Task<bool> MoveAsync(RPoint position)
  726. {
  727. try
  728. {
  729. CanExecute = false;
  730. string rev = await SendAndReceiveAsync($"Move,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool}");
  731. if (rev.Contains("Move"))
  732. return true;
  733. else
  734. return false;
  735. }
  736. catch (Exception ex)
  737. {
  738. CanExecute = true;
  739. throw ex;
  740. }
  741. finally
  742. {
  743. CanExecute = true;
  744. }
  745. }
  746. public bool SFree()
  747. {
  748. try
  749. {
  750. CanExecute = false;
  751. string rev = SendAndReceive($"SFree");
  752. if (rev.Contains("SFree"))
  753. return true;
  754. else
  755. return false;
  756. }
  757. catch (Exception ex)
  758. {
  759. CanExecute = true;
  760. throw ex;
  761. }
  762. finally
  763. {
  764. CanExecute = true;
  765. }
  766. }
  767. public async Task<bool> SFreeAsync()
  768. {
  769. try
  770. {
  771. CanExecute = false;
  772. string rev = await SendAndReceiveAsync($"SFree");
  773. if (rev.Contains("SFree"))
  774. return true;
  775. else
  776. return false;
  777. }
  778. catch (Exception ex)
  779. {
  780. CanExecute = true;
  781. throw ex;
  782. }
  783. finally
  784. {
  785. CanExecute = true;
  786. }
  787. }
  788. public bool SLock()
  789. {
  790. try
  791. {
  792. CanExecute = false;
  793. string rev = SendAndReceive($"SLock");
  794. if (rev.Contains("SLock"))
  795. return true;
  796. else
  797. return false;
  798. }
  799. catch (Exception ex)
  800. {
  801. CanExecute = true;
  802. throw ex;
  803. }
  804. finally
  805. {
  806. CanExecute = true;
  807. }
  808. }
  809. public async Task<bool> SLockAsync()
  810. {
  811. try
  812. {
  813. CanExecute = false;
  814. string rev = await SendAndReceiveAsync($"SLock");
  815. if (rev.Contains("SLock"))
  816. return true;
  817. else
  818. return false;
  819. }
  820. catch (Exception ex)
  821. {
  822. CanExecute = true;
  823. throw ex;
  824. }
  825. finally
  826. {
  827. CanExecute = true;
  828. }
  829. }
  830. public bool CalibMotion(RPoint position, double? LimZ)
  831. {
  832. try
  833. {
  834. CanExecute = false;
  835. double limz = LimZ == null ? 0 : (double)LimZ;
  836. string rev = SendAndReceive($"CalibMotion,{position.X},{position.Y},{position.Z},{position.U},{position.V},{position.W},{(int)position.Hand},{position.Local},{position.Tool},{limz}");
  837. if (rev.Contains("CalibMotion"))
  838. return true;
  839. else
  840. return false;
  841. }
  842. catch (Exception ex)
  843. {
  844. CanExecute = true;
  845. throw ex;
  846. }
  847. finally
  848. {
  849. CanExecute = true;
  850. }
  851. }
  852. public async Task<bool> CalibMotionAsync(RPoint position, double? LimZ)
  853. {
  854. try
  855. {
  856. CanExecute = false;
  857. double limz = LimZ == null ? 0 : (double)LimZ;
  858. 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}");
  859. if (rev.Contains("CalibMotion"))
  860. return true;
  861. else
  862. return false;
  863. }
  864. catch (Exception ex)
  865. {
  866. CanExecute = true;
  867. throw ex;
  868. }
  869. finally
  870. {
  871. CanExecute = true;
  872. }
  873. }
  874. public bool CalibOutIO(bool state)
  875. {
  876. try
  877. {
  878. CanExecute = false;
  879. int va = state ? 1 : 0;
  880. string rev = SendAndReceive($"CalibOutIO,{va}");
  881. if (rev.Contains("CalibOutIO"))
  882. return true;
  883. else
  884. return false;
  885. }
  886. catch (Exception ex)
  887. {
  888. CanExecute = true;
  889. throw ex;
  890. }
  891. finally
  892. {
  893. CanExecute = true;
  894. }
  895. }
  896. public async Task<bool> CalibOutIOAsync(bool state)
  897. {
  898. try
  899. {
  900. CanExecute = false;
  901. int va = state ? 1 : 0;
  902. string rev = await SendAndReceiveAsync($"CalibOutIO,{va}");
  903. if (rev.Contains("CalibOutIO"))
  904. return true;
  905. else
  906. return false;
  907. }
  908. catch (Exception ex)
  909. {
  910. CanExecute = true;
  911. throw ex;
  912. }
  913. finally
  914. {
  915. CanExecute = true;
  916. }
  917. }
  918. public bool CalibParame(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  919. {
  920. try
  921. {
  922. CanExecute = false;
  923. int va = Power ? 1 : 0;
  924. string rev = SendAndReceive($"CalibParame,{(int)Pick.PickPlaceModel},{Pick.Inhal},{Pick.Blow},{Speed},{Accel},{va},{WaitSuction},{WaitBlow}");
  925. if (rev.Contains("CalibParame"))
  926. return true;
  927. else
  928. return false;
  929. }
  930. catch (Exception ex)
  931. {
  932. CanExecute = true;
  933. throw ex;
  934. }
  935. finally
  936. {
  937. CanExecute = true;
  938. }
  939. }
  940. public async Task<bool> CalibParameAsync(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  941. {
  942. try
  943. {
  944. CanExecute = false;
  945. int va = Power ? 1 : 0;
  946. string rev = await SendAndReceiveAsync($"CalibParame,{(int)Pick.PickPlaceModel},{Pick.Inhal},{Pick.Blow},{Speed},{Accel},{va},{WaitSuction},{WaitBlow}");
  947. if (rev.Contains("CalibParame"))
  948. return true;
  949. else
  950. return false;
  951. }
  952. catch (Exception ex)
  953. {
  954. CanExecute = true;
  955. throw ex;
  956. }
  957. finally
  958. {
  959. CanExecute = true;
  960. }
  961. }
  962. /// <summary>
  963. /// 设置工具坐标
  964. /// </summary>
  965. /// <param name="index"></param>
  966. /// <param name="x"></param>
  967. /// <param name="y"></param>
  968. /// <returns></returns>
  969. public bool SetTool(int index, double x, double y)
  970. {
  971. try
  972. {
  973. CanExecute = false;
  974. string rev = SendAndReceive($"SetTool,{index},{x},{y}");
  975. if (rev.Contains("SetTool"))
  976. return true;
  977. else
  978. return false;
  979. }
  980. catch (Exception ex)
  981. {
  982. CanExecute = true;
  983. throw ex;
  984. }
  985. finally
  986. {
  987. CanExecute = true;
  988. }
  989. }
  990. /// <summary>
  991. /// 设置工具坐标
  992. /// </summary>
  993. /// <param name="index"></param>
  994. /// <param name="x"></param>
  995. /// <param name="y"></param>
  996. /// <returns></returns>
  997. public async Task<bool> SetToolAsync(int index, double x, double y)
  998. {
  999. try
  1000. {
  1001. CanExecute = false;
  1002. string rev = await SendAndReceiveAsync($"SetTool,{index},{x},{y}");
  1003. if (rev.Contains("SetTool"))
  1004. return true;
  1005. else
  1006. return false;
  1007. }
  1008. catch (Exception ex)
  1009. {
  1010. CanExecute = true;
  1011. throw ex;
  1012. }
  1013. finally
  1014. {
  1015. CanExecute = true;
  1016. }
  1017. }
  1018. /// <summary>
  1019. /// 选择工具坐标
  1020. /// </summary>
  1021. /// <param name="index"></param>
  1022. /// <returns></returns>
  1023. public bool SelectTool(int index)
  1024. {
  1025. try
  1026. {
  1027. SelectedTool = index;
  1028. CanExecute = false;
  1029. string rev = SendAndReceive($"SelectTool,{index}");
  1030. if (rev.Contains("SetTool"))
  1031. return true;
  1032. else
  1033. return false;
  1034. }
  1035. catch (Exception ex)
  1036. {
  1037. CanExecute = true;
  1038. throw ex;
  1039. }
  1040. finally
  1041. {
  1042. CanExecute = true;
  1043. }
  1044. }
  1045. /// <summary>
  1046. /// 选择工具坐标
  1047. /// </summary>
  1048. /// <param name="index"></param>
  1049. /// <returns></returns>
  1050. public async Task<bool> SelectToolAsync(int index)
  1051. {
  1052. {
  1053. try
  1054. {
  1055. SelectedTool = index;
  1056. CanExecute = false;
  1057. string rev = await SendAndReceiveAsync($"SelectTool,{index}");
  1058. if (rev.Contains("SetTool"))
  1059. return true;
  1060. else
  1061. return false;
  1062. }
  1063. catch (Exception ex)
  1064. {
  1065. CanExecute = true;
  1066. throw ex;
  1067. }
  1068. finally
  1069. {
  1070. CanExecute = true;
  1071. }
  1072. }
  1073. }
  1074. #endregion
  1075. #region 收发数据
  1076. public string SendAndReceive(string send)
  1077. {
  1078. return _tcp.SendAndReceive(send, Timeout);
  1079. }
  1080. public async Task<string> SendAndReceiveAsync(string send)
  1081. {
  1082. return await _tcp.SendAndReceiveAsync(send, Timeout);
  1083. }
  1084. public void Send(string send)
  1085. {
  1086. _tcp.Send(send);
  1087. }
  1088. public async Task SendAsync(string send)
  1089. {
  1090. await _tcp.SendAsync(send);
  1091. }
  1092. public Encoding GetEncoding()
  1093. {
  1094. return _tcp.GetEncoding();
  1095. }
  1096. #endregion
  1097. #region 属性通知
  1098. /// <summary>
  1099. /// Occurs when a property value changes.
  1100. /// </summary>
  1101. public event PropertyChangedEventHandler PropertyChanged;
  1102. /// <summary>
  1103. /// Checks if a property already matches a desired value. Sets the property and
  1104. /// notifies listeners only when necessary.
  1105. /// </summary>
  1106. /// <typeparam name="T">Type of the property.</typeparam>
  1107. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1108. /// <param name="value">Desired value for the property.</param>
  1109. /// <param name="propertyName">Name of the property used to notify listeners. This
  1110. /// value is optional and can be provided automatically when invoked from compilers that
  1111. /// support CallerMemberName.</param>
  1112. /// <returns>True if the value was changed, false if the existing value matched the
  1113. /// desired value.</returns>
  1114. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  1115. {
  1116. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1117. storage = value;
  1118. RaisePropertyChanged(propertyName);
  1119. return true;
  1120. }
  1121. /// <summary>
  1122. /// Checks if a property already matches a desired value. Sets the property and
  1123. /// notifies listeners only when necessary.
  1124. /// </summary>
  1125. /// <typeparam name="T">Type of the property.</typeparam>
  1126. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1127. /// <param name="value">Desired value for the property.</param>
  1128. /// <param name="propertyName">Name of the property used to notify listeners. This
  1129. /// value is optional and can be provided automatically when invoked from compilers that
  1130. /// support CallerMemberName.</param>
  1131. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  1132. /// <returns>True if the value was changed, false if the existing value matched the
  1133. /// desired value.</returns>
  1134. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  1135. {
  1136. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1137. storage = value;
  1138. onChanged?.Invoke();
  1139. RaisePropertyChanged(propertyName);
  1140. return true;
  1141. }
  1142. /// <summary>
  1143. /// Raises this object's PropertyChanged event.
  1144. /// </summary>
  1145. /// <param name="propertyName">Name of the property used to notify listeners. This
  1146. /// value is optional and can be provided automatically when invoked from compilers
  1147. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  1148. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  1149. {
  1150. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  1151. }
  1152. /// <summary>
  1153. /// Raises this object's PropertyChanged event.
  1154. /// </summary>
  1155. /// <param name="args">The PropertyChangedEventArgs</param>
  1156. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  1157. {
  1158. PropertyChanged?.Invoke(this, args);
  1159. }
  1160. public List<object> GetNodesValue()
  1161. {
  1162. throw new NotImplementedException();
  1163. }
  1164. #endregion
  1165. }
  1166. }