[EDIT] I was totally unaware that we had access to the entire standard C library from arduino. That is awesome. The code now works perfectly. I added a strcat to append a null terminator to printed text. This is to prevent buffer overflows.
void print(char string[]) {
strcat(string, "\0");
Serial.print('S', BYTE);
Serial.print(strlen(string), BYTE);
Serial.print(string);
}
[/EDIT]
I got it working. When I was passing char[] to the print() function it was being passed as a pointer, so sizeof() returned 1, apparently causing all sorts of chaos. I tried to find an elegant solution, but a friend of mine who is comfortable with C suggested that I use null-terminated strings, or pass the length as a parameter, so I created an overloaded function like so:
void print(char string[], int length) {
Serial.print('S', BYTE);
Serial.print(length, BYTE);
Serial.print(string);
}
void print(char string[]) {
Serial.print('S', BYTE);
Serial.print(strlen(string), BYTE);
Serial.print(string);
}
int strlen(char str[]) {
int i;
for(i=0; i<=255; i++) {
if(str[i]=='\0') {
return(i);
}
}
}
It can be used as either print("Junk", 4); or print("Junk\0");
If anyone has a better solution, please let me know. Otherwise, it works.
Thanks for your help Horace.
Tim