Wire library handshaking

I'm using 2 arduinos with the Mega 328s. I'm using the wire library and sending data from the master board to the slave which is connected to a GLCD. I'm trying to send out a lot of character strings from the master and read them into the slave a read them on the display. The problem is that the data needs to be read into different variables and the slave arduino would not know where one string ended and the other begins. I figured that one way to seperate the data would be to put some kind of handshake in the process. I think after sending one variable, I could do a Wire.request for a byte or so from the slave and then move to the next variable and repeat the process. I'm having problems actually do this and was wondering if someone has done something similar or knows of a simple solution to the problem I've mentioned?

You just need to define a simple protocol for the messaging. It could be a specific leading character (that's not part of the your data set obviously), followed by length data (then you could dynamically allocate space for the remainder). If the strings aren't too long and dynamically allocating space seems like overkill then just go with the leading character and use that to pull the message apart. If the order is not deterministic then you could include a byte to indicate the type of data as well.

I've done exactly this sort of thing before using pipe characters ('|') to separate message fields, but you could use anything that's not going to be in your data (or if it is you would need to define an escape sequence).

Cheers,

I'm not really advanced enough to write code that will scan for a particular leading character and split the strings starting with the next character and ending with the next leading character. But maybe I could get it if someone showed me an example of this. Anybody ever done something like this before?

I'm not really advanced enough to write code that will scan for a particular leading character and split the strings starting with the next character and ending with the next leading character. But maybe I could get it if someone showed me an example of this. Anybody ever done something like this before?

Are you advanced enough the call the strtok() function? It will return a pointer to all the characters from the current position (either the start of the string or the last delimiter, depending on the first argument) to the specified delimiter, or one of several delimiters. It's google time...

That seems pretty close to what I need. I'm sending over each variable and then putting a # in between each one. I found this example code online. Now I'm wondering how would I go about saving each piece of the string into individual strings when it sees a #. I'm gonna keep googling til I can find something.

Example:

char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is "%s"\n", result );
result = strtok( NULL, delims );
}

OUTPUT:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"

Now I'm wondering how would I go about saving each piece of the string into individual strings when it sees a #. I'm gonna keep googling til I can find something.

Look for strdup() - Duplicate a string.

I'm having trouble finding information on that. Could I have an example if its not too much trouble?

I'm having trouble finding information on that. Could I have an example if its not too much trouble?

buffer copyOfString[40];
strdup(copyOfString, result);