SchneiderRobot.cs 60 KB

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