I haven't source code of server side and couldn't change anything... And there are a lot of queries to meter and answer can be any size.
Ok, you will have to buffer it like you are doing now. 64 bytes isn't a huge buffer, I just wondered if there was some way to eliminate it.
I read your Serial Input Basics Example 2 but couldn't understand how to use it for my sketch... and don't know what byte I can use as endMarker.
The "end marker" for you is the timeout. It's an end "condition", not a character like '\n'.
Right now your sketch watches for the first character to appear on Serial, and then it waits for the whole packet to come in. If no more characters have arrived after 50ms, it assumes the packet has been completely received:
void loop()
{
// read from port 1, send to port 0:
if (Serial.available())
{
ReadDataFromSerial3_buf(50);
WriteDataToSerial2();
}
I'd like to suggest moving all these statements into ReadDataFromSerial3_buf, or Read3 for short:
void loop()
{
// read from port 1, send to port 0:
Read3( 50 );
.
.
.
}
void Read3( int timeout_max )
{
if (Serial.available()) {
index_in_buffer = 0;
memset(in_bufer, '\0', BUF_SIZE_IN);
unsigned long start_time = millis();
do {
.
.
.
} while((millis() - start_time) < (unsigned long)timeout_max);
WriteDataToSerial2();
}
}
The real problem is that you are blocked inside this routine until all data has been received, plus 50ms. Too long! Serial Input Basics shows how to not block while waiting for a '\n'. To paraphrase that example:
int index = 0;
uint8_t buffer[ 64 ];
void loop()
{
while (Serial.available()) {
uint8_t c = Serial.read();
if (c == '\n') {
// End of line!
buffer[ index ] = 0; // NUL-terminate the array so we can...
Serial.println( buffer ); // ...write it out as a C string (Serial.write does not need NUL)
index = 0; // ...and start saving a new line.
} else {
// Save another char, if there is room
if (index < sizeof(buffer)-1) {
buffer[ index++ ] = c;
}
}
}
}
Notice how it does not stay in the while loop until the '\n' is received. It just saves whatever characters are available now, and then exits the while loop. Most of the time, Serial.avaiable() will be zero. No big deal, it just skips the while loop. When a character is finally available (once every 1000 calls!), it reads the character and either (1) saves it, or (2) prints out the whole buffer (and resets the index).
The Arduino is free to process everything else while the buffer is being accumulated from individual characters. Let's change that to an end condition, when no characters have been received for 50ms:
uint32_t lastRX;
void loop()
{
while (Serial.available()) {
uint8_t c = Serial.read();
// Save another char, if there is room
if (index < sizeof(buffer)-1) {
buffer[ index++ ] = c;
}
// Remember that we just got a character
lastRX = millis();
}
if ((index > 0) && (millis() - lastRX > 50)) {
// We have some chars, and it has been 50ms since the last char was received.
buffer[ index ] = 0; // NUL-terminate the string so we can...
Serial.println( buffer ); // ...write it out as a C string (Serial.write does not need NUL)
index = 0; // ...and start saving a new line.
}
}
Notice how this accumulates chars if they are available, sets the time stamp, and then moves on. If no chars are available, lastRX does not get updated. When no chars have been available for 50ms, it prints the chars that have been accumulated. No blocking, no delays.
To apply this to your program, you should restructure Read3 to do the same thing. Accumulate the characters that are available "now". Then test if it has been 50ms since the last character. If it has, forward the entire buffer to Serial2 and reset the index.
void Read3( int timeout_max )
{
static int index = 0;
static uint32_t lastRX; // time stamp
while (Serial.available()) {
uint8_t c = Serial.read();
in_bufer[ index++ ] = c;
if (index >= BUF_SIZE_IN)
break;
lastRX = millis();
}
// Send the buffer if it is full OR it's time to send what we have
if ((index >= BUF_SIZE_IN) ||
((index > 0) && ((millis() - lastRX) < (unsigned long)timeout_max))) {
WriteDataToSerial2();
index = 0;
}
}
Notice how the while loop is terminated if the buffer is full. The next if statement will write the full buffer, OR write a partial buffer if it has been 50ms.
Apply this same technique to all inputs.
Cheers,
/dev