JelC5000RobotConnection.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System.Collections.Generic;
  2. using System.IO.Ports;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.RT.Log;
  6. using MECF.Framework.Common.Communications;
  7. using MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.RobotBase;
  8. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.JEL
  9. {
  10. public class JelC5000RobotMessage : AsciiMessage
  11. {
  12. public string Data { get; set; }
  13. public string ErrorText { get; set; }
  14. }
  15. public class JelC5000RobotConnection : SerialPortConnectionBase
  16. {
  17. //private bool _isAckMode = false;//should read from config file
  18. private string _cachedBuffer = string.Empty;
  19. private List<byte> _lstCacheBuffer = new List<byte>();
  20. private RobotBaseDevice m_Robot;
  21. public JelC5000RobotConnection(RobotBaseDevice robot, string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  22. : base(portName, baudRate, dataBits, parity, stopBits,"", true)
  23. {
  24. m_Robot = robot;
  25. }
  26. public override bool SendMessage(byte[] message)
  27. {
  28. _lstCacheBuffer.Clear();
  29. return base.SendMessage(message);
  30. }
  31. public override bool SendMessage(string message)
  32. {
  33. _cachedBuffer = string.Empty;
  34. while (m_Robot.CommandMessages.Count > 50)
  35. m_Robot.CommandMessages.RemoveLast();
  36. m_Robot.CommandMessages.AddFirst($"Send:{message.Replace("\r","")}");
  37. return base.SendMessage(message);
  38. }
  39. protected override MessageBase ParseResponse(string rawmsg)
  40. {
  41. _cachedBuffer += rawmsg;
  42. JelC5000RobotMessage msg = new JelC5000RobotMessage();
  43. msg.RawMessage = _cachedBuffer;
  44. if (_activeHandler == null)
  45. {
  46. _cachedBuffer = string.Empty;
  47. return msg;
  48. }
  49. if (_cachedBuffer == ">" && (_activeHandler.GetType() != typeof(JelC5000RobotReadHandler)))
  50. {
  51. msg.IsAck = true;
  52. msg.Data = _cachedBuffer.Replace(">", "").Replace("\r", "");
  53. while (m_Robot.CommandMessages.Count > 50)
  54. m_Robot.CommandMessages.RemoveLast();
  55. m_Robot.CommandMessages.AddFirst($"Received:{_cachedBuffer.Replace("\r", "")}");
  56. _cachedBuffer = string.Empty;
  57. }
  58. else if(_cachedBuffer.Contains("\r"))
  59. {
  60. msg.IsAck = true;
  61. msg.IsResponse = true;
  62. msg.Data = _cachedBuffer.Replace(">", "").Replace("\r", "");
  63. while (m_Robot.CommandMessages.Count > 50)
  64. m_Robot.CommandMessages.RemoveLast();
  65. m_Robot.CommandMessages.AddFirst($"Received:{_cachedBuffer.Replace("\r", "")}");
  66. _cachedBuffer = string.Empty;
  67. }
  68. //else if(_cachedBuffer.Contains("\r"))
  69. //{
  70. // msg.IsAck = true;
  71. // msg.Data = rawmsg.Replace(">", "").Replace("\r", "");
  72. // _cachedBuffer = string.Empty;
  73. //}
  74. return msg;
  75. }
  76. }
  77. }