Hi folks,
first of all again thank you for so much support during the last months and years while I am more and more familiar with arduino and programming.
Backgroud of my project:
I would like to extend the ICSC lib of Majenko (see at github) with a confirmation function in the way that the receiver always returns the transmitted data with another command. Then the sender can compare the sent data with the received data to ensure it was transmitted correctly.
Solution:
First I send my data on the way.
Second I store al the necessary values in a string with separators.
Third In case of unsuccessful transmission I unfold the string with strtok or whatever and then I can send the data again.
Problem:
When I unfold the string there is something going wrong. First I was trying to use strtok but this didn’t work out because data was empty. So I tried to do it on the long why (code below), which compiles without errors but is not accepted by the ICSC library in the way that nothing is sent so far.
The function I use to send messages:
boolean _ICSC::send(unsigned char origin, unsigned char station, char command, unsigned char len, char *data)
Do you have any idea what went wrong in this case?
If you have a completely different approach I would also be open to discuss.
Thank you so much,
curtuino
// demo message:
char b[10];
sprintf(b, "%ld", last_sent);
Serial.print("Sending ");
Serial.print(millis());
Serial.print(": ");
Serial.println(b);
last_sent = millis();
// this works perfect sending a message:
//ICSC.send(slaveAddress, demoCommand, 10, b);
String saveString = String(slaveAddress) + ";" + String(demoCommand) + ";" + String(b);
Serial.print("SaveString: "); Serial.println(saveString);
char saveChar[saveString.length() + 1];
saveString.toCharArray(saveChar, saveString.length() + 1);
char recT[3];
char comT[3];
char datT[saveString.length() + 1];
// slow way instead of strtok ;-/
int var = 0;
int recInt = 0;
int comInt = 0;
int datInt = 0;
String strRec, strCom, strDat;
for(int i = 0; i < sizeof(saveChar); i++){
//Serial.print("current i: "); Serial.print(i);
if(saveChar[i] != ';'){
//Serial.print(" var: "); Serial.print(var);
//Serial.print(" current character: "); Serial.println(saveChar[i]);
switch (var){
case 0:
strRec += saveChar[i];
recInt++;
break;
case 1:
strCom += saveChar[i];
comInt++;
break;
case 2:
strDat += saveChar[i];
datInt++;
break;
}
} else {
//Serial.print(" current character: "); Serial.println(saveChar[i]);
var++;
}
}
/*
//This is the approach I left behind:
char recPointer[] = strtok(saveChar, stringSeparator);
int comPointer = strtok(NULL, stringSeparator);
char *rec= {strtok(saveChar, stringSeparator)};
char recChar[] = { rec, "\0"};
char com[] = {strtok(NULL, stringSeparator)};
//char len[] = strtok(NULL, stringSeparator)};
char dat[] = {strtok(NULL, stringSeparator)};
*/
// this looks correct as far as I can see in serial debugging:
Serial.print(recInt); Serial.print(":");
Serial.print(strRec);
Serial.print("-");
Serial.print(comInt); Serial.print(":");
Serial.print(strCom);
Serial.print("-");
Serial.print(datInt); Serial.print(":");
Serial.print(strDat);
Serial.println("");
char charRec[strRec.length()+1];
strRec.toCharArray(charRec, strRec.length()+1);
int newRec = atoi(charRec);
char newCom[strCom.length()+1];
strCom.toCharArray(newCom, strCom.length()+1);
newCom[strCom.length()+1] = '\n';
char* newDat;
strDat.toCharArray(newDat, strDat.length()+1);
newDat[strDat.length()+1] = '\n';
// sending a message here doesn't work ;-(
ICSC.send(newRec, newCom[0], strDat.length(), *newDat);