Hi All,
Currently I try to use serial communication (XBee) to communicate between the PC and the robot via vc++. I have done the same thing via MATLAB. So there is no problem
for the hardware.
After I write commands to poll data, I can not receive any data. The interface requirement is:
Baudrate: 57600Flow control: ”Hardware” for XBee, ”None” for
USB cable adapter
Databits: 8, 1 startbit, 1 stopbit, no parity
Please give me your help.
// This is the main DLL file.
#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 written;
DWORD read;
char buffer[1];
char data[103];
int t=0;
int jj=0;
//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"
serialParams.fOutX=false;
serialParams.fInX=false;
serialParams.fOutxCtsFlow=true;
serialParams.fOutxDsrFlow=true;
serialParams.fDsrSensitivity=true;
serialParams.fRtsControl=RTS_CONTROL_HANDSHAKE;
serialParams.fDtrControl=DTR_CONTROL_HANDSHAKE;
//suggest to apply
//serialParams.fBinary=true;
//serialParams.fParity=true;
if (!SetCommState(serialHandle, &serialParams))
{
printf("Set configuration port has a problem.");
return FALSE;
}
GetCommState(serialHandle, &serialParams);
// Set timeouts
COMMTIMEOUTS timeout = { 0 };
timeout.ReadIntervalTimeout = 100;
timeout.ReadTotalTimeoutConstant = 100;
timeout.ReadTotalTimeoutMultiplier = 100;
timeout.WriteTotalTimeoutConstant = 100;
timeout.WriteTotalTimeoutMultiplier = 100;
SetCommTimeouts(serialHandle, &timeout);
if (!SetCommTimeouts(serialHandle, &timeout))
{
printf("Set configuration port has a problem.");
return FALSE;
}
bool err1;
//clear the buffer
PurgeComm(serialHandle,PURGE_RXCLEAR);
//write >*>p0x0004 to poll IMU_CalcData from robot
BYTE test[]={62,42,62,112,4};
WriteFile(serialHandle, test,5,&written,NULL);
do {
data[jj]=0;
err1=ReadFile (serialHandle,&data[jj],1,&read, NULL);
cout<<read;
cout<<(int)data[jj];
cout<<err1;
cout<<"\n";
if (read!=0)
{
jj++;
}
t++;
Sleep(10);
} while (t<100);
CloseHandle(serialHandle);
return 0;
}