WattsineRFHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using MECF.Framework.Common.Communications;
  2. using MECF.Framework.Common.Utilities;
  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.RFs.Wattsine
  9. {
  10. public class WattsineRFHandler : HandlerBase
  11. {
  12. public WattsineRF Device { get; set; }
  13. public string _command;
  14. protected string _parameter;
  15. private static byte _cId = 0x01;
  16. public WattsineRFHandler(WattsineRF device,string command,string parameter = null) : base(BuildMessage(ToByteArray(command), ToByteArray(parameter)))
  17. {
  18. }
  19. public override bool HandleMessage(MessageBase msg, out bool transactionComplete)
  20. {
  21. WattsineRFMessage response = msg as WattsineRFMessage;
  22. ResponseMessage = msg;
  23. transactionComplete = true;
  24. return true;
  25. }
  26. private static byte[] BuildMessage(byte[] commandArray, byte[] argumentArray)
  27. {
  28. List<byte> buffer = new List<byte>();
  29. buffer.Add(_cId);
  30. if (commandArray != null && commandArray.Length > 0)
  31. {
  32. if (!(commandArray.Length == 1 && commandArray[0] == 0))
  33. {
  34. buffer.AddRange(commandArray);
  35. }
  36. }
  37. if (argumentArray != null && argumentArray.Length > 0)
  38. {
  39. buffer.AddRange(argumentArray);
  40. }
  41. var checkSum = Crc16.Crc16Ccitt(buffer.ToArray());
  42. //buffer.AddRange(BitConverter.GetBytes(checkSum));
  43. buffer.Add((byte)(checkSum >> 8));
  44. buffer.Add((byte)(checkSum & 0xFF));
  45. return buffer.ToArray();
  46. }
  47. protected static byte[] ToByteArray(string parameter)
  48. {
  49. if (parameter == null)
  50. return new byte[] { };
  51. return parameter.Split(',').Select(para => Convert.ToByte(para, 16)).ToArray();
  52. }
  53. }
  54. public class WattsineRFReadRegisterHandler : WattsineRFHandler
  55. {
  56. public WattsineRFReadRegisterHandler(WattsineRF device,string adress,string readLength)
  57. : base(device,"03", $"{ adress},{ readLength}")
  58. {
  59. }
  60. public override bool HandleMessage(MessageBase msg, out bool handled)
  61. {
  62. if (base.HandleMessage(msg, out handled))
  63. {
  64. var result = msg as WattsineRFMessage;
  65. //Device.NoteInterfaceActived(true);
  66. }
  67. return true;
  68. }
  69. }
  70. public class WattsineRFWriteRegisterHandler : WattsineRFHandler
  71. {
  72. public WattsineRFWriteRegisterHandler(WattsineRF device, string adress,string commnad)
  73. : base(device, "06", $"{ adress},{commnad}")
  74. {
  75. }
  76. public override bool HandleMessage(MessageBase msg, out bool handled)
  77. {
  78. if (base.HandleMessage(msg, out handled))
  79. {
  80. var result = msg as WattsineRFMessage;
  81. //Device.NoteInterfaceActived(true);
  82. }
  83. return true;
  84. }
  85. }
  86. public class WattsineRFWriteMultiRegisterHandler : WattsineRFHandler
  87. {
  88. public WattsineRFWriteMultiRegisterHandler(WattsineRF device, string adress,string registerNumber,string writeLength,string multiValue)
  89. : base(device, "10", $"{adress},{registerNumber},{writeLength},{multiValue}")
  90. {
  91. }
  92. public override bool HandleMessage(MessageBase msg, out bool handled)
  93. {
  94. if (base.HandleMessage(msg, out handled))
  95. {
  96. var result = msg as WattsineRFMessage;
  97. //Device.NoteInterfaceActived(true);
  98. }
  99. return true;
  100. }
  101. }
  102. }