Reading data sent from xbee in c++

11 Nov 2012

I have an xbee Series 1 unit wired to an mbed, and I would like to send data from other sensors attached to the mbed to my laptop and read in the data using C++. The relevant part of the code are below.

On the mbed side I have:

Serial xbee(p13, p14);
while(true) {
    xbee.printf("%4.2f %d\n", th, range);
    update();
}

On my laptop, I am trying to read the data as it comes in by opening a serial port using Qt/C++:

#include <fcntl.h>
#include <termios.h>
#include <fstream>
#include "mainwindow.h"

void MainWindow::initPort(int *fd)
{
    struct termios options;
    tcgetattr(*fd, &options);
    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);
    options.c_cflag |= (CLOCAL | CREAD);
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    tcsetattr(*fd, TCSANOW, &options);
    fcntl(*fd, F_SETFL, FNDELAY);
}

void MainWindow::on_start_button_clicked()
{
    fd = open("/dev/tty.usbserial-A800fcAc", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        std::cout << "Error" << std::endl;
    }
    else {
        initPort(&fd);
        // Read in the data
        bool stop = false;
        while (!stop) {
            char buffer[64];
            read(fd, buffer, sizeof(buffer));
            char* end = buffer + sizeof(buffer) / sizeof(buffer[0]);
            if (std::find(buffer, end, 'x') != end) {
                stop = true;
            }
            if (buffer[0] != NULL) {
                std::cout << buffer;
            }
            buffer[0] = 0;
     }
     std::cout << std::endl;
}

As can be seen in the mbed code, the data being printed out is of the form "float int". However, when I get the data on the laptop side, it often comes out looking like this:

2.854 1333
2.93 742
3.02 1177
3.02 
1098
3.11 1021

3.11 943
-1021

3.08 610

Any thoughts as to why the data is getting garbled? Playing around with how I'm printing out the data affects quality, which makes me think it's some sort of timing issue: that characters are getting lost or double counted from the stream. Thanks in advance.

22 Nov 2012

I figured it out. I need to open the serial port first, and then use fopen to read from it as a file. Example shown below:

fd = open("/dev/tty.usbserial-A800fcAc", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd != -1) {
        initPort(&fd);
    }
file = fopen("/dev/tty.usbserial-A800fcAc", "r+");
25 Jan 2014
20 Feb 2014

Hi Phillip, I started using the xbee platform a while ago and had similar issues. To help make life a bit easier, I wrote an api to interface to the DigiMesh firmware on the xbee. it might help with what you're doing and shouldn't be hard to adjust for the other mesh/firmware types. http://anthonypocock.github.io/QtXbee/

04 Jan 2017

I have started working on xbee module with mbed, can any one help how can i make use of the xbee libraries. I am not getting how to add those libraries in my program