Hi all,
I have a master slave network where the master sends a character to the slave and the slave sends data back. The master then reads it and prints the output to the serial monitor. The problem is that the order is sometimes messed up. Everything is there but the order wrong. The order starts correct then changes randomly.
Here is how the data is sent from the slave. I’m transmitting using a MAX485 chip so I have to use delays and a control pin.
if (Serial.find(address)) { //Outputs data only if called for
digitalWrite(control, HIGH); //transmit enable
delay(20);
//Print gyro data
Serial.println(GX);
Serial.println(GY);
Serial.println(GZ);
//Print accel data
Serial.println(AX);
Serial.println(AY);
Serial.println(AZ);
//Print magnetometer data
Serial.println(MX);
Serial.println(MY);
Serial.println(MZ);
Serial.flush(); //send out all data before closing communication module
digitalWrite(control, LOW); //receive enable
delay(20);
}
Here is the master receiving the code
digitalWrite(control, HIGH); //transmit enable
delay(20);
Serial2.println('B'); //request to send data
Serial2.flush(); //make sure data is sent before setting pin to recieve
digitalWrite(control, LOW); //recieve enable
delay(20);
Serial.print("Bytes in port before read ");
Serial.println(Serial2.available());
if (Serial2.available()) {
for (i = 0; i < 9; i++) {
inputB[i] = Serial2.parseInt();
}
Serial.print("Bytes in port after read ");
Serial.println(Serial2.available());
while (Serial2.available()>1) { //empty the serial buffer
Serial2.read();
}
} else {
Serial.println("Not Available");
}
//Outputs data to computer via USB and also to datalogger
for (i = 0; i < 3; i++) {
Serial.print(inputB[i]);
Serial.print(",");
}
for (i = 3; i < 7; i++) {
Serial.print(inputB[i]);
Serial.print(",");
}
for (i = 7; i < 9; i++) {
Serial.print(inputB[i]);
Serial.print(",");
}
int stamp = now();
Serial.print(stamp);
Serial.println("");
Serial.println(" ");
Serial.flush();
delay(250);
Here is a printout of what the serial monitor displays. The last number is a timestamp added in the master, it never changes position
Bytes in port before read 5
Bytes in port after read 1
-12,52,-14,-20,25,1191,-2396,-1011,-4709,0
Bytes in port before read 55
Bytes in port after read 7
-12,66,-13,-21,26,1193,-2380,-997,-4743,0
Bytes in port before read 63
Bytes in port after read 15
57,-10,-20,25,1192,-2395,-997,-4743,-21,1 (notice the shift here, 2nd last digit should be -4743)
Bytes in port before read 50
Bytes in port after read 1
56,-11,-19,24,1192,-2407,-1053,-4694,-16,1
Here is another showing 2 shifts
Bytes in port before read 50
Bytes in port after read 1
53,-10,-20,27,1192,-2394,-1039,-4710,-10,138
Bytes in port before read 50
Bytes in port after read 1
57,-11,-21,27,1193,-2350,-1081,-4727,-14,139
Bytes in port before read 60
Bytes in port after read 3
58,-9,-20,27,1193,-2409,-1039,-4710,-4727,139
Bytes in port before read 54
Bytes in port after read 7
-11,48,-21,-19,25,1191,-2423,-1066,-4745,139
Bytes in port before read 63
Bytes in port after read 15
57,-11,-24,27,1190,-2380,-1025,-4727,-7,140
Bytes in port before read 63
Bytes in port after read 18
56,-7,-21,25,1192,-2380,-1025,-4709,-8,140
Anybody have any idea how or why this is happening and what I can do about it?