SPaceTRanRobotConnection.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.SCCore;
  10. using MECF.Framework.Common.Communications;
  11. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.SUNWAY
  12. {
  13. public class SPaceTRanRobotMessage : AsciiMessage
  14. {
  15. public int UNo { get; set; }
  16. public int SeqNo { get; set; }
  17. public string Status { get; set; }
  18. public string Ackcd { get; set; }
  19. public string Command { get; set; }
  20. public string[] Data { get; set; }
  21. public string ErrorCode { get; set; }
  22. public string EvNo { get; set; }
  23. public string EvDate { get; set; }
  24. public string EvData { get; set; }
  25. }
  26. public class SPaceTRanRobotConnection : TCPPortConnectionBase
  27. {
  28. public const string ERROR = "_ERR";
  29. public const string RDY = "_RDY";
  30. public const string CMDToken = " ";
  31. private SPaceTRanRobot _robot;
  32. private readonly Regex _rex_error_code = new Regex(@"_ERR\s+(\d+)\s*");
  33. public SPaceTRanRobotConnection(SPaceTRanRobot robot, string ipaddress): base(ipaddress, "\r", true)
  34. {
  35. _robot = robot;
  36. }
  37. protected override MessageBase ParseResponse(string rawMessage)
  38. {
  39. SPaceTRanRobotMessage msg = new SPaceTRanRobotMessage();
  40. try
  41. {
  42. msg.RawMessage = rawMessage.Replace("\r", "").Trim();
  43. msg.IsAck = true;
  44. msg.IsComplete = false;
  45. msg.IsFormatError = false;
  46. msg.IsEvent = false;
  47. if (_rex_error_code.IsMatch(rawMessage))
  48. {
  49. msg.IsAck = false;
  50. msg.IsComplete = false;
  51. msg.IsError = true;
  52. var results = _rex_error_code.Match(rawMessage);
  53. msg.ErrorCode = results.Groups[1].Value;
  54. _robot.OnError($"Execution error,Error code is {msg.ErrorCode}");
  55. }
  56. else if (rawMessage.Trim() == RDY || (rawMessage.Contains(RDY) && !rawMessage.Contains(ERROR)) || rawMessage.Contains("LOAD"))
  57. {
  58. msg.IsComplete = (rawMessage.Trim() == RDY) || (rawMessage.Contains(RDY) && !rawMessage.Contains(ERROR));
  59. msg.IsError = false;
  60. }
  61. else
  62. {
  63. msg.IsFormatError = true;
  64. msg.IsAck = false;
  65. msg.IsComplete = false;
  66. }
  67. return msg;
  68. }
  69. catch (Exception ex)
  70. {
  71. LOG.Write(ex);
  72. msg.IsFormatError = true;
  73. return msg;
  74. }
  75. }
  76. }
  77. }