I am writing an Arduino library for simple data transfers between the Arduino and a computer using a serial feed. I have created the library, etc. However, I am having issues with taking a char array, and adding a colon (':') to it. That is,
//Sends data via println()
void simpleTransfer::sendData(char *name, char *data){
char *str = name + ": " + data + ",";
_serial->println(str); //Sends in form 'name: data,'
}
This should take the name of a variable that I want to send, add a colon and a space, and the data I want to send and finally a comma. However, I instead get error messages:
invalid operands of types 'char*' and 'const char [3]' to binary 'operator+'
What is the reason?
You could use sprintf:
char str[64]; // Or whatever length you need to fit the resulting string
sprintf(str, "%s:%s,", name, data);
Or strcpy/strcat:
char str[64];
strcpy(str, name);
strcat(str, ":");
strcat(str, data);
strcat(str, ",");
Or just use C++'s std::string.