SchneiderRobot.cs 59 KB

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