You will need to adjust your concept of serial communication so that you don't rely on the complete 90 bytes of data to be transferred to the serial input buffer before you read it into RAM.
Instead you need to continually empty the hardware buffer into RAM as often as you run through loop()
, and avoid using delay()
, which will stop your program from being able to drain the buffer.
To know when you have reached the end of the transmission, you should either ensure that the incoming serial stream has a marker at the end (such as a '\n'
character) or is always a fixed length (such as always exactly 90 bytes.)
As an example, here is a program that looks for '\n'
as the end-of-data marker:
#DEFINE MAX_INPUT 90
char inputBuffer[MAX_INPUT+1]; // Handles up to 90 bytes in a c-style string, with a null character termination.
void setup()
{
Serial.begin(115200); // initialization
inputBuffer[0] = '\0'; //Initialize string to emtpy.
Serial.println("Begin:");
}
void loop() {
if (Serial.available()>0)
{
char input = Serial.read();
static int s_len; // static variables default to 0
if (s_len>=MAX_INPUT) {
// Have received already the maximum number of characters
// Ignore all new input until line termination occurs
} else if (input != '\n' && input != '\r') {
inputBuffer[s_len++] = input;
} else {
// Have received a LF or CR character
// INSERT YOUR CODE HERE TO PROCESS THE RECEIVED DATA //
// YOU COULD COPY TO A NEW VARIABLE WITH strncpy() OR //
// SET A FLAG TO SAY TO START SOME OTHER TASK //
Serial.print("RECEIVED MSG: ");
Serial.println(inputBuffer);
memset(inputBuffer, 0, sizeof(inputBuffer));
s_len = 0; // Reset input buffer here if you
// have already copied the data.
// If you don't reset here, then
// you can't start receiving more
// serial port data. This is your
// 'software' serial buffer, contrast
// with the hardware serial buffer.
}
}
}
read_response()
function should work fine as it is - however you probably have adelay()
before it that overflows the buffer. Read this: hackingmajenkoblog.wordpress.com/2016/02/01/…