FanucRobot.cs 61 KB

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