EpsonRobot.cs 37 KB

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