Home/Support/Support Forum/250ms delay to get acks in API mode - laptop to Arduino Uno, XBee S1
New and improved user forum site coming soon

250ms delay to get acks in API mode - laptop to Arduino Uno, XBee S1

0 votes
I’m sending packets of variable length (14 to 44 bytes) from a MacBook Pro to an Arduino Uno via XBee S1. The MBP is connected to the XBee S1 via a Sparkfun explorer USB (https://www.sparkfun.com/products/11812), and the Uno is connected to the XBee S1 via the Sparkfun explorer Regulated (https://www.sparkfun.com/products/11373).

I have set up a simple protocol through which the MBP sends a packet, and then when the Uno receives the packet, it sends back simple ack packet. Even at a range of just 2-3 feet, I’m not able to receive every packet that is sent, so the MBP resends until the ack is successfully received. However, it seems that the turnaround on the arduino board is about 250ms, so for me to send 100 bytes, using 10 bytes payload per packet, takes 5-10 seconds, because there are several retransmissions for each packet. Is anybody else sending multiple packets with acks and getting similar delay?

I’ve included some of my source code below.

// C++ on MBP
bool Route_Jerk_Command::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 = 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);
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;
}

// 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 = (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;
}

output_buffer[10] = 0xFF - sum;

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

}
asked Feb 3, 2017 in XBee Programmable Development by kevnishi New to the Community (0 points)

Please log in or register to answer this question.

1 Answer

0 votes
Assuming that you are not using sleep modes at all and both are always on XBee modules, the delay you are experiencing will be in your code.
answered Feb 3, 2017 by mvut Veteran of the Digi Community (15,515 points)
...