Go Down

Topic: XBee API system with ACK packets -- long turnaround time (Read 517 times) previous topic - next topic

kevnishi

Hi,

I'm sending data packets of 14-44 bytes from an XBee connected to a MacBook Pro to an XBee connected to an Arduino Uno. Inevitably, some of these data packets get lost. So I'm sending back 10-byte ACK packets. I get the most reliable results when I wait 250 ms between sending the data packet and reading the ACK packet. Unfortunately, I regularly send 12 or more packets at a time. Since there are regularly retransmissions, sending the whole thing takes 6-10 seconds, which is way too long. Is anybody else able to get faster turnaround time for the ACK? How might I try to tackle this problem? Some of my source code for both the laptop and the Uno is below. Any suggestions regarding how to use the XBee with the Uno are welcome. Thanks!

Code: [Select]

// C++ on MBP
bool Serialize_Clock_Sync(int fd)
{
  bool retval = false;                      // assume success
  int  c_rx_buff_size = 12;                 // size in bytes of Rx buffer
 
  char c_rx_buffer[c_rx_buff_size];         // the Rx buffer
  for (int i = 0; i < c_rx_buff_size; i++)  // clear the Rx buffer
  {
    c_rx_buffer[i] = 0x00;
  }
 
  // set the checksum
  rjc_c_tx[rjc_c_size - 1] = Checksum(rjc_c_tx, rjc_c_size);
 
  while(!retval)
  {
    cout << "sending ";
    write(fd, &rjc_c_tx, rjc_c_size);       // send the RJC
    usleep(ack_delay); // this is set to 250000 in the header file
    read(fd, &c_rx_buffer, c_rx_buff_size); // look for the acknowledgment
    if (c_rx_buffer[8] == 'C')
    {
      vehicle_sync[c_rx_buffer[9]] = true;
      retval = true;
    }
  }
  cout << "received " << c_rx_buffer[8] << (int) c_rx_buffer[9] << endl << endl;

  if (c_rx_buffer[8] == (char) 'C')
  {
    cout << "Sync complete" << endl;
  }
 
  return retval;
}


Code: [Select]
// on Arduino

void loop() {
  delay(5);
  int i = 0;
  Clear_Buffer();
  loop_count++;
  Serial.println(loop_count);

  while (VCXBee.available() > 0) { // check serial buffer for content
    serial_read_buffer[i] = (unsigned char)VCXBee.read();
    i++;
  }
 
  delay(100); 

  if (serial_read_buffer[8] == 'C')
  {
    Serial.println("got a clock sync packet");
    union_for_receiving_float_from_xbee.received_float_bytes[0] = serial_read_buffer[9];
    union_for_receiving_float_from_xbee.received_float_bytes[1] = serial_read_buffer[10];
    union_for_receiving_float_from_xbee.received_float_bytes[2] = serial_read_buffer[11];
    union_for_receiving_float_from_xbee.received_float_bytes[3] = serial_read_buffer[12];
    Clear_Buffer();
    Send_Ack('C', (char) VEHICLE_ID_NUMBER);
    Serial.print(F("sync time "));
    //Serial.print("sync time ");
    Serial.println(union_for_receiving_float_from_xbee.received_float);
  }
}

void Send_Ack(char packet_type, char data)
{
  char output_buffer[11];
  char sum = 0x00;

  output_buffer[0]  = 0x7E;        // start delimiter
  output_buffer[1]  = 0x00;        // msb
  output_buffer[2]  = 0x07;        // lsb
  output_buffer[3]  = 0x01;        // frame id
  output_buffer[4]  = 0x00;        // frame type
  output_buffer[5]  = 0xFF;        // destination address
  output_buffer[6]  = 0xFF;        // destination address
  output_buffer[7]  = 0x01;        // option
  output_buffer[8]  = packet_type; // packet type
  output_buffer[9]  = data;        // VID or Row Number 
  output_buffer[10]  = 0x00;       // checksum

  for (int i = 3; i <= 9; i++)
  {
    sum += output_buffer[i];
  }
 
  output_buffer[10]  = 0xFF - sum;

  VCXBee.write  (output_buffer, 11);
  Serial.print  ("sent ");
  Serial.print  (output_buffer[8]);
  Serial.println((int) output_buffer[9]);
   
}

mikb55

You need hardware CTS flow control to avoid overflowing/corrupting the Xbee Tx buffer .

Go Up