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.
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:
On my laptop, I am trying to read the data as it comes in by opening a serial port using Qt/C++:
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:
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.