EpsonRobot.cs 60 KB

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