EpsonRobot.cs 60 KB

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