FanucRobot.cs 61 KB

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