Hello, I'm trying to receive a string through the serial port, but I can't capture it, there is simply no data, I don't understand what could be the problem, can someone tell me how to correct this so that it works?.
I need the capture routine to be in setup but not in loop
You don't wait at all, so there's nothing there and you return.
Try
void receive(){
while (1) { // basically forever...
if (Serial.available() > 0) {
received += (char)Serial.read();
if (received.endsWith("OK\r\n\r\n")) {
Serial.println("answer: " + received);
break;
}
}
}
}
Sry, can't test it from here. One reason it might not work too good is the awfully specific ending condition you have there.
Perhaps some other test, or a way to break out of the forever loop like too much time has passed or N number of characters received but no joy yet on the OK thing should be in there.
I'm not sure I can, but here goes.
In setup(), you queue-up a string "AT+DATAFRAME?" for transmission, then immediately, you call the function "receive()".
First, this function looks to see if there is something in the Serial receive buffer to read.
Almost certainly, there isn't, so the function "receive()" returns, the the function "setup()" returns, and the function "loop()" gets called, and does nothing.