I have written a program which accepts human-readable commands (like, left hip 30 degrees), does some timing calculations, generates a groupmove and sends it off to the control board. The board successfully receives the information but does not act on it. Here’s the details:
if(WriteFile(hSerial, OUTCOMMAND, n, &dwBytesRead, NULL))
{
cout << endl << "telling Robot: " << thisGroupMove.gCMD.c_str() << endl;
}
Now, this WriteFile method takes char* data types. So if I initialize OUTCOMMAND like so:
char OUTCOMMAND] = "#4 P1450 S1000 /r"
Then the board receives and executes just fine. Problem is, this doesn’t do me much good.
My commands are generated as a string as follows:
// BUILD gCMD COMMAND STRING
int thisInt;
char thisChar[4];
gCMD = “”;
while (! srvQ.empty())
{
// LOAD SERVO NUMBER
thisInt = srvQ.front();
itoa(thisInt, thisChar, 10);
// APPEND TO COMMAND STRING
gCMD = gCMD += “#”;
gCMD = gCMD += thisChar;
srvQ.pop();
// LOAD POSITION
thisInt = posQ.front();
itoa(thisInt, thisChar, 10);
// APPEND TO COMMAND STRING
gCMD = gCMD += "P";
gCMD = gCMD += thisChar;
posQ.pop();
// LOAD SERVO NUMBER
thisInt = spdQ.front();
itoa(thisInt, thisChar, 10);
// APPEND TO COMMAND STRING
gCMD = gCMD += "S";
gCMD = gCMD += thisChar;
spdQ.pop();
}
// SET START TIME FOR COMMAND
startTime = lastTime;
So, in other words, I create an empty string and then I append, one by one, the pieces of the groupmove command until I have a complete command string.
However, when I try converting this into a character array and then passing it onto the control board, nothing happens. The message is received (txrx light flashes) but the board apparently does not recognize the command. I have tried appending a carriage return in the string itself, I have tried sending a second message to the serial board containing the carriage return, I have even tried manually sending a carriage return after sending my command. None of that works, so I suspect the problem is in my string-to-chararray conversion, or the original command string itself!
As for conversion methods from string to character array, I have tried looping through the string and copying character by character. This doesn’t work.
I have tried using strcpy and thisGroupMove.gCMD.c_str() methods to convert, this also doesn’t help.
What is going on here? If I initialize my outgoing command character array as
char OUTCMD] = “some command string”;
then it works. If I try converting my original groupmove command string into a character array, whether by char-by-char copy loop, or built-in copy method, then it doesn’t.
I have verified, many times over, that my generated groupmove command has the correct format, and after any of the conversion methods I’ve tried, the converted character array, upon printing to console, appears exactly as it should! So, there is something going on that is not clear upon visual inspection of the strings and char-arrays involved.
So is the problem with my command string itself?? I have tried everything and I am at my wits end.