Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ClsReceiveSerialData
- {
- // SerialPort property
- private ClsSerialPort serialPort;
- public ClsSerialPort SerialPor`enter code here`t
- {
- get
- {
- return serialPort;
- }
- set
- {
- serialPort = value;
- }
- }
- public static void ReceiveSerialData(byte[] byteBuffer, int length)
- {
- int i;
- int threadID = 0;
- #if (TEST)
- StringBuilder sb = new StringBuilder(byteBuffer.Length * 2);
- i = 0;
- foreach (byte b in byteBuffer)
- {
- //convert a byte to a hex string
- sb.AppendFormat("{0:x2}", b);
- i++;
- }
- //write the debuf string to the output window
- Debug.WriteLine("byteBuffer[" + (i - 1) + "] = " + sb.ToString());
- #endif
- // create an instance of a delegate class
- ClsProcessData ad = new ClsProcessData();
- // Create the delegate.
- AsyncProcessData caller =
- AsyncProcessData(ad.BeginProcessingData);
- // Initiate the asychronous call.
- IAsyncResult result =
- caller.BeginInvoke(byteBuffer, length, out threadID, null, null);
- // Call EndInvoke to wait for the asynchronous call to complete,
- // and to retrieve the results.
- caller.EndInvoke(out threadID, result);
- }
- }
- class ClsProcessData
- {
- bool gotFirstFEcode = false;
- bool gotSecondFEcode = false;
- bool gotCtrlrCIV = false;
- bool okToSaveCommandBytes = false;
- bool gotEndOfMessageCode = true;
- byte[] commandData;
- int commandIndex = 0;
- public void BeginProcessingData(byte[] data, int byteCount,
- out int threadId)
- {
- lock (this)
- {
- int i;
- threadId = Thread.CurrentThread.ManagedThreadId;
- #if (TEST)
- Debug.WriteLine(
- String.Concat("BeginProcessingData threadID = ",
- Convert.ToString(threadId)));
- #endif
- //find a preamble
- i = 0;
- while (i < byteCount)
- {
- // have we completed processing the current message?
- if (gotEndOfMessageCode)
- {
- //reset the preamble detection booleans
- gotFirstFEcode = false;
- gotSecondFEcode = false;
- gotCtrlrCIV = false;
- okToSaveCommandBytes = false;
- gotEndOfMessageCode = false;
- } // If
- //can we save a command byte now?
- if (okToSaveCommandBytes)
- {
- //go save a command byte
- i = ExtractCommand(data, i, byteCount);
- //reset the preamble detection booleans
- gotFirstFEcode = false;
- gotSecondFEcode = false;
- gotCtrlrCIV = false;
- okToSaveCommandBytes = false;
- gotEndOfMessageCode = false;
- } // If
- //have we found the radio//s civ address?
- if (gotCtrlrCIV && (data[i] ==
- ClsConstants.CTRLR_DEFAULT_CIV_ADDR))
- {
- //we are ok to start saving command bytes
- okToSaveCommandBytes = true;
- } // If
- //do we have both FE codes of the preamble and not
- another extraneous FE code in the buffer
- if (gotSecondFEcode && (data[i] ==
- ClsConstants.CTRLR_DEFAULT_CIV_ADDR))
- {
- gotCtrlrCIV = true;
- } // If
- if (gotFirstFEcode && (data[i] ==
- ClsConstants.PREAMBLE_CODE))
- {
- gotSecondFEcode = true;
- } // If
- //do we have the first preamble code?
- if ((data[i] == ClsConstants.PREAMBLE_CODE) &&
- (!gotSecondFEcode))
- {
- gotFirstFEcode = true;
- //reset } // of message boolean
- gotEndOfMessageCode = false;
- } // If
- //increment the array index
- i++;
- } // while
- }
- }
- // ExrtractCommand returns an updated index value
- private int ExtractCommand(byte[] data, int index, int byteCount)
- {
- int i = index;
- while ((i < byteCount) && (!gotEndOfMessageCode))
- {
- if (data[i] == ClsConstants.END_OF_MESSAGE_CODE)
- {
- //set the end of message flag
- gotEndOfMessageCode = true;
- //Process the command
- ProcessCommand(commandData, commandIndex);
- }
- else
- {
- //save a command byte
- commandData[commandIndex] = data[i];
- //increment to the next command index
- commandIndex++;
- } // If
- //increment the data array index
- i++;
- } // while
- return i;
- } // void
Add Comment
Please, Sign In to add comment