FanucRobot.cs 62 KB

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