C++ issue - sending commands to ssc-32

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.

RESOLVED!

For future reference, if anyone has this issue and comes across this thread in a search:

The following loop shows the hex content of a string:

for (string::iterator i = YOUR_STRING_NAME.begin(); i != YOUR_STRING_NAME.end(); ++i)
{
cout << “0x” << hex << (int)*i << ’ ';
}

and this one shows the hex content of a char array:

for (int i = 0; i < sizeof(YOUR_CHAR_ARRAY); ++i)
{
cout << “0x” << hex << (int)YOUR_CHAR_ARRAY* << ’ ';
}

Using these two debugging methods, I was able to see that my command was actually being truncated for some reason… and not only that, but my carriage return was being dropped out of the string when I tried to append it! I moved the transmission-to-board method inside of my string-to-char-array conversion method, explicitly added a carriage return like so:

outString[stringPointer] = (char)13;

(where outString is actually the name of my outgoing char array, and stringPointer is an index to the position in the char array)

and the entire command made it through successfully.

Hope this helps someone!*

Glad you got it going. I don’t know C or I would have tried to help. :frowning:

So why was your string being truncated, and your CR/LF being lost? Buffer size somewhere?

I like to use sprintf() and handle the constructions of strings myself. Older C code techniques…

Alan KM6VV