Home/Support/Support Forum/Send and Receive packets with c++ program

Send and Receive packets with c++ program

0 votes

I'm working with a friend on a project for my engineering school and we have some issues to communicate with ours xbees PRO s1.

With XCTU xbees work perfectly (full duplex OK)

But, for our project we have to developp a program in c++ to communicate between xbees. The problem is that we cannot receive any data with the software but we can send them with our program and receive thanks to XCTU.
The program uses the library SerialPort.h and Windows.h

Here the code to receive :

int SerialPort::readSerialPort(char *buffer, unsigned int buf_size)
DWORD bytesRead;
unsigned int toRead;

ClearCommError(_handler,&_errors, &_status);

if (_status.cbInQue > 0){
if (_status.cbInQue > buf_size){
toRead = buf_size;
else toRead = _status.cbInQue;

if (ReadFile(_handler, buffer, toRead, &bytesRead, NULL)) return bytesRead;

return 0;

and the main.cpp :

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SerialPort.h"

using namespace std;

char *c_string;

int main()
SerialPort Xbee_rx((char*)"\\\\.\\COM4");
if (Xbee_rx.isConnected()) cout << "Connection etablie avec succes du recepteur" << endl;
else cout << "ERROR, pas le bon port";
while (1) {
cout << " Lecture effectuee : " << Xbee_rx.readSerialPort(c_string, MAX_DATA_LENGTH) << endl;

The xbee is well connected to the port but receive nothing, the program only returns 0.

Thank you in advance
asked May 14, 2018 in IEEE 802.15.4 by robinSauter New to the Community (0 points)

Please log in or register to answer this question.

1 Answer

0 votes
I'm not super-proficient with Windows serial ports to debug what you've posted, but...

Digi provides a C XBee lib. One of the platforms its been ported to is Win32/gcc. You don't need to pull in the whole library but you could likely take out the low-level serial driver and use that instead of rolling your own.


I've personally used the Win32 port for a customer application (Visual Studio instead of GCC) so it should work.

You can also run one of the samples in the library before pulling out the serial driver as a sanity check.
answered May 17, 2018 by jlovinger Veteran of the Digi Community (421 points)