Pages: [1] 2 3   Go Down
Author Topic: Baud Rate/ Serial Timing Problem?  (Read 4488 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I have two arduinos; an ATmega1280 and Duemilanove ATmega328. Both of these arduinos have XBees with the latest firmware, AES Encryption enabled, and 9600 baud rate. I am trying to run a local send/recieve session between the arduinos using the XBees. The arduinos seem to communicate correctly; I print something to serial from one, and I see it pop out on the Serial of the other... The problem I am having, however, is that when I am printing from the ATmega1280(Client) to the ATmega328(Server) I have to send the, "command", byte twice. The Server doesn't ever seem to read the first byte. I have tried changing the baud rates to 19200, and 115200 on the XBees, the software I wrote, and the Serial Monitor. With a baud rate of 115200 everything printed on the Serial Monitor is garbled, whereas with the baud rate at 19200, only the first character becomes garbled. If I write the command twice and have the baud rate at 9600 it seems to work, whereas at any other baud rate it doesn't. I have pasted some of my code below.

Server.ino:
Code:
const unsigned int BAUD_RATE = 9600;
const int TIMEOUT = 2000;

const byte ID = B00000011;
const byte CLIENT_ID = B00000001;
const byte OPEN_ID = B00000111;

void setup()
{
  Serial.begin(BAUD_RATE);
}

void loop()
{
  // Scanning for header
  if(Serial.available() > 0)
  {
    byte uID = Serial.read();
    if(uID == ID)
    {
      // Identifier found, read command.
      ReadCommand();
    }
  }
}

void ReadCommand()
{
  // 4320000000 max before overflow
  unsigned long lapse = millis() + TIMEOUT;
  while(millis() < lapse)
  {
    if(Serial.available() > 0)
    {
      byte uAc = Serial.read();
      switch (uAc)
      {
      case OPEN_ID:
        OpenDoor();
        break;
      }
    }
  }
}

void OpenDoor()
{
  SendResult(true);
}

void SendResult(boolean result)
{
  if(result)
  {
    Serial.write(SUCCESS);
  }
  else
  {
    Serial.write(FAILURE);
  }
}

Client.ino
Code:
// Constants
const unsigned int BAUD_RATE = 9600;
const int TIMEOUT = 2000;

const byte ID = B00000001;
const byte SERVER_ID = B00000011;
const byte OPEN_ID = B00000111;

// Fields
boolean toggle;

void setup()
{
  Serial.begin(BAUD_RATE);
  toggle = true;
}

void loop()
{
  if(toggle)
  {
    SendDoorOpen();
    toggle = false;
  }
}

void SendDoorOpen()
{
  // Need to write twice to work :(
  Serial.write(SERVER_ID);
  Serial.write(SERVER_ID);

  Serial.write(OPEN_ID);
  
  if(ReadResult())
  {
    Serial.println("Door succesfully opened.");
  }
  else
  {
    Serial.println("Door failed to open.");
  }
}

boolean ReadResult()
{
  // 4320000000 max before overflow
  unsigned long lapse = millis() + TIMEOUT;
  while(millis() < lapse)
  {
    if(Serial.available() > 0)
    {
      byte response = Serial.read();
      return response == SUCCESS;
    }
  }
  return false;
}
« Last Edit: January 18, 2012, 09:55:47 am by falven » Logged

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

Quote
Both of these arduinos have XBees with the latest firmware, AES Encryption enabled, and 9600 baud rate.
Xbees are not generic things like 10k Ohm resistors. Which XBees? How are they configured?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

XBee pro 802.15.4 on version 10ED with default firmware. The only value I changed is the AES Encryption, which i enabled and set an AES Encrption key.
« Last Edit: January 18, 2012, 06:11:28 pm by falven » Logged

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

Quote
The only value I changed is the AES Encryption, which i enabled and set an AES Encrption key.
So, both XBees are still using the default DL value of 0? So, everything is being broadcast to all other XBees?

That is NOT the proper way to do encrypted transmission, or any point-to-point, for that matter.

Assign DL and MY on both XBees. DL on one must match MY on the other. The exact values do not matter, except that the should not be 0.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So, both XBees are still using the default DL value of 0? So, everything is being broadcast to all other XBees?

That is NOT the proper way to do encrypted transmission, or any point-to-point, for that matter.

Assign DL and MY on both XBees. DL on one must match MY on the other. The exact values do not matter, except that the should not be 0.

I have set the DL on the client to 500 and the MY to 1000. I have set the DL on the Server to 1000 and the MY to 500. The problem still persists; the initial byte written by the client is not getting picked up by the Server.
Logged

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

Do you have the same problem without AES encryption?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Do you have the same problem without AES encryption?

Yes, here are my settings.

Client:


Server:


Sample code I loaded.

Client:
Code:
boolean toggle;
void setup()
{
  Serial.begin(9600);
  toggle = true;
}

void loop()
{
  if(toggle)
  {
    //delay(10);
    Serial.println("Hello");
    toggle = false;
  }
}

Server:
Code:
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available())
  {
    Serial.write(Serial.read());
  }
}

Results:


Second set of results after uncommenting the delay(10); in the Client code.


As you can see, as I stated above, the Server (Or client if the code is switched around) never reads the first byte of data, or adds in some garbage byte of data!

edit: if i set the delay to 1ms i get garbage on the first byte, if i set it to .99 i get no byte.

