What does _serial_.bufferUntil(byte) do, and how does it synergies with serialEvent?

Viewed 222

I am having trouble understanding this. Upon searching I found

Sets a specific byte to buffer until before calling serialEvent()

This is from this link from the Processing Website

Serial event is the function, that the user states by putting in the Serial port defined as the function, if I’m not mistaken.

But I have seen bufferUntil(‘\n’) when nothing’s being sent to the Serial, so what is this doing, and what does it mean before calling serialEvent() this is put in setup how could it be called each time before a function? And I have also seen arguments like lf, so what is happening here, and how does it synergies with that serialEvent() function?

Thanks for the help, cheers!

1 Answers

With bufferUntil(lf) you set up your serial port to listen (write data to its buffer) until it gets a certain character (lf, which in the example you linked is the line feed character).

As you've noticed bufferUntil(lf) won't actually read any data. To read the data the port received you need to define an interrupt function where you call readString:

void serialEvent(Serial port)
   { inString = port.readString(); } 

This function will be called (interrupting the normal flow of your program and hence its name) automatically as soon as the serial port receives the character you defined with bufferUntil(lf); that'll be until the line feed character for the example. After reading data from the port's buffer your program will return to wherever it was interrupted.

EDIT: What is a buffer? The buffer is either a software (a variable hidden in the library that you're using) or hardware (a bank of memory on the serial port chip) place where you store data coming to the port (this one is the reception buffer, but there is also a transmission buffer for the info you send out through the port).

Think of it as a bucket for bits or bytes. In an analogy with water flow coming out of the tap, you can open your tap and place a glass under it if you want to have a glass of water. But it might be that you want to drink your water later, so you can place a bucket (water buffer) to store water for you. In this case, the bufferUntil(lf) statement would be the action of placing the bucket and you can think of the serialEvent as the action of taking water from the bucket (the fact that you are using the bucket allows you to keep doing your errands around the house but at some point the bucket will overflow unless you either close the tap or start emptying it, and to do that you have to interrupt your normal flow of activities).

Why do we need buffers? Well, you could be polling (listening on the port from the main task of your software) continuously but then your code would be very inefficient. With buffers you're allowed to do other things (calculating stuff, reading data from sensors or whatever) and you'll only check the port when you're sure (because your routine was interrupted) when the data you want is there. In this case, the data you want is indicated with the character you used as an argument in the bufferUntil(lf) function.

I hope I did not overstretch the analogies.

Related