Guest User

Untitled

a guest
Jul 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.66 KB | None | 0 0
  1. class ClsReceiveSerialData
  2. {
  3. // SerialPort property
  4. private ClsSerialPort serialPort;
  5. public ClsSerialPort SerialPor`enter code here`t
  6. {
  7. get
  8. {
  9. return serialPort;
  10. }
  11.  
  12. set
  13. {
  14. serialPort = value;
  15. }
  16. }
  17.  
  18. public static void ReceiveSerialData(byte[] byteBuffer, int length)
  19. {
  20. int i;
  21. int threadID = 0;
  22.  
  23. #if (TEST)
  24. StringBuilder sb = new StringBuilder(byteBuffer.Length * 2);
  25. i = 0;
  26. foreach (byte b in byteBuffer)
  27. {
  28. //convert a byte to a hex string
  29. sb.AppendFormat("{0:x2}", b);
  30.  
  31. i++;
  32. }
  33.  
  34. //write the debuf string to the output window
  35. Debug.WriteLine("byteBuffer[" + (i - 1) + "] = " + sb.ToString());
  36. #endif
  37. // create an instance of a delegate class
  38. ClsProcessData ad = new ClsProcessData();
  39.  
  40. // Create the delegate.
  41. AsyncProcessData caller =
  42. AsyncProcessData(ad.BeginProcessingData);
  43.  
  44.  
  45. // Initiate the asychronous call.
  46. IAsyncResult result =
  47. caller.BeginInvoke(byteBuffer, length, out threadID, null, null);
  48.  
  49.  
  50. // Call EndInvoke to wait for the asynchronous call to complete,
  51. // and to retrieve the results.
  52. caller.EndInvoke(out threadID, result);
  53. }
  54. }
  55.  
  56. class ClsProcessData
  57. {
  58.  
  59. bool gotFirstFEcode = false;
  60. bool gotSecondFEcode = false;
  61. bool gotCtrlrCIV = false;
  62. bool okToSaveCommandBytes = false;
  63. bool gotEndOfMessageCode = true;
  64. byte[] commandData;
  65. int commandIndex = 0;
  66.  
  67. public void BeginProcessingData(byte[] data, int byteCount,
  68. out int threadId)
  69. {
  70. lock (this)
  71. {
  72. int i;
  73.  
  74. threadId = Thread.CurrentThread.ManagedThreadId;
  75.  
  76. #if (TEST)
  77. Debug.WriteLine(
  78. String.Concat("BeginProcessingData threadID = ",
  79. Convert.ToString(threadId)));
  80. #endif
  81.  
  82. //find a preamble
  83. i = 0;
  84. while (i < byteCount)
  85. {
  86. // have we completed processing the current message?
  87. if (gotEndOfMessageCode)
  88. {
  89. //reset the preamble detection booleans
  90. gotFirstFEcode = false;
  91. gotSecondFEcode = false;
  92. gotCtrlrCIV = false;
  93. okToSaveCommandBytes = false;
  94. gotEndOfMessageCode = false;
  95. } // If
  96.  
  97. //can we save a command byte now?
  98. if (okToSaveCommandBytes)
  99. {
  100. //go save a command byte
  101. i = ExtractCommand(data, i, byteCount);
  102.  
  103. //reset the preamble detection booleans
  104. gotFirstFEcode = false;
  105. gotSecondFEcode = false;
  106. gotCtrlrCIV = false;
  107. okToSaveCommandBytes = false;
  108. gotEndOfMessageCode = false;
  109. } // If
  110.  
  111.  
  112. //have we found the radio//s civ address?
  113. if (gotCtrlrCIV && (data[i] ==
  114. ClsConstants.CTRLR_DEFAULT_CIV_ADDR))
  115. {
  116. //we are ok to start saving command bytes
  117. okToSaveCommandBytes = true;
  118. } // If
  119.  
  120. //do we have both FE codes of the preamble and not
  121. another extraneous FE code in the buffer
  122. if (gotSecondFEcode && (data[i] ==
  123. ClsConstants.CTRLR_DEFAULT_CIV_ADDR))
  124. {
  125. gotCtrlrCIV = true;
  126. } // If
  127.  
  128. if (gotFirstFEcode && (data[i] ==
  129. ClsConstants.PREAMBLE_CODE))
  130. {
  131. gotSecondFEcode = true;
  132. } // If
  133.  
  134. //do we have the first preamble code?
  135. if ((data[i] == ClsConstants.PREAMBLE_CODE) &&
  136. (!gotSecondFEcode))
  137. {
  138. gotFirstFEcode = true;
  139.  
  140. //reset } // of message boolean
  141. gotEndOfMessageCode = false;
  142. } // If
  143.  
  144. //increment the array index
  145. i++;
  146.  
  147. } // while
  148. }
  149. }
  150.  
  151. // ExrtractCommand returns an updated index value
  152. private int ExtractCommand(byte[] data, int index, int byteCount)
  153. {
  154. int i = index;
  155.  
  156. while ((i < byteCount) && (!gotEndOfMessageCode))
  157. {
  158.  
  159. if (data[i] == ClsConstants.END_OF_MESSAGE_CODE)
  160. {
  161. //set the end of message flag
  162. gotEndOfMessageCode = true;
  163.  
  164. //Process the command
  165. ProcessCommand(commandData, commandIndex);
  166. }
  167. else
  168. {
  169. //save a command byte
  170. commandData[commandIndex] = data[i];
  171.  
  172. //increment to the next command index
  173. commandIndex++;
  174. } // If
  175.  
  176. //increment the data array index
  177. i++;
  178. } // while
  179.  
  180. return i;
  181. } // void
Add Comment
Please, Sign In to add comment