Pages: [1]   Go Down
Author Topic: Not transmitting to Xbee transmitter  (Read 921 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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]=' ';
}
}
}
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 640
Posts: 50345
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:






Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Anyone? smiley-cry
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You could post code that compiles.

Code:
sketch_may03b.cpp: In function 'void loop()':
sketch_may03b:74: error: expected `}' at end of input
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

here is the formatted code:

Code:
#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]=' ';
      }
    }
  }
}

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 502
Posts: 19086
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  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:
     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:
        if (linea[i]==comandoGPR[i-1])
        {
          bien++;
        }

Look up strcmp or memcmp.
Logged


Pages: [1]   Go Up
Jump to: