Seiral1 buffer question - advice needed

I’m new to the community and this is my first project. I’m attempting to read data from a serial port and so far I’ve been able to get pretty far. I’ve run into a slight issue with the serial buffer and I’m looking for some recommendations.

My serial connection runs at 19200. I send a short message and receive back 99 bytes of data. The serial buffer is only 64 bytes in size. At the current connection speed I’m receiving approx 2.4 bytes a millisecond. Since I’m only able to read from the buffer 1 byte at a time, I’m having issues getting all 99 bytes of data before the buffer fills up.

I’ve done some reading on others that have had this issue and they seem to say that you need to slow down the script. In my case, I think I need to speed up the scrip before the buffer runs out. Do I have that correct?

I have an option to slow down the serial protocol so the bytes don’t come into the buffer so quick. Would this be the best solution or is there something I’m missing?

I would structure the code so that in loop you read and buffer all of the available serial bytes before returning from loop. The problem is that loop doesn’t get called frequently enough to read one byte per loop iteration, but it will work fine if you empty the buffer each loop iteration.

@rickkas7, I currently read all of the buffer in a while loop but that doesn’t seem to get it all.

while (Serial1.available())
{
readByte = Serial1.read();
inByte[count] = readByte;
count++;
}

I don’t send another command to the serial until I’ve gotten through this loop.

Under normal circumstances you’ll see 1 iteration of loop() per millisecond and that while() will empty the buffer (which I think is only 32byte) in way less than 250µs which would give you enough slack, so I guess your code might have other places where you lose time.

Also when sending 99byte @19200, I’d assume that each iteration of loop() will only ever give you a few byte of the full message.

You could also try SYSTEM_THREAD(ENABLED) to decouple your code from the cloud connection.

@ScruffR, Thank you for the reply. I was not aware on how fast the while loop can be. This explains why I might need to slow it down a bit. I had a delay of 3 milliseconds added in the while loop but it didn’t seem to make a difference. I’ll tweak this a bit and see if I can get a different result.

I’m receiving the 99bytes @ 19200 and if my calculations are correct should fill up the buffer (64 bytes) in about 28 milliseconds.

I guess you didn’t actually mean 64kb :wink:

I’d always try to find ways to keep the maximum speed possible and not slow things down artificially.
Depending in your data, you might be able to build up your message bit by bit always keeping your buffer “dry”.
Just collect a few byte at a time, appending new data to a partial message till you finally have all your data collected and start over again.

@ScruffR Sorry I meant 64 bytes not bits. I’m assuming that is what you’re referencing?

I’ll play with this tonight and report back. Thanks again for the help.

Another option is to continue the DO While loop until I see something in the buffer (end marker). I might try that as well.

Found here: Serial Port receiving buffer size

Actually I got mostly puzzled about the k in 64kb :wink:

Wow…yeah…thats not correct at all. Sorry for the confusion.