JelC5000RobotFlippeConnection.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 JelC5000RobotFlippeMessage : AsciiMessage
  11. {
  12. public string Data { get; set; }
  13. public string ErrorText { get; set; }
  14. }
  15. public class JelC5000RobotFlippeConnection : 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 JelC5000RobotFlippeConnection(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. JelC5000RobotFlippeMessage msg = new JelC5000RobotFlippeMessage();
  43. if (_activeHandler == null)
  44. {
  45. _cachedBuffer = string.Empty;
  46. return msg;
  47. }
  48. if (_cachedBuffer == ">" && (_activeHandler.GetType() != typeof(JelC5000RobotFlippeReadHandler)))
  49. {
  50. msg.IsAck = true;
  51. msg.Data = _cachedBuffer.Replace(">", "").Replace("\r", "");
  52. while (m_Robot.CommandMessages.Count > 50)
  53. m_Robot.CommandMessages.RemoveLast();
  54. m_Robot.CommandMessages.AddFirst($"Received:{_cachedBuffer.Replace("\r", "")}");
  55. _cachedBuffer = string.Empty;
  56. }
  57. else if(_cachedBuffer.Contains("\r"))
  58. {
  59. msg.IsAck = true;
  60. msg.IsResponse = true;
  61. msg.Data = _cachedBuffer.Replace(">", "").Replace("\r", "");
  62. while (m_Robot.CommandMessages.Count > 50)
  63. m_Robot.CommandMessages.RemoveLast();
  64. m_Robot.CommandMessages.AddFirst($"Received:{_cachedBuffer.Replace("\r", "")}");
  65. _cachedBuffer = string.Empty;
  66. }
  67. //else if(_cachedBuffer.Contains("\r"))
  68. //{
  69. // msg.IsAck = true;
  70. // msg.Data = rawmsg.Replace(">", "").Replace("\r", "");
  71. // _cachedBuffer = string.Empty;
  72. //}
  73. return msg;
  74. }
  75. }
  76. }