Hardware Reference
In-Depth Information
if (incomingByteLen > 0 && incomingPacketChecksum ==
incomingBuffer[incomingByteLen+1]){
Serial3.flush(); // done with serial buffer for now
ReadPacket(); // read and handled the data
SendOK(); // reply to original sender
}
else { // if checksum is bad, perform clean and bad packet send
ERRcount++; // increment error count
for (int i = 0; i <= 80; i++){ // clear packet from incoming buffer
incomingBuffer[i] = 0;
}
Serial3.flush(); // clear serial connection
delay (100);
// if too many errors encountered, reset serial connection
if (ERRcount == 10) {
Serial3.end(); // stop serial completely
for (int i = 0; i <= 10; i++) { // blink for verification
digitalWrite (13, (Blink = !Blink));
delay (50);
ERRcount = 0; // reset error count
}
Serial3.begin(115200); // restart serial connection
delay (30);
}
Serial3.write (badPacket,21); // send BAD reply
lastReply = false; // set last reply ad bad flag
} // end else checksum bad
} // end if (Serial3.available() && 0x7E
} // end void CheckIncoming()
Part 5 reads a proper incoming packet using a switch statement and recognizes three packet types. The three
packet are a data packet, an AT command response, and a transmit response. The transmit and AT command response
both print to the serial monitor when they are detected and perform no other work on those types. When a data
packet is received, the address of the sending node is placed in an array for use in the SendOK function, which will be
called after this function returns to CheckForIncoming . Data is also phrased from the data packet and placed in an
array to be used for prepping the format and containment in the log file on the SD card.
Listing 8-3. Data Logger and ADK Handler, Part 5 of 8
void ReadPacket(){
IcomingTime = 0; // received a good packet-reset time
switch (incomingBuffer[3]){ // check packet type and perform any responses
case 0x90: // data packet
dataLength = 0;
for (int i = 4; i <= 13; i++){ // get both addresses of the source device
sourceADR[i-4] = incomingBuffer[i];
}
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]4data from the packet
}
 
Search WWH ::




Custom Search