XYZ_Platform.cs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538
  1. using ControlzEx.Standard;
  2. using MahApps.Metro.Controls;
  3. using MathNet.Numerics;
  4. using Opc.Ua;
  5. using OpcUaHelper;
  6. using OxyPlot.Axes;
  7. using Prism;
  8. using Prism.Ioc;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.ComponentModel;
  12. using System.Diagnostics;
  13. using System.Linq;
  14. using System.Net;
  15. using System.Runtime.CompilerServices;
  16. using System.Text;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using TeamAAS_VP;
  20. using TeamAAS_VP.Enums;
  21. using TeamAAS_VP.Interfaces;
  22. using TeamAAS_VP.Models;
  23. using TouchSocket.Core;
  24. using TouchSocket.Sockets;
  25. using static NPOI.HSSF.Util.HSSFColor;
  26. using static TeamAAS_VP.Core.CoordinateTransformer;
  27. namespace TeamAAS_VP.Core.Robots
  28. {
  29. public class XYZ_Platform : IRobot, INotifyPropertyChanged
  30. {
  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, List<ProcedureOutputOpcAdr>> TrggerChangeEvent;
  35. public event Action<Guid, object, string> SendEvent;
  36. public XYZ_Platform(RobotInfo robot)
  37. {
  38. RobotInfo = robot;
  39. Id = robot.Id;
  40. RobotIp = robot.IP;
  41. RobotPort = robot.Port;
  42. ConnectType = robot.ConnectType;
  43. Terminator = robot.Terminator;
  44. DataEncoding = robot.DataEncoding;
  45. Brand = RobotBrand.XYZ_Platform;
  46. var app = (PrismApplicationBase)App.Current;
  47. Management management = app.Container.Resolve<Management>();
  48. Plc = management.PlcService.GetPlc(robot.PLC);
  49. Plc.ConnectChangedEvent += Plc_ConnectChangedEvent;
  50. }
  51. private void Plc_ConnectChangedEvent(object arg1, bool arg2)
  52. {
  53. CanExecute = arg2;
  54. }
  55. #region 属性
  56. public TcpClient TcpClient { get; private set; }
  57. public TcpService TcpService { get; private set; }
  58. public Guid Id { get; set; }
  59. public int RobotPort { get; private set; }
  60. public string RobotIp { get; private set; }
  61. public TCPConnectType ConnectType { get; private set; }
  62. public Terminator Terminator { get; private set; }
  63. public DataEncoding DataEncoding { get; private set; }
  64. public bool IsConnected
  65. {
  66. get
  67. {
  68. if (Plc != null)
  69. {
  70. return Plc.IsConnected;
  71. }
  72. else
  73. {
  74. return false;
  75. }
  76. }
  77. }
  78. public int Timeout { get; set; } = 20000;
  79. private bool _CanExecute = true;
  80. public bool CanExecute
  81. {
  82. get { return _CanExecute; }
  83. set { SetProperty(ref _CanExecute, value); }
  84. }
  85. public int SelectedTool { get; private set; } = 0;
  86. public RobotBrand Brand { get; private set; }
  87. public IPlc Plc { get; private set; }
  88. public RobotInfo RobotInfo { get; private set; }
  89. #endregion
  90. #region 连接
  91. public void Connect()
  92. {
  93. }
  94. public Task ConnectAsync()
  95. {
  96. return Task.CompletedTask;
  97. }
  98. public void Disconnect()
  99. {
  100. }
  101. public void Dispose()
  102. {
  103. }
  104. #endregion
  105. #region 控制
  106. public bool Reset()
  107. {
  108. return false;
  109. }
  110. public async Task<bool> ResetAsync()
  111. {
  112. return false;
  113. }
  114. public bool Motor(bool state)
  115. {
  116. return false;
  117. }
  118. public async Task<bool> MotorAsync(bool state)
  119. {
  120. return false;
  121. }
  122. public bool Power(bool state)
  123. {
  124. return true;
  125. }
  126. public async Task<bool> PowerAsync(bool state)
  127. {
  128. return true;
  129. }
  130. public bool Speed(int value)
  131. {
  132. if (Plc == null || !Plc.IsConnected)
  133. {
  134. return false;
  135. }
  136. return Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.Speed, value);
  137. }
  138. public async Task<bool> SpeedAsync(int value)
  139. {
  140. if (Plc == null || !Plc.IsConnected)
  141. {
  142. return false;
  143. }
  144. return await Plc.OpcUaClient.WriteNodeAsync(RobotInfo.PlcRobotParameter.CommandParameter.Speed, value);
  145. }
  146. public bool Speedfactor(int value)
  147. {
  148. return true;
  149. }
  150. public async Task<bool> SpeedfactorAsync(int value)
  151. {
  152. return true;
  153. }
  154. public bool Speeds(double value)
  155. {
  156. return true;
  157. }
  158. public async Task<bool> SpeedsAsync(double value)
  159. {
  160. return true;
  161. }
  162. public bool Accel(int value)
  163. {
  164. if (Plc == null || !Plc.IsConnected)
  165. {
  166. return false;
  167. }
  168. return Plc.OpcUaClient.WriteNode(RobotInfo.PlcRobotParameter.CommandParameter.Accel, value);
  169. }
  170. public async Task<bool> AccelAsync(int value)
  171. {
  172. if (Plc == null || !Plc.IsConnected)
  173. {
  174. return false;
  175. }
  176. return await Plc.OpcUaClient.WriteNodeAsync(RobotInfo.PlcRobotParameter.CommandParameter.Accel, value);
  177. }
  178. public bool Accels(double value)
  179. {
  180. return true;
  181. }
  182. public async Task<bool> AccelsAsync(double value)
  183. {
  184. return true;
  185. }
  186. public RPoint GetRobotPos()
  187. {
  188. if (Plc == null || !Plc.IsConnected)
  189. {
  190. return null;
  191. }
  192. try
  193. {
  194. for (int i = 0; i < 3; i++)
  195. {
  196. var data = Plc.OpcUaClient.ReadNodes(new List<string>
  197. {
  198. RobotInfo.PlcRobotParameter.StateParameter.X,
  199. RobotInfo.PlcRobotParameter.StateParameter.Y,
  200. RobotInfo.PlcRobotParameter.StateParameter.Z,
  201. RobotInfo.PlcRobotParameter.StateParameter.U,
  202. }.Select(p => new Opc.Ua.NodeId(p)).ToArray());
  203. if (StatusCode.IsGood(data[0].StatusCode) && StatusCode.IsGood(data[1].StatusCode) && StatusCode.IsGood(data[2].StatusCode) && StatusCode.IsGood(data[3].StatusCode))
  204. {
  205. RPoint point = new RPoint();
  206. point.X = Convert.ToSingle(data[0].Value);
  207. point.Y = Convert.ToSingle(data[1].Value);
  208. point.Z = Convert.ToSingle(data[2].Value);
  209. point.U = Convert.ToSingle(data[3].Value);
  210. point.V = 0;
  211. point.W = 0;
  212. point.Hand = RobotHand.Right;
  213. point.Local = 0;
  214. point.Tool = 0;
  215. return point;
  216. }
  217. }
  218. return null;
  219. }
  220. catch (Exception ex)
  221. {
  222. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  223. throw ex;
  224. }
  225. }
  226. public async Task<RPoint> GetRobotPosAsync()
  227. {
  228. if (Plc == null || !Plc.IsConnected)
  229. {
  230. return null;
  231. }
  232. try
  233. {
  234. for (int i = 0; i < 3; i++)
  235. {
  236. var data = await Plc.OpcUaClient.ReadNodesAsync(new List<string>
  237. {
  238. RobotInfo.PlcRobotParameter.StateParameter.X,
  239. RobotInfo.PlcRobotParameter.StateParameter.Y,
  240. RobotInfo.PlcRobotParameter.StateParameter.Z,
  241. RobotInfo.PlcRobotParameter.StateParameter.U,
  242. }.Select(p => new Opc.Ua.NodeId(p)).ToArray());
  243. if (StatusCode.IsGood(data[0].StatusCode) && StatusCode.IsGood(data[1].StatusCode) && StatusCode.IsGood(data[2].StatusCode) && StatusCode.IsGood(data[3].StatusCode))
  244. {
  245. RPoint point = new RPoint();
  246. point.X = Convert.ToSingle(data[0].Value);
  247. point.Y = Convert.ToSingle(data[1].Value);
  248. point.Z = Convert.ToSingle(data[2].Value);
  249. point.U = Convert.ToSingle(data[3].Value);
  250. point.V = 0;
  251. point.W = 0;
  252. point.Hand = RobotHand.Right;
  253. point.Local = 0;
  254. point.Tool = 0;
  255. return point;
  256. }
  257. }
  258. return null;
  259. }
  260. catch (Exception ex)
  261. {
  262. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  263. throw ex;
  264. }
  265. }
  266. public bool Jog(string axis, double distance)
  267. {
  268. if (Plc == null || !Plc.IsConnected)
  269. {
  270. return false;
  271. }
  272. try
  273. {
  274. CanExecute = false;
  275. int jogAxis = 0;
  276. switch (axis)
  277. {
  278. case "X":
  279. jogAxis = 1;
  280. break;
  281. case "Y":
  282. jogAxis = 2;
  283. break;
  284. case "Z":
  285. jogAxis = 3;
  286. break;
  287. case "U":
  288. jogAxis = 4;
  289. break;
  290. default:
  291. break;
  292. }
  293. if (jogAxis == 0)
  294. {
  295. return false;
  296. }
  297. List<string> nodes = new List<string>() {
  298. RobotInfo.PlcRobotParameter.CommandParameter.Distance,
  299. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  300. RobotInfo.PlcRobotParameter.CommandParameter.JogCmd,
  301. };
  302. List<object> values = new List<object>()
  303. {
  304. (float)distance,
  305. false,
  306. jogAxis,
  307. };
  308. for (int i = 0; i < 3; i++)
  309. {
  310. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  311. {
  312. Stopwatch sw = new Stopwatch();
  313. sw.Start();
  314. while (true)
  315. {
  316. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  317. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  318. {
  319. sw.Stop();
  320. return true;
  321. }
  322. if (sw.Elapsed.TotalMilliseconds > Timeout)
  323. {
  324. sw.Stop();
  325. return false;
  326. }
  327. }
  328. }
  329. }
  330. return false;
  331. }
  332. catch (Exception ex)
  333. {
  334. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  335. CanExecute = true;
  336. throw ex;
  337. }
  338. finally
  339. {
  340. CanExecute = true;
  341. }
  342. }
  343. public async Task<bool> JogAsync(string axis, double distance)
  344. {
  345. if (Plc == null || !Plc.IsConnected)
  346. {
  347. return false;
  348. }
  349. return await Task.Run(() =>
  350. {
  351. try
  352. {
  353. CanExecute = false;
  354. int jogAxis = 0;
  355. switch (axis)
  356. {
  357. case "X":
  358. jogAxis = 1;
  359. break;
  360. case "Y":
  361. jogAxis = 2;
  362. break;
  363. case "Z":
  364. jogAxis = 3;
  365. break;
  366. case "U":
  367. jogAxis = 4;
  368. break;
  369. default:
  370. break;
  371. }
  372. if (jogAxis == 0)
  373. {
  374. return false;
  375. }
  376. List<string> nodes = new List<string>() {
  377. RobotInfo.PlcRobotParameter.CommandParameter.Distance,
  378. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  379. RobotInfo.PlcRobotParameter.CommandParameter.JogCmd,
  380. };
  381. List<object> values = new List<object>()
  382. {
  383. (float)distance,
  384. false,
  385. jogAxis,
  386. };
  387. for (int i = 0; i < 3; i++)
  388. {
  389. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  390. {
  391. Stopwatch sw = new Stopwatch();
  392. sw.Start();
  393. while (true)
  394. {
  395. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  396. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  397. {
  398. sw.Stop();
  399. return true;
  400. }
  401. if (sw.Elapsed.TotalMilliseconds > Timeout)
  402. {
  403. sw.Stop();
  404. return false;
  405. }
  406. }
  407. }
  408. }
  409. return false;
  410. }
  411. catch (Exception ex)
  412. {
  413. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  414. CanExecute = true;
  415. throw ex;
  416. }
  417. finally
  418. {
  419. CanExecute = true;
  420. }
  421. });
  422. }
  423. public bool Joint(int joint, double distance)
  424. {
  425. return true;
  426. }
  427. public async Task<bool> JointAsync(int joint, double distance)
  428. {
  429. return true;
  430. }
  431. public bool Go(RPoint position)
  432. {
  433. if (Plc == null || !Plc.IsConnected)
  434. {
  435. return false;
  436. }
  437. try
  438. {
  439. CanExecute = false;
  440. List<string> nodes = new List<string>() {
  441. RobotInfo.PlcRobotParameter.CommandParameter.X,
  442. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  443. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  444. RobotInfo.PlcRobotParameter.CommandParameter.U,
  445. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  446. };
  447. List<object> values = new List<object>()
  448. {
  449. position.X,
  450. position.Y,
  451. position.Z,
  452. position.U,
  453. false,
  454. };
  455. for (int i = 0; i < 3; i++)
  456. {
  457. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  458. {
  459. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  460. {
  461. Stopwatch sw = new Stopwatch();
  462. sw.Start();
  463. while (true)
  464. {
  465. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  466. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  467. {
  468. sw.Stop();
  469. return true;
  470. }
  471. if (sw.Elapsed.TotalMilliseconds > Timeout)
  472. {
  473. sw.Stop();
  474. return false;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. return false;
  481. }
  482. catch (Exception ex)
  483. {
  484. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  485. CanExecute = true;
  486. throw ex;
  487. }
  488. finally
  489. {
  490. CanExecute = true;
  491. }
  492. }
  493. public async Task<bool> GoAsync(RPoint position)
  494. {
  495. return await Task.Run(() =>
  496. {
  497. if (Plc == null || !Plc.IsConnected)
  498. {
  499. return false;
  500. }
  501. try
  502. {
  503. CanExecute = false;
  504. List<string> nodes = new List<string>() {
  505. RobotInfo.PlcRobotParameter.CommandParameter.X,
  506. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  507. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  508. RobotInfo.PlcRobotParameter.CommandParameter.U,
  509. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  510. };
  511. List<object> values = new List<object>()
  512. {
  513. position.X,
  514. position.Y,
  515. position.Z,
  516. position.U,
  517. false,
  518. };
  519. for (int i = 0; i < 3; i++)
  520. {
  521. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  522. {
  523. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  524. {
  525. Stopwatch sw = new Stopwatch();
  526. sw.Start();
  527. while (true)
  528. {
  529. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  530. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  531. {
  532. sw.Stop();
  533. return true;
  534. }
  535. if (sw.Elapsed.TotalMilliseconds > Timeout)
  536. {
  537. sw.Stop();
  538. return false;
  539. }
  540. }
  541. }
  542. }
  543. }
  544. return false;
  545. }
  546. catch (Exception ex)
  547. {
  548. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  549. CanExecute = true;
  550. throw ex;
  551. }
  552. finally
  553. {
  554. CanExecute = true;
  555. }
  556. });
  557. }
  558. public bool Jump(RPoint position, double? LimZ)
  559. {
  560. if (Plc == null || !Plc.IsConnected)
  561. {
  562. return false;
  563. }
  564. try
  565. {
  566. CanExecute = false;
  567. double limz = LimZ == null ? 0 : (double)LimZ;
  568. List<string> nodes = new List<string>() {
  569. RobotInfo.PlcRobotParameter.CommandParameter.X,
  570. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  571. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  572. RobotInfo.PlcRobotParameter.CommandParameter.U,
  573. RobotInfo.PlcRobotParameter.CommandParameter.Limz,
  574. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  575. };
  576. List<object> values = new List<object>()
  577. {
  578. position.X,
  579. position.Y,
  580. position.Z,
  581. position.U,
  582. limz,
  583. false,
  584. };
  585. for (int i = 0; i < 3; i++)
  586. {
  587. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  588. {
  589. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  590. {
  591. Stopwatch sw = new Stopwatch();
  592. sw.Start();
  593. while (true)
  594. {
  595. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  596. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  597. {
  598. sw.Stop();
  599. return true;
  600. }
  601. if (sw.Elapsed.TotalMilliseconds > Timeout)
  602. {
  603. sw.Stop();
  604. return false;
  605. }
  606. }
  607. }
  608. }
  609. }
  610. return false;
  611. }
  612. catch (Exception ex)
  613. {
  614. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  615. CanExecute = true;
  616. throw ex;
  617. }
  618. finally
  619. {
  620. CanExecute = true;
  621. }
  622. }
  623. public async Task<bool> JumpAsync(RPoint position, double? LimZ)
  624. {
  625. if (Plc == null || !Plc.IsConnected)
  626. {
  627. return false;
  628. }
  629. return await Task.Run(() =>
  630. {
  631. try
  632. {
  633. CanExecute = false;
  634. double limz = LimZ == null ? 0 : (double)LimZ;
  635. List<string> nodes = new List<string>() {
  636. RobotInfo.PlcRobotParameter.CommandParameter.X,
  637. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  638. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  639. RobotInfo.PlcRobotParameter.CommandParameter.U,
  640. RobotInfo.PlcRobotParameter.CommandParameter.Limz,
  641. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  642. };
  643. List<object> values = new List<object>()
  644. {
  645. position.X,
  646. position.Y,
  647. position.Z,
  648. position.U,
  649. limz,
  650. false,
  651. };
  652. for (int i = 0; i < 3; i++)
  653. {
  654. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  655. {
  656. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  657. {
  658. Stopwatch sw = new Stopwatch();
  659. sw.Start();
  660. while (true)
  661. {
  662. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  663. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  664. {
  665. sw.Stop();
  666. return true;
  667. }
  668. if (sw.Elapsed.TotalMilliseconds > Timeout)
  669. {
  670. sw.Stop();
  671. return false;
  672. }
  673. }
  674. }
  675. }
  676. }
  677. return false;
  678. }
  679. catch (Exception ex)
  680. {
  681. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  682. CanExecute = true;
  683. throw ex;
  684. }
  685. finally
  686. {
  687. CanExecute = true;
  688. }
  689. });
  690. }
  691. public bool Move(RPoint position)
  692. {
  693. if (Plc == null || !Plc.IsConnected)
  694. {
  695. return false;
  696. }
  697. try
  698. {
  699. CanExecute = false;
  700. List<string> nodes = new List<string>() {
  701. RobotInfo.PlcRobotParameter.CommandParameter.X,
  702. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  703. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  704. RobotInfo.PlcRobotParameter.CommandParameter.U,
  705. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  706. };
  707. List<object> values = new List<object>()
  708. {
  709. position.X,
  710. position.Y,
  711. position.Z,
  712. position.U,
  713. false,
  714. };
  715. for (int i = 0; i < 3; i++)
  716. {
  717. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  718. {
  719. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  720. {
  721. Stopwatch sw = new Stopwatch();
  722. sw.Start();
  723. while (true)
  724. {
  725. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  726. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  727. {
  728. sw.Stop();
  729. return true;
  730. }
  731. if (sw.Elapsed.TotalMilliseconds > Timeout)
  732. {
  733. sw.Stop();
  734. return false;
  735. }
  736. }
  737. }
  738. }
  739. }
  740. return false;
  741. }
  742. catch (Exception ex)
  743. {
  744. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  745. CanExecute = true;
  746. throw ex;
  747. }
  748. finally
  749. {
  750. CanExecute = true;
  751. }
  752. }
  753. public async Task<bool> MoveAsync(RPoint position)
  754. {
  755. if (Plc == null || !Plc.IsConnected)
  756. {
  757. return false;
  758. }
  759. return await Task.Run(() =>
  760. {
  761. try
  762. {
  763. CanExecute = false;
  764. List<string> nodes = new List<string>() {
  765. RobotInfo.PlcRobotParameter.CommandParameter.X,
  766. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  767. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  768. RobotInfo.PlcRobotParameter.CommandParameter.U,
  769. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  770. };
  771. List<object> values = new List<object>()
  772. {
  773. position.X,
  774. position.Y,
  775. position.Z,
  776. position.U,
  777. false,
  778. };
  779. for (int i = 0; i < 3; i++)
  780. {
  781. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  782. {
  783. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  784. {
  785. Stopwatch sw = new Stopwatch();
  786. sw.Start();
  787. while (true)
  788. {
  789. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  790. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  791. {
  792. sw.Stop();
  793. return true;
  794. }
  795. if (sw.Elapsed.TotalMilliseconds > Timeout)
  796. {
  797. sw.Stop();
  798. return false;
  799. }
  800. }
  801. }
  802. }
  803. }
  804. return false;
  805. }
  806. catch (Exception ex)
  807. {
  808. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  809. CanExecute = true;
  810. throw ex;
  811. }
  812. finally
  813. {
  814. CanExecute = true;
  815. }
  816. });
  817. }
  818. public bool SFree()
  819. {
  820. return false;
  821. }
  822. public async Task<bool> SFreeAsync()
  823. {
  824. return false;
  825. }
  826. public bool SLock()
  827. {
  828. return false;
  829. }
  830. public async Task<bool> SLockAsync()
  831. {
  832. return false;
  833. }
  834. public bool CalibMotion(RPoint position, double? LimZ)
  835. {
  836. if (Plc == null || !Plc.IsConnected)
  837. {
  838. return false;
  839. }
  840. try
  841. {
  842. CanExecute = false;
  843. double limz = LimZ == null ? 0 : (double)LimZ;
  844. List<string> nodes = new List<string>() {
  845. RobotInfo.PlcRobotParameter.CommandParameter.X,
  846. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  847. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  848. RobotInfo.PlcRobotParameter.CommandParameter.U,
  849. RobotInfo.PlcRobotParameter.CommandParameter.Limz,
  850. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  851. };
  852. List<object> values = new List<object>()
  853. {
  854. position.X,
  855. position.Y,
  856. position.Z,
  857. position.U,
  858. limz,
  859. false,
  860. };
  861. for (int i = 0; i < 3; i++)
  862. {
  863. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  864. {
  865. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  866. {
  867. Stopwatch sw = new Stopwatch();
  868. sw.Start();
  869. while (true)
  870. {
  871. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  872. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  873. {
  874. sw.Stop();
  875. return true;
  876. }
  877. if (sw.Elapsed.TotalMilliseconds > Timeout)
  878. {
  879. sw.Stop();
  880. return false;
  881. }
  882. }
  883. }
  884. }
  885. }
  886. return false;
  887. }
  888. catch (Exception ex)
  889. {
  890. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  891. CanExecute = true;
  892. throw ex;
  893. }
  894. finally
  895. {
  896. CanExecute = true;
  897. }
  898. }
  899. public async Task<bool> CalibMotionAsync(RPoint position, double? LimZ)
  900. {
  901. if (Plc == null || !Plc.IsConnected)
  902. {
  903. return false;
  904. }
  905. return await Task.Run(() =>
  906. {
  907. try
  908. {
  909. CanExecute = false;
  910. float limz = LimZ == null ? 0f : (float)LimZ;
  911. List<string> nodes = new List<string>() {
  912. RobotInfo.PlcRobotParameter.CommandParameter.X,
  913. RobotInfo.PlcRobotParameter.CommandParameter.Y,
  914. RobotInfo.PlcRobotParameter.CommandParameter.Z,
  915. RobotInfo.PlcRobotParameter.CommandParameter.U,
  916. RobotInfo.PlcRobotParameter.CommandParameter.Limz,
  917. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  918. };
  919. List<object> values = new List<object>()
  920. {
  921. position.X,
  922. position.Y,
  923. position.Z,
  924. position.U,
  925. position.U,
  926. false,
  927. };
  928. for (int i = 0; i < 3; i++)
  929. {
  930. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  931. {
  932. if (Plc.OpcUaClient.WriteNode<bool>(RobotInfo.PlcRobotParameter.CommandParameter.ExecuteMove, true))
  933. {
  934. Stopwatch sw = new Stopwatch();
  935. sw.Start();
  936. while (true)
  937. {
  938. var data = Plc.OpcUaClient.ReadNode(RobotInfo.PlcRobotParameter.StateParameter.MoveFinish);
  939. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  940. {
  941. sw.Stop();
  942. return true;
  943. }
  944. if (sw.Elapsed.TotalMilliseconds > Timeout)
  945. {
  946. sw.Stop();
  947. return false;
  948. }
  949. }
  950. }
  951. }
  952. }
  953. return false;
  954. }
  955. catch (Exception ex)
  956. {
  957. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  958. CanExecute = true;
  959. throw ex;
  960. }
  961. finally
  962. {
  963. CanExecute = true;
  964. }
  965. });
  966. }
  967. public bool CalibOutIO(bool state)
  968. {
  969. if (Plc == null || !Plc.IsConnected)
  970. {
  971. return false;
  972. }
  973. try
  974. {
  975. for (int i = 0; i < 3; i++)
  976. {
  977. string node = state ? RobotInfo.PlcRobotParameter.CommandParameter.VacuumOn :
  978. RobotInfo.PlcRobotParameter.CommandParameter.VacuumOff;
  979. if (Plc.OpcUaClient.WriteNode(node, true))
  980. {
  981. Stopwatch sw = new Stopwatch();
  982. sw.Start();
  983. while (true)
  984. {
  985. string node1 = state ? RobotInfo.PlcRobotParameter.StateParameter.VacuumOn : RobotInfo.PlcRobotParameter.StateParameter.VacuumOff;
  986. var data = Plc.OpcUaClient.ReadNode(node1);
  987. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  988. {
  989. sw.Stop();
  990. Plc.OpcUaClient.WriteNode(node, false);
  991. return true;
  992. }
  993. if (sw.Elapsed.TotalMilliseconds > Timeout)
  994. {
  995. sw.Stop();
  996. Plc.OpcUaClient.WriteNode(node, false);
  997. return false;
  998. }
  999. }
  1000. }
  1001. }
  1002. return false;
  1003. }
  1004. catch (Exception ex)
  1005. {
  1006. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1007. throw ex;
  1008. }
  1009. }
  1010. public async Task<bool> CalibOutIOAsync(bool state)
  1011. {
  1012. if (Plc == null || !Plc.IsConnected)
  1013. {
  1014. return false;
  1015. }
  1016. return await Task.Run(() =>
  1017. {
  1018. try
  1019. {
  1020. for (int i = 0; i < 3; i++)
  1021. {
  1022. string node = state ? RobotInfo.PlcRobotParameter.CommandParameter.VacuumOn :
  1023. RobotInfo.PlcRobotParameter.CommandParameter.VacuumOff;
  1024. if (Plc.OpcUaClient.WriteNode(node, true))
  1025. {
  1026. Stopwatch sw = new Stopwatch();
  1027. sw.Start();
  1028. while (true)
  1029. {
  1030. string node1 = state ? RobotInfo.PlcRobotParameter.StateParameter.VacuumOn : RobotInfo.PlcRobotParameter.StateParameter.VacuumOff;
  1031. var data = Plc.OpcUaClient.ReadNode(node1);
  1032. if (StatusCode.IsGood(data.StatusCode) && (bool)data.Value)
  1033. {
  1034. sw.Stop();
  1035. Plc.OpcUaClient.WriteNode(node, false);
  1036. return true;
  1037. }
  1038. if (sw.Elapsed.TotalMilliseconds > Timeout)
  1039. {
  1040. sw.Stop();
  1041. Plc.OpcUaClient.WriteNode(node, false);
  1042. return false;
  1043. }
  1044. }
  1045. }
  1046. }
  1047. return false;
  1048. }
  1049. catch (Exception ex)
  1050. {
  1051. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1052. throw ex;
  1053. }
  1054. });
  1055. }
  1056. public bool CalibParame(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  1057. {
  1058. if (Plc == null || !Plc.IsConnected)
  1059. {
  1060. return false;
  1061. }
  1062. try
  1063. {
  1064. List<string> nodes = new List<string>() {
  1065. RobotInfo.PlcRobotParameter.CommandParameter.Speed,
  1066. RobotInfo.PlcRobotParameter.CommandParameter.Accel,
  1067. RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOn,
  1068. RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOff,
  1069. };
  1070. List<object> values = new List<object>()
  1071. {
  1072. Speed,
  1073. Accel,
  1074. WaitSuction,
  1075. WaitBlow,
  1076. };
  1077. for (int i = 0; i < 3; i++)
  1078. {
  1079. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  1080. {
  1081. return true;
  1082. }
  1083. }
  1084. return false;
  1085. }
  1086. catch (Exception ex)
  1087. {
  1088. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1089. throw ex;
  1090. }
  1091. }
  1092. public async Task<bool> CalibParameAsync(PickInfo Pick, int Speed, int Accel, bool Power, int WaitSuction, int WaitBlow)
  1093. {
  1094. if (Plc == null || !Plc.IsConnected)
  1095. {
  1096. return false;
  1097. }
  1098. return await Task.Run(() =>
  1099. {
  1100. try
  1101. {
  1102. List<string> nodes = new List<string>() {
  1103. RobotInfo.PlcRobotParameter.CommandParameter.Speed,
  1104. RobotInfo.PlcRobotParameter.CommandParameter.Accel,
  1105. RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOn,
  1106. RobotInfo.PlcRobotParameter.CommandParameter.WaitVacuumOff,
  1107. };
  1108. List<object> values = new List<object>()
  1109. {
  1110. Speed,
  1111. Accel,
  1112. WaitSuction,
  1113. WaitBlow,
  1114. };
  1115. for (int i = 0; i < 3; i++)
  1116. {
  1117. if (Plc.OpcUaClient.WriteNodes(nodes.ToArray(), values.ToArray()))
  1118. {
  1119. return true;
  1120. }
  1121. }
  1122. return false;
  1123. }
  1124. catch (Exception ex)
  1125. {
  1126. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1127. throw ex;
  1128. }
  1129. });
  1130. }
  1131. public List<DataValue> GetNodesValue()
  1132. {
  1133. if (Plc == null || !Plc.IsConnected)
  1134. {
  1135. return new List<DataValue>();
  1136. }
  1137. try
  1138. {
  1139. CanExecute = false;
  1140. List<NodeId> nodes = new List<NodeId>() {
  1141. RobotInfo.PlcRobotParameter.StateParameter.X,
  1142. RobotInfo.PlcRobotParameter.StateParameter.Y,
  1143. RobotInfo.PlcRobotParameter.StateParameter.Z,
  1144. RobotInfo.PlcRobotParameter.StateParameter.U,
  1145. RobotInfo.PlcRobotParameter.StateParameter.MoveFinish,
  1146. RobotInfo.PlcRobotParameter.StateParameter.Error,
  1147. RobotInfo.PlcRobotParameter.StateParameter.VacuumOn,
  1148. RobotInfo.PlcRobotParameter.StateParameter.VacuumOff
  1149. };
  1150. List<DataValue> Results = new List<DataValue>();
  1151. for (int i = 0; i < 3; i++)
  1152. {
  1153. Results = Plc.OpcUaClient.ReadNodes(nodes.ToArray());
  1154. if (Results.Count != 0)
  1155. {
  1156. return Results;
  1157. }
  1158. }
  1159. return new List<DataValue>();
  1160. }
  1161. catch (Exception ex)
  1162. {
  1163. LogHelper.WriteLogError("执行机器人命令时出错!", ex);
  1164. CanExecute = true;
  1165. throw ex;
  1166. }
  1167. finally
  1168. {
  1169. CanExecute = true;
  1170. }
  1171. }
  1172. /// <summary>
  1173. /// 设置工具坐标
  1174. /// </summary>
  1175. /// <param name="index"></param>
  1176. /// <param name="x"></param>
  1177. /// <param name="y"></param>
  1178. /// <returns></returns>
  1179. public bool SetTool(int index, double x, double y)
  1180. {
  1181. return true;
  1182. }
  1183. /// <summary>
  1184. /// 设置工具坐标
  1185. /// </summary>
  1186. /// <param name="index"></param>
  1187. /// <param name="x"></param>
  1188. /// <param name="y"></param>
  1189. /// <returns></returns>
  1190. public async Task<bool> SetToolAsync(int index, double x, double y)
  1191. {
  1192. return true;
  1193. }
  1194. /// <summary>
  1195. /// 选择工具坐标
  1196. /// </summary>
  1197. /// <param name="index"></param>
  1198. /// <returns></returns>
  1199. public bool SelectTool(int index)
  1200. {
  1201. return true;
  1202. }
  1203. /// <summary>
  1204. /// 选择工具坐标
  1205. /// </summary>
  1206. /// <param name="index"></param>
  1207. /// <returns></returns>
  1208. public async Task<bool> SelectToolAsync(int index)
  1209. {
  1210. return true;
  1211. }
  1212. #endregion
  1213. #region plc触发信号监控
  1214. private bool isTrigger = false;
  1215. public bool IsTrigger { get => isTrigger; set => isTrigger = value; }
  1216. public Thread CreateThreadToTrigger(NodeId[]Keys, List<ProcedureOutputOpcAdr> outputadr)
  1217. {
  1218. Thread TriggerTherad = new Thread(() =>
  1219. {
  1220. List<DataValue> Results = new List<DataValue>();
  1221. List<DataValue> ResultsT = new List<DataValue>();
  1222. while (IsTrigger)
  1223. {
  1224. try
  1225. {
  1226. if (Plc.IsConnected)
  1227. {
  1228. Results = Plc.OpcUaClient.ReadNodes(Keys);
  1229. if (ResultsT.Count != Results.Count)
  1230. {
  1231. ResultsT.Clear();
  1232. for (int i = 0; i < Results.Count; i++)
  1233. {
  1234. ResultsT.Add(new DataValue() { Value = Results[i].Value });
  1235. if (Results[i].StatusCode.Code != 2150891520)
  1236. {
  1237. if ((bool)Results[i].Value)
  1238. {
  1239. TrggerChangeEvent?.Invoke(Id, Keys[i], outputadr);
  1240. }
  1241. }
  1242. }
  1243. }
  1244. else
  1245. {
  1246. for (int i = 0; i < Results.Count; i++)
  1247. {
  1248. if (Results[i].StatusCode.Code != 2150891520)
  1249. {
  1250. if ((bool)Results[i].Value && (bool)Results[i].Value != (bool)ResultsT[i].Value)
  1251. {
  1252. TrggerChangeEvent?.Invoke(Id, Keys[i], outputadr);
  1253. if (TrggerChangeEvent != null)
  1254. {
  1255. Delegate[] aaa = TrggerChangeEvent.GetInvocationList();
  1256. }
  1257. }
  1258. ResultsT[i].Value = Results[i].Value;
  1259. }
  1260. }
  1261. }
  1262. }
  1263. }
  1264. catch (Exception ex)
  1265. {
  1266. Thread.Sleep(5000);
  1267. }
  1268. Thread.Sleep(50);
  1269. }
  1270. });
  1271. TriggerTherad.IsBackground = true;
  1272. TriggerTherad.Name = Plc.Name + "触发检测" + Keys[0];
  1273. TriggerTherad.Start();
  1274. return TriggerTherad;
  1275. }
  1276. #endregion
  1277. #region 收发数据
  1278. /// <summary>
  1279. /// 发送并接收数据
  1280. /// </summary>
  1281. /// <param name="send"></param>
  1282. /// <returns></returns>
  1283. public string SendAndReceive(string send)
  1284. {
  1285. return string.Empty;
  1286. }
  1287. /// <summary>
  1288. /// 发送并接收数据
  1289. /// </summary>
  1290. /// <param name="send"></param>
  1291. /// <returns></returns>
  1292. public Task<string> SendAndReceiveAsync(string send)
  1293. {
  1294. return Task.FromResult(string.Empty);
  1295. }
  1296. /// <summary>
  1297. /// 发送并接收数据
  1298. /// </summary>
  1299. /// <param name="send"></param>
  1300. /// <returns></returns>
  1301. public string SendAndReceive(ITcpSessionClient client, string send)
  1302. {
  1303. return string.Empty;
  1304. }
  1305. /// <summary>
  1306. /// 发送并接收数据
  1307. /// </summary>
  1308. /// <param name="send"></param>
  1309. /// <returns></returns>
  1310. public Task<string> SendAndReceiveAsync(ITcpSessionClient client, string send)
  1311. {
  1312. return Task.FromResult(string.Empty);
  1313. }
  1314. public void Send(string send)
  1315. {
  1316. }
  1317. public Task SendAsync(string send)
  1318. {
  1319. return Task.CompletedTask;
  1320. }
  1321. public void Send(ITcpSessionClient client, string send)
  1322. {
  1323. }
  1324. public Task SendAsync(ITcpSessionClient client, string send)
  1325. {
  1326. return Task.CompletedTask;
  1327. }
  1328. /// <summary>
  1329. /// 获取编码
  1330. /// </summary>
  1331. /// <param name="dataEncoding"></param>
  1332. /// <returns></returns>
  1333. public Encoding GetEncoding()
  1334. {
  1335. DataEncoding dataEncoding = this.DataEncoding;
  1336. Encoding encoding;
  1337. if (dataEncoding == DataEncoding.Default)
  1338. {
  1339. encoding = Encoding.Default;
  1340. }
  1341. else if (dataEncoding == DataEncoding.ASCII)
  1342. {
  1343. encoding = Encoding.ASCII;
  1344. }
  1345. else if (dataEncoding == DataEncoding.UTF7)
  1346. {
  1347. encoding = Encoding.UTF7;
  1348. }
  1349. else if (dataEncoding == DataEncoding.UTF8)
  1350. {
  1351. encoding = Encoding.UTF8;
  1352. }
  1353. else if (dataEncoding == DataEncoding.UTF32)
  1354. {
  1355. encoding = Encoding.UTF32;
  1356. }
  1357. else if (dataEncoding == DataEncoding.Unicode)
  1358. {
  1359. encoding = Encoding.Unicode;
  1360. }
  1361. else if (dataEncoding == DataEncoding.BigEndianUnicode)
  1362. {
  1363. encoding = Encoding.BigEndianUnicode;
  1364. }
  1365. else if (dataEncoding == DataEncoding.GB2312)
  1366. {
  1367. encoding = Encoding.GetEncoding("gb2312");
  1368. }
  1369. else
  1370. {
  1371. encoding = Encoding.Default;
  1372. }
  1373. return encoding;
  1374. }
  1375. #endregion
  1376. #region 属性通知
  1377. /// <summary>
  1378. /// Occurs when a property value changes.
  1379. /// </summary>
  1380. public event PropertyChangedEventHandler PropertyChanged;
  1381. /// <summary>
  1382. /// Checks if a property already matches a desired value. Sets the property and
  1383. /// notifies listeners only when necessary.
  1384. /// </summary>
  1385. /// <typeparam name="T">Type of the property.</typeparam>
  1386. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1387. /// <param name="value">Desired value for the property.</param>
  1388. /// <param name="propertyName">Name of the property used to notify listeners. This
  1389. /// value is optional and can be provided automatically when invoked from compilers that
  1390. /// support CallerMemberName.</param>
  1391. /// <returns>True if the value was changed, false if the existing value matched the
  1392. /// desired value.</returns>
  1393. protected virtual bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
  1394. {
  1395. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1396. storage = value;
  1397. RaisePropertyChanged(propertyName);
  1398. return true;
  1399. }
  1400. /// <summary>
  1401. /// Checks if a property already matches a desired value. Sets the property and
  1402. /// notifies listeners only when necessary.
  1403. /// </summary>
  1404. /// <typeparam name="T">Type of the property.</typeparam>
  1405. /// <param name="storage">Reference to a property with both getter and setter.</param>
  1406. /// <param name="value">Desired value for the property.</param>
  1407. /// <param name="propertyName">Name of the property used to notify listeners. This
  1408. /// value is optional and can be provided automatically when invoked from compilers that
  1409. /// support CallerMemberName.</param>
  1410. /// <param name="onChanged">Action that is called after the property value has been changed.</param>
  1411. /// <returns>True if the value was changed, false if the existing value matched the
  1412. /// desired value.</returns>
  1413. protected virtual bool SetProperty<T>(ref T storage, T value, Action onChanged, [CallerMemberName] string propertyName = null)
  1414. {
  1415. if (EqualityComparer<T>.Default.Equals(storage, value)) return false;
  1416. storage = value;
  1417. onChanged?.Invoke();
  1418. RaisePropertyChanged(propertyName);
  1419. return true;
  1420. }
  1421. /// <summary>
  1422. /// Raises this object's PropertyChanged event.
  1423. /// </summary>
  1424. /// <param name="propertyName">Name of the property used to notify listeners. This
  1425. /// value is optional and can be provided automatically when invoked from compilers
  1426. /// that support <see cref="CallerMemberNameAttribute"/>.</param>
  1427. protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
  1428. {
  1429. OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  1430. }
  1431. /// <summary>
  1432. /// Raises this object's PropertyChanged event.
  1433. /// </summary>
  1434. /// <param name="args">The PropertyChangedEventArgs</param>
  1435. protected virtual void OnPropertyChanged(PropertyChangedEventArgs args)
  1436. {
  1437. PropertyChanged?.Invoke(this, args);
  1438. }
  1439. #endregion
  1440. }
  1441. }