edit 2: ran a range test to see if the problem was interference, but I got clean results...
« Last Edit: January 19, 2012, 04:17:19 am by falven » Logged

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

edit: if i set the delay to 1ms i get garbage on the first byte, if i set it to .99 i get no byte.

The delay() function takes an integer argument, so 0.99 is the same as zero, that is, no delay.

It isn't particularly unusual for serial comms to need a brief "settling" period. After all, the clock is an implied clock (implied by the baud rate). I wouldn't get too excited if the first byte got lost.
Logged


0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It isn't particularly unusual for serial comms to need a brief "settling" period. After all, the clock is an implied clock (implied by the baud rate). I wouldn't get too excited if the first byte got lost.

Thank you for your input.

I, however, believe that this issue stems deeper than just a "settling period", because I have not been able to make the Xbee and Arduino work at any Baud rate higher than 19200. (Setting the baud rate of both the XBee and Arduino to the same values...)

At any pre-set baud rate > 19200, i get no data.

Any ideas?

Edit: Here's the output for data at a baud rate of 19200



Edit 2:
When I set the baud rate on the Xbees to 38400 and in Serial.begin(38400), I see no output in the Serial Monitor, at all. however, If I change the baud rate on the program to say 40000, I can see garbled data. Does this mean the XBees are running at a different baud rate than the one I wrote to them?But that wouldn't make sense because X-CTU can Test / Query them at the Baud Rate I wrote to them (38400)?
« Last Edit: January 19, 2012, 05:49:22 am by falven » Logged

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

There are a total of 6 baud rates involved - the rate that the PC talks to the Arduino with the 1st XBee, the rate that that Arduino talks to the PC/XBee, the rate that the first XBee talks to the other XBee, the rate that the second XBee talks to the other XBee, the rate that the 2nd Arduino talks to the XBee, the rate that the 2nd Arduino talks to the PC, and the rate that the PC talks to the 2nd Arduino.

Obviously, the 1st two need to match, so the Arduino and PC can understand each other. The 2nd pair need to match, so that the XBees can understand each other. The third pair needs to match so that the 2nd Arduino and PC can understand each other.

What might not be so obvious is that the 6 rates all need to be the same, so that the Arduino and XBee can communicate, since you are using the same port to talk to the PC and the XBee.

Typically, this is not a good idea. Depending on the XBee shield you have, you may be able to select a different set of pins for the Arduino and XBee to talk to each other on, using NewSoftSerial (0023-) or SoftwareSerial (1.0+). Then, you can use different speeds, if needed. Standard speeds, of course. 40000 is not a standard value.

If you can't do that, add a small delay() to setup() (maybe half a second), to give the XBees time to boot up, too.

Even with the XBees on different pins, the small delay in setup() may be required.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4089
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... the rate that the first XBee talks to the other XBee, the rate that the second XBee talks to the other XBee ...

I'm not aware that these are observable parameters, let alone configurable. My understanding is that how the XBees communicate to each other is a constant, and is independent of the settings for their local serial port, even to the extent of AT vs. API mode.

Quote
What might not be so obvious is that the 6 rates all need to be the same, so that the Arduino and XBee can communicate, since you are using the same port to talk to the PC and the XBee.

I'd be very surprised if I could not configure two XBees for different baud rates and still have them communicate perfectly. In fact, I'm pretty sure I've done this with the S2 modules. falven is using S1? Is there something about the S1 modules that is different from the S2 in this regard?

If it were me, at this point, I'd get the Arduinos out of the loop, and just try to make the XBees talk between each other, either with two terminal programs running on one PC, or one terminal program running on each of two PCs.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well? I still don't have an inkling as to how to fix this problem... This is pretty serious too, wait 'till you guys see this gem...



I am really surprised to see that nobody else has had this problem, at least I haven't been able to find a thread similar to this for the past couple of days...
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4089
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well? I still don't have an inkling as to how to fix this problem... This is pretty serious too, wait 'till you guys see this gem...

I am really surprised to see that nobody else has had this problem, at least I haven't been able to find a thread similar to this for the past couple of days...

That is a gem all right. Darn things behave pretty well for me. Any chance of trying the XBee-to-XBee thing without the Arduinos? I'm not entirely clear yet, these are Series 1 Pro models? (A link or exact model number would be great) What are you using for adapters and how are they powered?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 55
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
That is a gem all right. Darn things behave pretty well for me. Any chance of trying the XBee-to-XBee thing without the Arduinos? I'm not entirely clear yet, these are Series 1 Pro models? (A link or exact model number would be great) What are you using for adapters and how are they powered?

From left to right:
1. Arduino Duemilanove http://arduino.cc/en/Main/ArduinoBoardDuemilanove (Atmega328 edition) with XBee Pro s1 http://www.sparkfun.com/products/8742
2. Arduino Mega http://arduino.cc/en/Main/ArduinoBoardMega  (Atmega1280 edition) width XBee Pro s1 http://www.sparkfun.com/products/8742
3. XBee Explorer USB http://www.sparkfun.com/products/8687

Again, configuration and code is in Reply #6.



I only have one XBee Explorer to connect to the computer. Unless there is another way to connect an XBee... I might have an FTDI cable laying around, I will have a look.
« Last Edit: January 19, 2012, 07:40:18 pm by falven » Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4089
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks, the pic helps! Where do the shields come from?
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Pages: [1] 2 3   Go Up
Jump to: