My android app is going to send a stream of bytes, 2 byte at a time.
The arduino is to receive them 2 byte at a time, and reply "w" when it receives them, telling android to send then next 2.
The problem i got it that sometimes, the stream pauses due to android not receiving a "w", as "w" might be sent during the times the Android might be processing the next byte, and flags in there were reset, ignoring the "w" received.
So I want to let the arduino, whenever the serial buffer is 0, to
1)send a "w"
2)send a "w" again, after 1millisecond, if no response is received.
Is this the right way to do it?
unsigned long Timer;
unsigned long inteval = 1;
while( s0 != '|' || s1 != '|' ){//terminates if '|' is received
if(s0 == '|' || s1 == '|')break;
else if(HWSERIAL.available() == 0){
HWSERIAL.print("w");//tell Android to send next byte
Timer = millis();
if(HWSERIAL.available() == 0 && millis() - Timer > 1){
HWSERIAL.print("w");//tell Android to send next byte
}
}
else if(HWSERIAL.available() >= 2) {
s0=HWSERIAL.read();
s1=HWSERIAL.read();
<some IO operations>
You should also seriously look into why the message gets lost, as that shouldn't happen to begin with. I bet that device of yours will have some kind of serial buffer.
You should also seriously look into why the message gets lost, as that shouldn't happen to begin with. I bet that device of yours will have some kind of serial buffer.
the "w" sent to android might've gotten lost while the flags are being set.
Android app: on thread 1: receives "w" sets its flag to true
on thread 2: processes and sends 2bytes to Arduino, set flag to false
Arduino: receives the 2 bytes, replies with "w", wait for next 2 bytes.
on multiple occasions, the stream stops completely. and i cant make the arduino keep sending "w" without some sort of control. the Android app will crash due to too many internal broadcasts(broadcast is sent whenever incoming message).