Xbee communication errors

by bulmung on Fri Oct 05, 2012 9:23 pm

At the NY Maker Faire I won an xstick(http://store.digi.com/index.cfm?fuseaction=product.display&Product_ID=1273) and have been interested in playing around with xbees so I ordered the 1mW series 1 xbee and the usb adapter.

I got them communicating but when I go to create a variable from the serial read on the arduino it stores the same letter (y with dots over it) but the serial is receiving the correct message. So for instance, when I send "i" over hyperterminal the arduino receives the "i" and stores the "y". I am able to fix this however by sending two letters in quick succession with the second being the one I want it to store.

Is there some way to fix this?
Thanks!
bulmung
 
Posts: 12
Joined: Sun Dec 27, 2009 10:48 pm

Re: Xbee communication errors

by adafruit_support_rick on Fri Oct 05, 2012 10:33 pm

It would help to see your code, and some sample output. The "y with dots over it" signals a non-printable character (not to be confused with the unprintable words you no doubt uttered when the problem first occurred :D ). It is likely that you are storing some sort of binary number instead of the character you think you are storing.
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Xbee communication errors

by bulmung on Sat Oct 06, 2012 4:05 pm

Here's the code:
Code: Select all
char inByte;


void setup()  {
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  Serial.println("Connected");
}

void loop()             
{

if (Serial.available()) {
      Serial.println((char)Serial.read());
  inByte = (char)Serial.read();
  Serial.println(inByte);
  if (inByte == 'i')
  {
    digitalWrite(13, HIGH);
  }
  if (inByte == 'o')
  {
    digitalWrite(13, LOW);
  }
}
  delay(1000);
}


This is the output, first typing just one letter than two letters quickly, the first shows whats received over serial, the letter after shows whats stored.
Image
bulmung
 
Posts: 12
Joined: Sun Dec 27, 2009 10:48 pm

Re: Xbee communication errors

by adafruit_support_rick on Sun Oct 07, 2012 10:29 am

The problem is here:
Code: Select all
if (Serial.available()) {
      Serial.println((char)Serial.read());
  inByte = (char)Serial.read();
  Serial.println(inByte);


You are calling Serial.read() twice. If there is only one character available, the second call to Serial.read() will return -1 (which prints as the dotted 'y').

The good character will always go to the serial monitor, and the -1 character will always be used to control your LED.
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY

Re: Xbee communication errors

by bulmung on Sun Oct 07, 2012 10:49 am

Fixed! Thank you very much!
bulmung
 
Posts: 12
Joined: Sun Dec 27, 2009 10:48 pm

Re: Xbee communication errors

by adafruit_support_rick on Sun Oct 07, 2012 10:55 am

cool! :)
User avatar
adafruit_support_rick
 
Posts: 8569
Joined: Tue Mar 15, 2011 10:42 am
Location: Buffalo, NY