BoxDataHandlingAdapter.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using RRQMCore.ByteManager;
  2. using RRQMSocket;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ProjectManagementSystem.Device.CasunBox
  9. {
  10. public class BoxDataHandlingAdapter : DataHandlingAdapter
  11. {
  12. /// <summary>
  13. /// 获取已设置的数据包的长度
  14. /// </summary>
  15. public int FixedSize { get; private set; } = 14;
  16. /// <summary>
  17. /// 临时包
  18. /// </summary>
  19. private ByteBlock tempByteBlock;
  20. /// <summary>
  21. /// 包剩余长度
  22. /// </summary>
  23. private int surPlusLength = 0;
  24. public BytePool BytePoolEx { get; set; } = new BytePool();
  25. /// <summary>
  26. /// 预处理
  27. /// </summary>
  28. /// <param name="byteBlock"></param>
  29. protected override void PreviewReceived(ByteBlock byteBlock)
  30. {
  31. bool lightProtocol = byteBlock.Buffer[0] == 0x20 && byteBlock.Length % 16 == 0;
  32. bool callProtocol = byteBlock.Buffer[0] == 0x10 && byteBlock.Length % 14 == 0 && byteBlock.Buffer[byteBlock.Length - 1] == 0x03;
  33. //报警灯协议长度16
  34. if (lightProtocol)
  35. {
  36. FixedSize = 16;
  37. if (this.tempByteBlock != null)
  38. {
  39. this.tempByteBlock.Dispose();
  40. this.tempByteBlock = null;
  41. }
  42. }
  43. else if(callProtocol)
  44. {
  45. FixedSize = 14;
  46. if (this.tempByteBlock != null)
  47. {
  48. this.tempByteBlock.Dispose();
  49. this.tempByteBlock = null;
  50. }
  51. }
  52. byte[] buffer = byteBlock.Buffer;
  53. int r = (int)byteBlock.Length;
  54. if (this.tempByteBlock == null)
  55. {
  56. SplitPackage(buffer, 0, r);
  57. }
  58. else
  59. {
  60. if (surPlusLength == r)
  61. {
  62. this.tempByteBlock.Write(buffer, 0, surPlusLength);
  63. PreviewHandle(this.tempByteBlock);
  64. this.tempByteBlock.Dispose();
  65. this.tempByteBlock = null;
  66. surPlusLength = 0;
  67. }
  68. else if (surPlusLength < r)
  69. {
  70. this.tempByteBlock.Write(buffer, 0, surPlusLength);
  71. PreviewHandle(this.tempByteBlock);
  72. this.tempByteBlock.Dispose();
  73. this.tempByteBlock = null;
  74. SplitPackage(buffer, surPlusLength, r);
  75. }
  76. else
  77. {
  78. this.tempByteBlock.Write(buffer, 0, r);
  79. surPlusLength -= r;
  80. }
  81. }
  82. }
  83. private void SplitPackage(byte[] dataBuffer, int index, int r)
  84. {
  85. while (index < r)
  86. {
  87. if (r - index >= this.FixedSize)
  88. {
  89. ByteBlock byteBlock = this.BytePoolEx.GetByteBlock(this.FixedSize);
  90. byteBlock.Write(dataBuffer, index, this.FixedSize);
  91. PreviewHandle(byteBlock);
  92. surPlusLength = 0;
  93. }
  94. else//半包
  95. {
  96. this.tempByteBlock = this.BytePoolEx.GetByteBlock(this.FixedSize);
  97. surPlusLength = this.FixedSize - (r - index);
  98. this.tempByteBlock.Write(dataBuffer, index, r - index);
  99. }
  100. index += this.FixedSize;
  101. }
  102. }
  103. protected virtual void PreviewHandle(ByteBlock byteBlock)
  104. {
  105. try
  106. {
  107. if ((byteBlock.Buffer[0] == 0x10 && byteBlock.Length == 14 && byteBlock.Buffer[byteBlock.Length - 1] == 0x03)
  108. || (byteBlock.Buffer[0] == 0x20 && byteBlock.Length == 16))
  109. {
  110. this.GoReceived(byteBlock, null);
  111. }
  112. }
  113. finally
  114. {
  115. byteBlock.Dispose();
  116. }
  117. }
  118. protected override void PreviewSend(byte[] buffer, int offset, int length, bool isAsync)
  119. {
  120. this.GoSend(buffer, offset, length, isAsync);
  121. }
  122. }
  123. }