Sometimes Im not getting all the chars that esp is sending but its very hard to detect most of the time this function is working as it should. Are there any mistakes that Im missing or any suggestions how to improve?
Thanks.
void listener(char a[]) {
char serial_stream = 0;
unsigned int i = 0;
unsigned long _time = 0;
const unsigned int max_time = 10000;
const byte delay_time = 1;
bool flag=false;
_time = millis();
while ((millis() - _time) <= max_time) {
if (mySerial.available() > 1) break;
}
while ((mySerial.available() > 0) && (serial_stream = mySerial.read()) != '\n') {
if (serial_stream >= 32 && serial_stream <= 127) {
flag=true;
a[i] = serial_stream;
if (a[i] == '>') {
while (mySerial.available() > 0) mySerial.read();
break;
}
_time = millis();
while ((millis() - _time) < delay_time);
Serial.write(a[i]);//write to serial window
i++;
}
}
if (flag) a[i + 1] = '\0';
return;
}
you read super fast and possibly empty the buffer, maybe without having read all the input that's still coming in, then mySerial.available() with return 0 and you'll exit without having read all the input.
I would recommend your read Serial Input Basics to see how to deal with Serial correctly.
Softserial at 9600.
I put 1ms delay between reading chars (const byte delay_time in while ).
I have read serial imput basics by Robin2 but still cant see how to inplement that in my case.
Sometimes I`m not getting all the chars that esp is sending
How do you KNOW that?
but its very hard to detect
If the sender was REQUIRED to send an end-of-data marker, then it would be SIMPLE to detect that you got that marker.
serial_stream = mySerial.read()
I'm sorry, but serial_stream is a stupid name for a char variable.
if (a[i] == '>') {
while (mySerial.available() > 0) mySerial.read();
break;
}
You apparently ARE sending an end of packet marker. When it is found, throwing away all unread data that followed it is hardly the mark of the sharpest crayon in the box.
surepic:
I put 1ms delay between reading chars (const byte delay_time in while ).
at 9600 bauds, a 1ms delay means roughly less than 1 extra character can arrive into your buffer. so you are likely to starve. --> don't try to second guess asynchronous communication by adding synchronous wait times, it's a recipe for failure...
Ok. Jml pointed right to my first while loop no sense to wait for 2 chars anyway it will exit after timeout but will change to 1 char.
So esp in at command mode is sending stringns terminated by new line char. But when in transmission mode (‘>’) there is no terminating char. I checked with docklight its sending just >. So im catching that char and breaking the loop as you see to deal with that case.
@PaulS
I was supprised you didnt noticed char array named “a”
How i know that sometimes im missing chars because im echoing all data back that im sending.
After > there is no data to come thats the reason to break the loop.
I read that st 9600bps gap between data is 1.04ms thats why i put 1ms delay knowing thats not the way i would like to solve that problem.
Robin2 i’ve read your article many times and most part of this function is from uour tutorial but cant go any further.
@PaulS
About data after > i realized while i was replying to your previous question. You are right no need to check for anything after >.
Serial.write is right after filtering unnecessary chars to understand what string will be formed at the end. Now you will ask “what unnecessary chars?” Chars that are being sent from esp right after restart and are readable only at baud rate 74880. Thats the reason for filtering in if.
Whole code was autoformated with ctrl+t before posting. I edited 2 lines of the code in topic couple times that may cause it look unformatted. And I apologize for that.
Why i put 1ms deay knowing beforehand thats not right way because as follows from reply#6 i couldnt think of better solution. And from all solutions that came to my mind i picked the best one.
Couldnt find any restriction in forum about posting functions,statements or sketch.
Currently i am having issue only within this function other functions are functioning properly.
Using delay() will not help you out. As Paul explained you need to build code that checks if something is available to read, is so read it, if not go do something else.
You need to have a state machine. First state is “waiting for start sequence”, then once you have received that, the second state is “acquiring message” until you receive the end sequence where you go to a “message received” state which should trigger analysis of your message, and once handled you. Go back to your “waiting for start sequence” state.