Mag7Robot.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using System.Text.RegularExpressions;
  4. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot.MAG7
  5. {
  6. public class Mag7Robot : Robot
  7. {
  8. public override bool Error
  9. {
  10. get
  11. {
  12. return _commErr || _exceuteErr;
  13. }
  14. }
  15. public Mag7Robot(string module, string address)
  16. : base(module, module, module, module, address, RobotType.MAG7)
  17. {
  18. }
  19. public override void OnDataChanged(string package)
  20. {
  21. try
  22. {
  23. package = package.ToUpper();
  24. string[] msgs = Regex.Split(package, delimiter);
  25. foreach (string msg in msgs)
  26. {
  27. if (msg.Length > 0)
  28. {
  29. bool completed = false;
  30. string resp = msg;
  31. lock (_locker)
  32. {
  33. if (_foregroundHandler != null && _foregroundHandler.OnMessage(ref _socket, resp, out completed))
  34. {
  35. if (completed)
  36. {
  37. _foregroundHandler = null;
  38. }
  39. }
  40. else if (_backgroundHandler != null && _backgroundHandler.OnMessage(ref _socket, resp, out completed))
  41. {
  42. if (completed)
  43. {
  44. string reason = string.Empty;
  45. QueryState(out reason);
  46. _backgroundHandler = null;
  47. }
  48. }
  49. else
  50. {
  51. if (_eventHandler != null)
  52. {
  53. if (_eventHandler.OnMessage(ref _socket, resp, out completed))
  54. {
  55. if (completed)
  56. {
  57. EV.PostMessage("Robot", EventEnum.DefaultWarning, string.Format(" has error. {0:X}", ErrorCode));
  58. _exceuteErr = true;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. catch (ExcuteFailedException e)
  68. {
  69. EV.PostMessage("Robot", EventEnum.DefaultWarning, string.Format("Robot execute failed, {0}", e.Message));
  70. _exceuteErr = true;
  71. if (_foregroundHandler != null)
  72. {
  73. _foregroundHandler = null;
  74. }
  75. else if (_backgroundHandler != null)
  76. {
  77. _backgroundHandler = null;
  78. }
  79. }
  80. catch (InvalidPackageException e)
  81. {
  82. EV.PostMessage("Robot", EventEnum.DefaultWarning, string.Format("receive invalid package. {0}", e.Message));
  83. }
  84. catch (System.Exception ex)
  85. {
  86. _commErr = true;
  87. LOG.Write("Robot failed:" + ex.ToString());
  88. }
  89. }
  90. public override void Reset()
  91. {
  92. _exceuteErr = false;
  93. if (_commErr)
  94. {
  95. Connect();
  96. }
  97. Swap = false;
  98. }
  99. }
  100. }