FanucRobot.cs 61 KB

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