SchneiderRobot.cs 60 KB

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