OsakaPumpTGkineConnection.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.Pumps.OsakaPumpTGkine
  8. {
  9. public class OsakaPumpTGkineMessage : BinaryMessage
  10. {
  11. public int Length { get; set; }
  12. }
  13. public class OsakaPumpTGkineConnection : SerialPortConnectionBase
  14. {
  15. private List<byte> _lstCacheBuffer = new List<byte>();
  16. public OsakaPumpTGkineConnection(string portName, int baudRate = 115200, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
  17. : base(portName, baudRate, dataBits, parity, stopBits, "", false)
  18. {
  19. }
  20. public override bool SendMessage(byte[] message)
  21. {
  22. _lstCacheBuffer.Clear();
  23. return base.SendMessage(message);
  24. }
  25. protected override MessageBase ParseResponse(byte[] rawMessage)
  26. {
  27. _lstCacheBuffer.AddRange(rawMessage);
  28. byte[] temps = _lstCacheBuffer.ToArray();
  29. OsakaPumpTGkineMessage msg = new OsakaPumpTGkineMessage();
  30. msg.IsResponse = false;
  31. msg.IsAck = false;
  32. msg.IsComplete = false;
  33. msg.RawMessage = _lstCacheBuffer.ToArray();
  34. if (temps.Length < 8)
  35. return msg;
  36. if (temps.Length >= 8 && temps[6] == 0x03 && temps[7] == 0x03)
  37. {
  38. msg.IsResponse = true;
  39. msg.Length = 8;
  40. msg.RawMessage = _lstCacheBuffer.Take(8).ToArray();
  41. _lstCacheBuffer.RemoveRange(0, msg.Length);
  42. }
  43. else if (temps.Length >= 10 && temps[8] == 0x03 && temps[9] == 0x03)
  44. {
  45. msg.IsResponse = true;
  46. msg.Length = 10;
  47. msg.RawMessage = _lstCacheBuffer.Take(10).ToArray();
  48. _lstCacheBuffer.RemoveRange(0, msg.Length);
  49. }
  50. return msg;
  51. }
  52. }
  53. }