SkySGMPumpConnection.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using Aitex.Core.RT.Log;
  2. using MECF.Framework.Common.Communications;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.Pumps.SkySGMPump
  9. {
  10. public class SkySGMPumpMessage : AsciiMessage
  11. {
  12. public string Data { get; set; }
  13. public string Address { get; set; }
  14. public string ErrorText { get; set; }
  15. }
  16. public class SkySGMPumpConnection: TCPPortConnectionBase
  17. {
  18. private bool _isAckMode = false;//should read from config file
  19. public SkySGMPumpConnection(string adress, string endof) : base(adress, endof, true)
  20. {
  21. }
  22. public override bool SendMessage(byte[] message)
  23. {
  24. return base.SendMessage(message);
  25. }
  26. protected override void ActiveHandlerProceedMessage(MessageBase msg)
  27. {
  28. lock (_lockerActiveHandler)
  29. {
  30. if (_activeHandler != null)
  31. {
  32. if (msg.IsFormatError || (_activeHandler.HandleMessage(msg, out bool transactionComplete) && transactionComplete))
  33. {
  34. _activeHandler = null;
  35. }
  36. }
  37. }
  38. }
  39. protected override MessageBase ParseResponse(string rawText)
  40. {
  41. SkySGMPumpMessage msg = new SkySGMPumpMessage();
  42. msg.RawMessage = rawText;
  43. if (rawText.Length <= 0)
  44. {
  45. LOG.Error($"empty response,");
  46. msg.IsFormatError = true;
  47. return msg;
  48. }
  49. if (rawText.Length <= 4)
  50. {
  51. LOG.Error($"too short response,");
  52. msg.IsFormatError = true;
  53. return msg;
  54. }
  55. rawText = rawText.Substring(3, rawText.Length - 3);//remove start char and end char
  56. //msg.Address = rawText.Substring(0, 2);
  57. msg.Data = rawText.Substring(0,rawText.Length-2);
  58. msg.IsResponse = true;
  59. msg.IsAck = true;
  60. return msg;
  61. }
  62. }
  63. }