Hardware Reference
In-Depth Information
Listing 8-3. Data Logger and ADK Handler, Part 3 of 8
void loop(){
if (digitalRead(2) == HIGH){
digitalWrite (13, HIGH);
if (IcomingTime >= 25){
if (lastReply){
SendOK();
}
else{
Serial3.write (badPacket,21);
}
IcomingTime = 0;
}
HandleADK();
CheckForIncoming();
delay (50);
IcomingTime++;
} // end if (digitalRead(2) == HIGH)
else{
IcomingTime = 1000;
bool lastReply = false; // will request a new packet to be set
// if node is waiting for reply
digitalWrite (13, (Blink = !Blink)); // blink when SD card is not available
delay (100);
} // end else for if (digitalRead(2) == HIGH)
} // end loop
Part 4 is the function to capture incoming packets from the Arduino. It performs the checksum verification. This
function is closely related to the receive function created in the openFrameworks portion and described in Chapter 5.
The CheckForIncoming function has a bit more control than previous examples to ensure that the packets are properly
received. It does this by flushing all of the input and serial connection buffers connected to the XBee module when
too many errors have been encountered. This function also initiates the proper reply packet based on the checksum
being correct, along with the reading of the packet when a proper packet is received.
Listing 8-3. Data Logger and ADK Handler, Part 4 of 8
void CheckForIncoming(){
incomingBuffer[0] = 0; // clear the first byte of the incoming buffer
if (Serial3.available() && 0x7E == (incomingBuffer[0] = Serial3.read())){
incomingBuffer[1] = Serial3.read(); // pull packet length
incomingBuffer[2] = Serial3.read();
incomingByteLen = incomingBuffer[1] + incomingBuffer[2]; // calculate packet length
incomingPacketChecksum = 0; // clear checksum
for (int i = 3; i <= incomingByteLen + 3; i++){
incomingBuffer[i] = Serial3.read(); // capture packet
incomingPacketChecksum += incomingBuffer[i]; // calculate checksum
}
incomingPacketChecksum = (0xFF - incomingPacketChecksum); // finish checksum
incomingByteLen += 3;
 
Search WWH ::




Custom Search