De-Escaping a char array?

I'm wanting to use a Arduino Mega as a generic IO platform for a commercial (and proprietary) control system. It is particularly useful because of it's 3 UARTs. The general idea is that the control system will connect to the Mega via I2C and be able to use all of the Mega's IO.

I've come up with a basic protocol for the control system to use to talk to the Mega. It's pretty straight-forward and things were going smoothly until I got to the serial ports. I want the control system to be able to send ascii text strings with escape codes in them. Ex:

\x02Some Command\x03

The arduino would take that as a byte array or char array and replace the escape codes with the actual numeric code. From the example above, cmd[0] = 2 and cmd[13] = 3. (Corrected!)

It's not important to me that the escape codes are formatted in a particular way. In other words, the code could be \x02 or \02 or $02. I might slightly prefer generic C type codes.

Are there any built in functions that could make this easier for me or do I just have to bite the bullet and write it myself?

Thanks for any guidance.

\x02Some Command\x03

From the example above, cmd[0] = 2 and cmd[13] = 2.

Can you please expand on how the two relate to one another in your example as I am not seeing it ?

Presumably it should be cmd[13] = 3.

If you never need to send a NULL byte \x00, then you could send the bytes as-is without escaping them.

Pete

If you can you design the protocol completely what about sending the data as a,message,b where a and b could be the byte values for the codes.

That is easy to parse and the Arduino could just concatenate the a and b to the start and end of the message.

Does the system need to receive data?

...R

UKHeliBob:
\x02Some Command\x03Can you please expand on how the two relate to one another in your example as I am not seeing it ?

Sorry. cmd[0] = 2 and cmd[13] = 3. So the 3 chars '\x02' would be replace with 1 byte which would be 2.

Thanks for the prompt replies!

el_supremo, I do need to be able to send null bytes.

Robin2, Since I don't know what equipment the arduino will be connected to, I can't mandate a specific protocol. The system does need to receive data as well.

The equipment I control DO NOT translate escape codes. So I can't just pass them, I have to convert them to the actual thing before passing the control string.

I thought that, since C strings automatically convert escape codes from string literals that there might be some mechanism there that I could take advantage of.

The char array
\x02Some Command\x03

will actually be

{ 27 2 S o m e - C o m m a n d 27 3 }

so you cannot just replace the ESC02 -> 2 because you will get "holes in your string"

What you could do:

  1. You receive the char array's one byte at a time. If an escape comes in, go to special mode that processes the netx byte
  2. write a filter that processes a char array
bool escapeFlag = false;
char cmd[32];

void loop()
{
  if (Serial.available() > 0)
  {
    int c = Serial.read();
    if (escapeFlag)
    {
      escapeFalg = false;
      command[idx++] = c-'0'; // change ascii '2' 0x32  to byte 2  0x02
    }
    else if (c == ESCAPE) 
    {
      escapeFlag = true;
      continue;
    }
    else
    {
      command[idx++] = c; // leave rest intact
    }
   }
   // check if command can be processed
}

solution 2 uses a similar trick, only it reads from one array and writes to another array.