Hello, i was trying to adapt a code i use on an arduino mega 2560 to recieve and parse serial data from an XBee, however it seems that the following code isn´t working:
const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars];
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
float floatFromPC = 0.00;
String Valuelist[15] = {"Paquetes rcv", "Tiempo instantaneo", "Numero de paquete", "Altitud BMP", "TAtmosferica BMP", "Presion", "Tension de la bateria", "Longitud", "Latitud", "Altitud GPS", "Satelites GPS", "Etapa de vuelo", "GPS Hora", "GPS Minutos", "GPS Segundos"};
boolean newData = false;
float Floatlist[15];
float RCV = -1;
float prev = 0;
float intervalo = 0;
void setup() {
Serial1.setRX(1);
Serial1.setTX(0);
Serial.begin(115200);
Serial1.begin(115200);
while (!Serial);
Serial.println("Listo");
}
void loop() {
//Serial.println(Serial.readString());
recvWithStartEndMarkers();
if (newData == true) {
strcpy(tempChars, receivedChars);
parseData();
//showParsedData();
parX();
newData = false;
}
}
void parX() {
int messageFromP = atoi(messageFromPC);
if ((messageFromP >= 0) && (messageFromP <= 14)) { // no permite que se exceda
String propiedad;
propiedad = Valuelist[messageFromP] + ": ";
Floatlist[messageFromP] = floatFromPC;
//Serial.print(propiedad);
//Serial.println(Floatlist[messageFromP]);
if (messageFromP == 14) {
RCV++;
Floatlist[0] = RCV;
handoff();
}
}
else {
(Serial.println("error de paquete"));
}
}
void handoff() {
float timer1 = millis();
if (timer1 - prev >= intervalo) {
prev = timer1;
for (int n = 0; n < 15; n++)
{
//Serial.print(Valuelist[n]);
//Serial.print(": ");
Serial.print(Floatlist[n], 10);
Serial.print(", ");
}
//Serial.println("pkg");
}
Serial.println();
}
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';
recvInProgress = false;
ndx = 0;
newData = true;
}
}
else if (rc == startMarker) {
recvInProgress = true;
}
}
}
//============
void parseData() {
char * strtokIndx;
strtokIndx = strtok(tempChars, ",");
strcpy(messageFromPC, strtokIndx);
strtokIndx = strtok(NULL, ",");
floatFromPC = atof(strtokIndx);
}
//============
void showParsedData() {
Serial.print("Message ");
Serial.println(messageFromPC);
Serial.print("Integer ");
Serial.println(floatFromPC, 4);
}
I managed to make it work some times after uncommenting and then commenting again the
Serial.println(Serial.readString());
line from the code.
The application i´m using it for sends a string with <data1,data2,....,data15> format.
For this i used the following code as a baseline Serial Input Basics - updated
I´m using the Earle Philhower core if it matters, and would appreciate any help, thanks!
