Click here to Skip to main content
15,507,387 members
Please Sign up or sign in to vote.
3.22/5 (2 votes)
Hi All,

Currently I try to write a serial port communication in VC++ to transfer data from PC and robot via XBee transmitter. But after I wrote some commands to poll data from robot, I can not receive anything from the robot (the output of buffer is empty.). One problem may be that I have no idea how big of data I will receive from robot. So I am not sure whether I set the size of "buffer" to 257 is correct. In addition, the output of "error" is always 1. Because my MATLAB interface works, so the problem should happen in the code not the hardware or communication. Would you please give me help?

#include "StdAfx.h"
#include <iostream>
#define WIN32_LEAN_AND_MEAN //for GetCommState command
#include "Windows.h"
#include <WinBase.h>

using namespace std;

int main(){
  char init[]="";

  HANDLE serialHandle;

  // Open serial port
  serialHandle = CreateFile("\\\\.\\COM8", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

// Do some basic settings
  DCB serialParams;
  DWORD read, written;
  serialParams.DCBlength = sizeof(serialParams);

  if((GetCommState(serialHandle, &serialParams)==0))
	printf("Get configuration port has a problem.");
    return FALSE;

   GetCommState(serialHandle, &serialParams);
   serialParams.BaudRate = CBR_57600;
   serialParams.ByteSize = 8;
   serialParams.StopBits = ONESTOPBIT;
   serialParams.Parity = NOPARITY;

   //set flow control="hardware"

   if (!SetCommState(serialHandle, &serialParams))
	   printf("Set configuration port has a problem.");
       return FALSE;


   GetCommState(serialHandle, &serialParams);

   // Set timeouts
   COMMTIMEOUTS timeout = { 0 };
   timeout.ReadIntervalTimeout = 3;
   timeout.ReadTotalTimeoutConstant = 3;
   timeout.ReadTotalTimeoutMultiplier = 3;
   timeout.WriteTotalTimeoutConstant = 3;
   timeout.WriteTotalTimeoutMultiplier = 3;

   SetCommTimeouts(serialHandle, &timeout);

   if (!SetCommTimeouts(serialHandle, &timeout))
	   printf("Set configuration port has a problem.");
       return FALSE;


   //write packet to poll data from robot

   //check whether the data can be received
   char buffer[257];

 int t;
 bool error;
 DWORD numberOfBytesRead=0;
 DWORD err = GetLastError();
  for (int jj=0;jj<10;jj++)

   return 0;
Updated 5-Jan-14 12:45pm
CPallini 4-Jan-14 16:39pm    
Why did you set to 3 all the timeout constants?
What is exactly the problem? Did the first ReadFile call fail?
[no name] 4-Jan-14 20:30pm    
Thanks for your reply. Which timeout constants you recommend me to removed? I am not sure whether the ReadFile call is fail or not. The problem is that the output of buffer is empty which means that I can not receive any data from robot.
CPallini 5-Jan-14 3:54am    
Timeouts looks a bit to short to me. You should allow for tens of milliseconds, at least.
Since you are checking the ReadFile return value, then you should know if it failed or not.
[no name] 5-Jan-14 12:55pm    
Hi, I have done what you suggest. But it still not works. Do you think that I set flow control to hardware and close USB cable adapter correctly in the code?
CPallini 5-Jan-14 15:28pm    
You should use the same settings the Matlab code does.
I would try also to manually send packets with a communication application like putty to check if the microcontroller answers.

1 solution

if the rc is 1 the ReadFile is sucessful. The the count of readed bytes to detect for calling ReadFile again.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900