Pages: [1]   Go Down
Author Topic: Sending byte data through xBee serial  (Read 1649 times)
0 Members and 1 Guest are viewing this topic.
St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to send packets of 7 bytes of data, all will be numbers between 0 and 255.   The data is sent with xBee.print(255), but when it is received, it comes over as the ascii representation of each digit....50,53,53.

I am including the source, but my guess is that I just don't understand sending data through serial ports.  Any advice would be greatly appriciated.
Heres the sending code....
Code:
#include <SoftwareSerial.h>

SoftwareSerial xBee = SoftwareSerial(2,3);

int hPin = 1;  // Joystick Horizontal Pot data
int vPin = 0;  // Joystick Vertical Pot data
int valV = 0;  // Vertical data
int valH = 0;  // Horizontal data
int V = 0;     // remapped vertical data
int H = 0;      // remapped horizontal data
//int pkt[10];

void setup() {
  Serial.begin(9600);
  xBee.begin(9600);
  Serial.println("Sending...");
}

void loop() {
    // read the joy stick poteniometors
    valV=analogRead(vPin);
    valH=analogRead(hPin);

    //convert analog signal to the range 0 to 250
 
    V=map(valV,0,1024,0,250);
    H=map(valH,0,1024,0,250);
   
    sendStatus(valV,valH,1,2,3,4);   // Send out through xBee

    delay(100);
}


void sendStatus(byte V, byte H, byte s1, byte s2, byte s3, byte s4) {

  int HB=5;   // this is the heart beat.  If the Rx does not recieve
              // this every cycle, it will automatically stop the motors
             
  Serial.print(255);
  Serial.print(",");
  Serial.print(V);
  Serial.print(",");
  Serial.print(H);
  Serial.print(",");
  Serial.print(s1);
  Serial.print(",");
  Serial.print(s2);
  Serial.print(",");
  Serial.print(s3);
  Serial.print(",");
  Serial.print(s4);
  Serial.print(",");
  Serial.print(HB);
  Serial.print(",");
  Serial.println(254);

  xBee.print(255);
  xBee.print(V);
  xBee.print(H);
  xBee.print(s1);
  xBee.print(s2);
  xBee.print(s3);
  xBee.print(s4);
  xBee.print(HB);
 xBee.println(254);

}

and heres the receiving code....
Code:
#include <SoftwareSerial.h>

SoftwareSerial xBee(2, 3); // RX, TX
int V;
int H;
int HB=5;
void setup() 
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Running...");
  xBee.begin(9600);
}

void loop() // run over and over
{
  if (xBee.available()) {
    byte ch = xBee.read();
    delay(15);
    Serial.println(ch);                             // for debugging
    if (ch == 255) {                                   //  this is were I am seeing the problem.   I send 255 and get the ascii codes 50,53,53.     
        getData();                                     //  now do I get this as a signel byte????????????????
    }
  }
   
}

void getData() {
  byte c = 0;
  int n = 0;
  byte dta[6];
  while (c != 254) {
      c = xBee.read();
      if (c != 254) {
        dta[n]=c+1000;
        n++;
        }
     } 
 
   // this is ment to convert from a linear progression to exponential progress at the end points.
   // and shift from 0 to 250 back to -125 to 125
   // not being used until I can figure out what I am doing wrong on getting the data from xBee
   
   byte V = dta[0]; //(pow(dta[0],3)/15876)-126;
   byte H = dta[1]; //(pow(dta[1],3)/15876)-126;
   byte HB = dta[6];
   
   // Just for debugging.  Will eventually be sent to the motor controller.
   
   Serial.print("V = ");
   Serial.print(V);         
   Serial.print(",   H = ");
   Serial.print(H);
   Serial.print(",   s1 = ");
   Serial.print(dta[2]);
   Serial.print(",   s2 = ");
   Serial.print(dta[3]);
   Serial.print(",   s3 = ");
   Serial.print(dta[4]);
   Serial.print(",   s4 = ");
   Serial.print(dta[5]);
   Serial.print(",   HB = ");
   Serial.println(HB);
  }
 
 
Logged

Charlie R Chisholm

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
The data is sent with xBee.print(255), but when it is received, it comes over as the ascii representation of each digit....50,53,53.
Why does that surprise you? That is what the print() method does.

To send binary data, use the write() method.
Logged

St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It surprizes me because half the time I don't know what I am doing and the other half, I don't remember.

Thank you.
Logged

Charlie R Chisholm

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 615
Posts: 49388
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It surprizes me because half the time I don't know what I am doing and the other half, I don't remember.
That made my day.
Logged

St Petersburg, FL
Offline Offline
Jr. Member
**
Karma: 0
Posts: 70
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That was it.  Thanks again.
Logged

Charlie R Chisholm

Pages: [1]   Go Up
Jump to: