arduino-robust-serial icon indicating copy to clipboard operation
arduino-robust-serial copied to clipboard

[question] Why is readBytes considered slow?

Open hazkaz opened this issue 5 years ago • 2 comments

Hi @araffin , I came across your library and medium article and found it helpful in writing my own binary protocol for arduino. I was wondering why you considered that readBytes is slow compared to checking if serial.available()>size and then using read().

https://github.com/araffin/arduino-robust-serial/blob/5d83ec5ddbf6990d7a8e090589c6785f12cc92e2/arduino-board/slave.cpp#L157

hazkaz avatar Feb 09 '20 18:02 hazkaz

Hello,

Good question, I think this came from test a colleague did a while ago (already 5 years from now...)

But maybe it has changed or the test is not valid anymore. Feel free to replace that with the readBytes, I would be happy to have a feeback/proper comparison ;).

araffin avatar Feb 09 '20 19:02 araffin

Hi, I looked up the code for upto 2012 and based on what I saw, the code for readBytes and timedRead are the same. What might however cause different behaviour is that readBytes calls timedRead n times for n bytes. Each timedRead makes a call to millis (which could be making the difference here).

Your code however waits for the all the bytes to be available in the serial buffer before reading all of them at once.

I haven't made any measurements on a DSO and can't verify this however. I don't even know if readBytes is infact slower, let alone how much slower per byte(microseconds, milliseconds etc). Let me know if you find out more.

hazkaz avatar Feb 11 '20 17:02 hazkaz