DWConnection.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using MECF.Framework.Common.Communications;
  8. namespace MECF.Framework.RT.EquipmentLibrary.HardwareUnits.FFUs.AAF
  9. {
  10. public class DWPressureMessage : BinaryMessage
  11. {
  12. public byte DeviceAddress { get; set; }
  13. public byte FunctionCode { get; set; }
  14. public byte Length { get; set; }
  15. public byte[] Data { get; set; }
  16. }
  17. public class DWPressureConnection : SerialPortConnectionBase
  18. {
  19. private List<byte> _lstCacheBuffer = new List<byte>();
  20. public DWPressureConnection(string portName) : base(portName,9600,8, System.IO.Ports.Parity.None, System.IO.Ports.StopBits.One,"\n",false)
  21. {
  22. }
  23. public override bool SendMessage(byte[] message)
  24. {
  25. _lstCacheBuffer.Clear();
  26. return base.SendMessage(message);
  27. }
  28. protected override MessageBase ParseResponse(byte[] rawMessage)
  29. {
  30. _lstCacheBuffer.AddRange(rawMessage);
  31. byte[] temps = _lstCacheBuffer.ToArray();
  32. DWPressureMessage msg = new DWPressureMessage();
  33. msg.IsResponse = false;
  34. msg.IsAck = false;
  35. msg.IsComplete = false;
  36. msg.RawMessage = _lstCacheBuffer.ToArray();
  37. if (temps.Length < 4) return msg;
  38. msg.DeviceAddress = temps[0];
  39. msg.FunctionCode = temps[1];
  40. msg.Length = temps[2];
  41. if (temps.Length < msg.Length + 5) return msg;
  42. msg.Data = _lstCacheBuffer.Skip(3).Take(msg.Length).ToArray();
  43. msg.IsResponse = true;
  44. msg.IsAck = true;
  45. msg.IsComplete = true;
  46. return msg;
  47. }
  48. private static byte ModRTU_CRC(byte[] buffer)
  49. {
  50. //ushort crc = 0xFFFF;
  51. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  52. var buf = buffer;
  53. var len = buffer.Length;
  54. byte temp = buffer[0];
  55. for (int i = 1; i < buffer.Length; i++)
  56. {
  57. temp = (byte)(temp ^ buffer[i]);
  58. }
  59. return (byte)~temp;
  60. }
  61. }
  62. public abstract class DWPressureHandler : HandlerBase
  63. {
  64. public DWPressure Device { get; }
  65. protected DWPressureHandler(DWPressure device, byte[] commandvalue)
  66. : base(BuildMessage(commandvalue))
  67. {
  68. Device = device;
  69. }
  70. private static byte[] BuildMessage(byte[] commandvalue)
  71. {
  72. byte[] crc = ModRTU_CRC(commandvalue);
  73. List<byte> result = commandvalue.ToList();
  74. foreach(byte b in crc)
  75. {
  76. result.Add(b);
  77. }
  78. return result.ToArray();
  79. }
  80. public override bool HandleMessage(MessageBase msg, out bool handled)
  81. {
  82. var result = msg as DWPressureMessage;
  83. ResponseMessage = msg;
  84. handled = true;
  85. return true;
  86. }
  87. private static byte[] ModRTU_CRC(byte[] buffer)
  88. {
  89. ushort crc = 0xFFFF;
  90. // var buf = System.Text.Encoding.UTF8.GetBytes(String.Join(Environment.NewLine, buffer));
  91. var buf = buffer;
  92. var len = buffer.Length;
  93. for (var pos = 0; pos < len; pos++)
  94. {
  95. crc ^= buf[pos]; // XOR byte into least sig. byte of crc
  96. for (var i = 8; i != 0; i--)
  97. // Loop over each bit
  98. if ((crc & 0x0001) != 0)
  99. {
  100. // If the LSB is set
  101. crc >>= 1; // Shift right and XOR 0xA001
  102. crc ^= 0xA001;
  103. }
  104. else // Else LSB is not set
  105. {
  106. crc >>= 1; // Just shift right
  107. }
  108. }
  109. // Note, this number has low and high bytes swapped, so use it accordingly (or swap bytes)
  110. return BitConverter.GetBytes(crc);
  111. }
  112. }
  113. public class DWPressureQueryPressureHandler : DWPressureHandler
  114. {
  115. public DWPressureQueryPressureHandler(DWPressure device, byte groupAddress)
  116. : base(device, new byte[] { groupAddress, 3,0,0,0,1 })
  117. {
  118. Name = "Query pressure";
  119. }
  120. public override bool HandleMessage(MessageBase msg, out bool handled)
  121. {
  122. var result = msg as DWPressureMessage;
  123. handled = false;
  124. if (!result.IsResponse) return true;
  125. short rvalue = 0;
  126. if (result.Data != null && result.Data.Length >= 2)
  127. {
  128. rvalue = (short)(rvalue ^ result.Data[0]);
  129. rvalue = (short)(rvalue << 8);
  130. rvalue = (short)(rvalue ^ result.Data[1]);
  131. }
  132. if (result.DeviceAddress == 1)
  133. {
  134. Device.PressureValue1 = rvalue;
  135. }
  136. else
  137. {
  138. Device.PressureValue2 = rvalue;
  139. }
  140. ResponseMessage = msg;
  141. handled = true;
  142. Thread.Sleep(2000);
  143. return true;
  144. }
  145. }
  146. }