Hardware Reference
In-Depth Information
Listing 8-1. testApp.cpp, Part 4 of 7
void testApp::ReadPacket(){
switch (incomingBuffer[3]){ // check packet type and perform any responses
case 0x90:
dataLength = incomingByteLen - 15; // reduce to just the data length to get the data
for (int i = 0; i <= dataLength; i++){
incomeData [i] = incomingBuffer[i+15]; // phrase out the data from the packet
}
if (dataLength == 2 && incomeData[0] == 'O' && incomeData[1] == 'K'){
printf ("OKAY\n"); // set Okay flag true when a good reply is received
ReplyOK = true;
}
if (dataLength == 3 && incomeData[0] == 'B' && incomeData[1] == 'A' &&
incomeData[2] == 'D' && FirstPacketsent){
ReplyOK = false; // make sure that the flag is false when a BAD notify is received
printf ("BAD\n");
serial.writeBytes (packetBuffer, lastPacketLength); // send last known packet
WaitForReply(); // wait again for an okay
}
break;
case 0x8B:
printf ("Transmt Responce\n");
break;
case 0x88:
printf ("Command response %X%X \n", incomingBuffer[8] , incomingBuffer[9]);
break;
default: // announce unknown packet type
printf ("error: packet type not known\n" );
} // end switch
} // end testApp::ReadPacket()
In part 5, the WaitForReply function is called after sending a packet to the Arduino, and will remain in a loop,
constantly polling for new packets. The loop is complete when the reply comes back as a data packet containing an
“OK.” The program will stop everything else it is doing while in the loop; this could be mitigated with more complexity,
such as implementing a timeout. A recursive situation occurs when waiting for a good reply and a “BAD” packet is
received, because the wait for reply is called when the resend occurs. The recursive situation is not detrimental to the
running of the example and is completely exited when an “OK” is received. The recursive call can cause problems
in more complex situations, though, and needs to be handled differently—with the use of timeouts and more robust
packet-correction methods.
Listing 8-1. testApp.cpp, Part 5 of 7
void testApp::WaitForReply(){
printf ("Wait for reply \n");
ReplyOK = false;
while (ReplyOK != true){
CheckForIncoming();
}
} // end testApp::WaitForReply()
 
Search WWH ::




Custom Search