Friday, June 17, 2016

About the serial ports

A few things I've learned:
1.  The Arduino Uno only has 1 hard serial port.

2.  The Mega has more.

3.  Most people use the SoftSerial Library to create a software serial port for the xbee.  The Sparkfun xbee shield assumes that you'll be using it like this.  When the switch is in DLine mode, the xbee serial port is on pins D2 and D3.  Make sure that the code is set this way!

4.  All information received and transmitted by the xbee will go through the soft serial port.

5.  Use the hard serial port for debugging and printing to the serial monitor.

Here are parts of code as examples of how to use the serial ports

#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(2, 3); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop()
{
Serial.println(XBee.available()); //prints # bytes in transmitted message
if (XBee.available()>10) //needs 11 bytes to proceed - this is the xbee soft serial port
  {
    incomingByte = XBee.read(); //read the first byte from xbee soft serial
    Serial.println(incomingByte);  //print the first byte to the hard serial port (i.e.serial monitor)
  }

}


No comments:

Post a Comment