EpsonRobot.cs 59 KB

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