HAtmConnection.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Aitex.Core.RT.Log;
  2. using MECF.Framework.Common.Communications;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robot.HineAutomation
  10. {
  11. public class HAtmMessage : AsciiMessage
  12. {
  13. public string DeviceAddress { get; set; }
  14. public string Action { get; set; }
  15. public string Parameter { get; set; }
  16. public int DataLength { get; set; }
  17. public string Data { get; set; }
  18. public string ErrorText { get; set; }
  19. public string ResponseRecordType { get; set; }
  20. public string Category { get; set; }
  21. }
  22. public class HAtmConnection : ConnectionBase
  23. {
  24. private const string ResponseRecordType = "X";
  25. public HAtmConnection(string addr) : base(addr)
  26. {
  27. }
  28. public void EnableLog(bool enable)
  29. {
  30. _socket.EnableLog = enable;
  31. }
  32. protected override void OnDataReceived(string oneLineMessage)
  33. {
  34. MessageBase msg = ParseResponse(oneLineMessage);
  35. if (msg.IsFormatError)
  36. {
  37. SetCommunicationError(true, "received invalid response message.");
  38. }
  39. lock (_lockerActiveHandler)
  40. {
  41. if (_activeHandler != null)
  42. {
  43. if (msg.IsFormatError || (((HAtmHandler)_activeHandler).HasResponse && (_activeHandler.HandleMessage(msg, out bool completed) && completed)))
  44. {
  45. _activeHandler = null;
  46. }
  47. }
  48. }
  49. }
  50. protected override MessageBase ParseResponse(string rawText)
  51. {
  52. HAtmMessage msg = new HAtmMessage();
  53. msg.IsResponse = false;
  54. msg.IsAck = false;
  55. msg.IsComplete = false;
  56. msg.RawMessage = rawText;
  57. rawText = rawText.Replace("\r", "").Replace("\n", "");
  58. string[] words = Regex.Split(rawText, ",");
  59. if (words.Length < 2 || words[0] != ResponseRecordType)
  60. {
  61. msg.IsFormatError = true;
  62. LOG.Error($"Record type check failed, " + rawText);
  63. return msg;
  64. }
  65. msg.ResponseRecordType = words[0];
  66. msg.Category = words[1];
  67. if(words.Length > 2)
  68. {
  69. msg.Data = words[2];
  70. msg.MessagePart = new string[words.Length - 2];
  71. for (int i = 0; i < msg.MessagePart.Length; i++)
  72. {
  73. msg.MessagePart[i] = words[2 + i];
  74. }
  75. }
  76. msg.IsResponse = true;
  77. msg.IsAck = true;
  78. msg.IsComplete = true;
  79. return msg;
  80. }
  81. }
  82. }