JelRobotConnection.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.JEL
  8. {
  9. public class JelRobotMessage : AsciiMessage
  10. {
  11. public string Data { get; set; }
  12. public string ErrorText { get; set; }
  13. }
  14. public class JelRobotConnection : SerialPortConnectionBase
  15. {
  16. //private bool _isAckMode = false;//should read from config file
  17. private string _cachedBuffer = string.Empty;
  18. private List<byte> _lstCacheBuffer = new List<byte>();
  19. public JelRobot Robot;
  20. public JelRobotConnection(string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  21. : base(portName, baudRate, dataBits, parity, stopBits, "", true)
  22. {
  23. }
  24. public override bool SendMessage(byte[] message)
  25. {
  26. _lstCacheBuffer.Clear();
  27. return base.SendMessage(message);
  28. }
  29. public override bool SendMessage(string message)
  30. {
  31. _cachedBuffer = string.Empty;
  32. return base.SendMessage(message);
  33. }
  34. protected override MessageBase ParseResponse(string rawmsg)
  35. {
  36. _cachedBuffer += rawmsg;
  37. JelRobotMessage msg = new JelRobotMessage();
  38. if (_cachedBuffer == ">")
  39. {
  40. msg.IsAck = true;
  41. msg.Data = _cachedBuffer.Replace(">", "").Replace("\r", "");
  42. _cachedBuffer = string.Empty;
  43. }
  44. else if (_cachedBuffer.Contains("\r"))
  45. {
  46. msg.IsAck = true;
  47. msg.IsResponse = true;
  48. msg.Data = _cachedBuffer.Replace(">", "").Replace("\r", "");
  49. _cachedBuffer = string.Empty;
  50. }
  51. //else if(_cachedBuffer.Contains("\r"))
  52. //{
  53. // msg.IsAck = true;
  54. // msg.Data = rawmsg.Replace(">", "").Replace("\r", "");
  55. // _cachedBuffer = string.Empty;
  56. //}
  57. if (msg.Data != null && msg.Data.Contains("EVT:"))
  58. Robot.OnEventReceived(msg);
  59. return msg;
  60. }
  61. }
  62. }