TazmoRobotConnection.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Ports;
  4. using System.Linq;
  5. using System.Text;
  6. using Aitex.Core.RT.Log;
  7. using MECF.Framework.Common.Communications;
  8. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Robots.TazmoRobot
  9. {
  10. public class TazmoAlignerMessage : AsciiMessage
  11. {
  12. public string Data { get; set; }
  13. }
  14. public class TazmoRobotMessageBin : BinaryMessage
  15. {
  16. public byte[] CMD { get; set; }
  17. public byte[] Data { get; set; }
  18. }
  19. public class TazmoRobotConnection : SerialPortConnectionBase
  20. {
  21. private List<byte> _lstCacheBuffer = new List<byte>();
  22. private TazmoRobot _device;
  23. public TazmoRobotConnection(TazmoRobot device, string portName, int baudRate = 9600, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  24. : base(portName, baudRate, dataBits, parity, stopBits, "\r", false)
  25. {
  26. _device = device;
  27. }
  28. public override bool SendMessage(byte[] message)
  29. {
  30. LOG.Write($"{Address} send message:{Encoding.ASCII.GetString(message)}");
  31. _lstCacheBuffer.Clear();
  32. return base.SendMessage(message);
  33. }
  34. protected override MessageBase ParseResponse(byte[] rawBuffer)
  35. {
  36. _lstCacheBuffer.AddRange(rawBuffer);
  37. byte[] temps = _lstCacheBuffer.ToArray();
  38. TazmoRobotMessageBin msg = new TazmoRobotMessageBin();
  39. msg.RawMessage = _lstCacheBuffer.ToArray();
  40. if (temps[0] == 0x6)
  41. {
  42. msg.IsAck = true;
  43. _lstCacheBuffer.RemoveAt(0);
  44. }
  45. if (temps.Length == 1 && temps[0] == 0x15)
  46. {
  47. msg.IsNak = true;
  48. _lstCacheBuffer.Clear();
  49. }
  50. if (temps.Length == 1 && temps[0] == 0x11)
  51. {
  52. msg.IsBusy = true;
  53. _lstCacheBuffer.Clear();
  54. }
  55. if (temps.Length >= 3 && (Encoding.Default.GetString(temps.Take(3).ToArray()) == "ERR") && temps.LastOrDefault() == 0xD)
  56. {
  57. int index1 = Array.IndexOf(temps, (byte)44);
  58. int index2 = Array.IndexOf(temps, (byte)0xD);
  59. msg.Data = temps.Skip(index1 + 1).Take(index2 - index1 - 1).ToArray();
  60. msg.IsError = true;
  61. _lstCacheBuffer.Clear();
  62. }
  63. if (temps.Length > 3 && temps.LastOrDefault() == 0xD && Encoding.Default.GetString(temps.Take(3).ToArray()) != "ERR")
  64. {
  65. _lstCacheBuffer.Clear();
  66. msg.IsResponse = true;
  67. if (Array.IndexOf(temps, (byte)44) != -1) // to ,
  68. {
  69. msg.CMD = temps.Take(Array.IndexOf(temps, (byte)44)).ToArray();
  70. int index1 = Array.IndexOf(temps, (byte)44);
  71. int index2 = Array.IndexOf(temps, (byte)0xD);
  72. msg.Data = temps.Skip(index1 + 1).Take(index2 - index1 - 1).ToArray();
  73. }
  74. else
  75. {
  76. msg.CMD = temps.Take(temps.Length - 1).ToArray();
  77. }
  78. }
  79. LOG.Write($"{Address} received message:{Encoding.ASCII.GetString(temps)}");
  80. return msg;
  81. }
  82. }
  83. }