EpsonRobot.cs 60 KB

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