Hi! If someone can help me with this problem.
First of all, i tried to send a char array from one Arduino Due to another Arduino Due by the serial port. From my perspective the simpliest way to do this is like it's shown in this article -> Serial Input Basics - updated - Introductory Tutorials - Arduino Forum
I based my solution basically like the Example #5 showed in the article. It's work when i send a message by the Monitor Serial, but, if i try to send a message by a another Serial port the data doesn't look complete. I don't know why... Help!
The sender code:
void setup()
{
Serial.begin(9600);
Serial3.begin(9600);//Iniciamos serial para nivel inferior
}
void loop(){
char msg[32];
strcpy(msg,"#-29.12345,-71.12345,120@");
Serial.println(msg);
Serial3.println(msg);
}
The receiver code:
// Example 5 - Receive with start- and end-markers combined with parsing
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing
// variables to hold the parsed data
float latitud = 0.0;
float longitud = 0.0;
int orientacion = 0;
boolean newData = false;
//============
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
//============
void loop() {
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
// this temporary copy is necessary to protect the original data
// because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}
//============
void recvWithStartEndMarkers() {
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '#';
char endMarker = '@';
char rc;
while (Serial1.available() > 0 && newData == false) {
rc = Serial1.read();
if (recvInProgress == true) {
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() { // split the data into its parts
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, ","); // get the first part - the string
latitud = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
longitud = atof(strtokIndx); // convert this part to a float
strtokIndx = strtok(NULL, ",");
orientacion = atoi(strtokIndx); // convert this part to an integer
}
//============
void showParsedData() {
Serial.print("Latitud ");
Serial.println(latitud, 5);
Serial.print("Longitud ");
Serial.println(longitud, 5);
Serial.print("Orientacion ");
Serial.println(orientacion);
}
Like i said, the receiver code it's very similar to the example #5. The only difference it's in the Serial port that i listen and the variables I Parsed to.