Go Down

Topic: Not transmitting to Xbee transmitter (Read 1 time) previous topic - next topic

longbow

Hi all I have the following code uploaded to my Ardupilot Mega 2560, when I connect a serial cable directly I am able to receive data. But when I try to transmit via Xbee no data appears.

The Xbees have been tested to work with another software, the baud rate is set to 57600.

Am I missing out something?

Code: [Select]
#include <string.h>
#include <ctype.h>
#include <Servo.h>
char data; //data to check to see if receive any command for servo

int byteGPS=-1;
char linea[300] = ""; //to be fill up with gps data
int conta=0;
int loopcount = 0;
char comandoGPR[7] = "$GPRMC"; //setup header
char comandoGPA[7] = "$GPGGA"; //setup header
int bien=0; //check if GPRMC found
int bien2=0; //check if GPGGA found
void setup()
{

Serial.begin(19200); // set to the tx and rx as per xbee pin. currently this is TX 0
Serial1.begin(4800); // this is GPS fixes.
for (int i=0;i<300;i++)
{ // Initialize a buffer for received data
linea[i]=' ';
}
}
void loop()
{
byteGPS=Serial1.read();
if (byteGPS == -1)
{ // See if the port is empty yet
delay(100);
}
else
{
linea[conta]=byteGPS; // If there is Serial2 port data, it is put in the buffer
conta++;
//Serial.println(byteGPS, BYTE);
if (byteGPS==13)
{ // If the received byte is = to 13, end of transmission
//for (i=0,)
bien2=0;
for (int i=1;i<7;i++)
{ // Verifies if the received command starts with $GPR
//Serial.print(linea[i]);
//Serial.print(",");
//Serial.println(comandoGPR[i-1]);
if (linea[i]==comandoGPR[i-1])
{
bien++;
}
if (linea[i]==comandoGPA[i-1])
{
bien2++;
}
}
if (bien == 6) //GPRMC found
{
for (int i=0; i<conta;i++)
{
Serial.print(linea[i]);
}
}
if (bien2 == 6) //GPGGA found
{
for (int i=0; i<conta;i++)
{
Serial.print(linea[i]);
}
//Serial.println("//////////////////////////////////////");
}
conta=0; // Reset the buffer
for (int i=0;i<300;i++)
{
linea[i]=' ';
}
}
}

PaulS

Quote
Am I missing out something?

Information on how the XBee is connect to the Mega.
Information about how the other XBee is being used.
Information about whether any lights flash on the XBee shield.
Information about whether any lights flash on the receiver.

longbow

Information on how the XBee is connect to the Mega.
The Xbee is connected to the IMU Shield, which is connected to the Ardupilot Mega. (Serial connection that succesfully transmitted data was also connected to the shield)

Information about how the other XBee is being used.
The Other Xbee is connected to my laptop via a serial cable

Information about whether any lights flash on the XBee shield.
No lights flashing on the transmitting Xbee shield, but there is a blue LED that is lit up

Information about whether any lights flash on the receiver.
No lights flashing on the receiver, but there is a red LED that is lit up

A picture speaks a thousand words~ Here is the setup:








Nick Gammon

You could post code that compiles.

Code: [Select]
sketch_may03b.cpp: In function 'void loop()':
sketch_may03b:74: error: expected `}' at end of input
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

And hit the auto-format button. This just looks strange:

Code: [Select]
if (linea[i]==comandoGPA[i-1])
{
bien2++;
}
}
if (bien == 6) //GPRMC found
{
for (int i=0; i<conta;i++)
{
Serial.print(linea[i]);
}
}
if (bien2 == 6) //GPGGA found
{
for (int i=0; i<conta;i++)
{
Serial.print(linea[i]);
}


Indenting helps see what you are doing and sorting out logic problems.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

longbow

here is the formatted code:

Code: [Select]
#include <string.h>
#include <ctype.h>
char data; //data to check to see if receive any command for servo
int byteGPS=-1;
char linea[300] = ""; //to be fill up with gps data
int conta=0;
int loopcount = 0;
char comandoGPR[7] = "$GPRMC"; //setup header
char comandoGPA[7] = "$GPGGA"; //setup header
int bien=0; //check if GPRMC found
int bien2=0; //check if GPGGA found
void setup()
{
  Serial.begin(19200); // set to the tx and rx as per xbee pin. currently this is TX 0
  Serial1.begin(4800); // this is GPS fixes.
  for (int i=0;i<300;i++)
  { // Initialize a buffer for received data
    linea[i]=' ';
  }
}
void loop()
{
  byteGPS=Serial1.read();
  if (byteGPS == -1)
  { // See if the port is empty yet
    delay(100);
  }
  else
  {
    linea[conta]=byteGPS; // If there is Serial2 port data, it is put in the buffer
    conta++;
    //Serial.println(byteGPS, BYTE);
    if (byteGPS==13)
    { // If the received byte is = to 13, end of transmission
      //for (i=0,)
      bien2=0;
      for (int i=1;i<7;i++)
      { // Verifies if the received command starts with $GPR
        //Serial.print(linea[i]);
        //Serial.print(",");
        //Serial.println(comandoGPR[i-1]);
        if (linea[i]==comandoGPR[i-1])
        {
          bien++;
        }
        if (linea[i]==comandoGPA[i-1])
        {
          bien2++;
        }
      }
      if (bien == 6) //GPRMC found
      {
        for (int i=0; i<conta;i++)
        {
          Serial.print(linea[i]);
        }
      }
      if (bien2 == 6) //GPGGA found
      {
        for (int i=0; i<conta;i++)
        {
          Serial.print(linea[i]);
        }
        //Serial.println("//////////////////////////////////////");
      }
      conta=0; // Reset the buffer
      for (int i=0;i<300;i++)
      {
        linea[i]=' ';
      }
    }
  }
}


longbow

Basically, I am receiving 2 data strings each starting with "GPRMC" and "GPGGA". It works perfectly fine when I plug in a serial cable but nothing is thrown to the Xbee. Everything works normally when I flash in upload Arduplane so my guess is that there is something wrong with my code.

Nick Gammon

Code: [Select]
  byteGPS=Serial1.read();
  if (byteGPS == -1)
  { // See if the port is empty yet
    delay(100);
  }
  else
  {
    linea[conta]=byteGPS; // If there is Serial2 port data, it is put in the buffer
    conta++;


Don't throw in delays to make it work. See this:

http://www.gammon.com.au/serial





Code: [Select]
     for (int i=1;i<7;i++)
      { // Verifies if the received command starts with $GPR
        if (linea[i]==comandoGPR[i-1])


Doesn't the buffer start at zero, not 1?




Code: [Select]
        if (linea[i]==comandoGPR[i-1])
        {
          bien++;
        }


Look up strcmp or memcmp.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up