FastLED 3.9.15
Loading...
Searching...
No Matches

◆ readBytes()

size_t fl::SerialPort::readBytes ( u8 * buffer,
size_t length )

Read fixed number of bytes into buffer.

Parameters
bufferPointer to destination buffer
lengthNumber of bytes to read
Returns
Number of bytes actually read

Reads up to length bytes from serial. May return fewer bytes if timeout occurs.

Definition at line 179 of file serial.cpp.hpp.

179 {
180 if (!buffer || length == 0) {
181 return 0;
182 }
183
184 size_t count = 0;
185 u32 startTime = fl::millis();
186
187 while (count < length && (fl::millis() - startTime < mTimeoutMs)) {
188 if (available() > 0) {
189 int c = read();
190 if (c != -1) {
191 buffer[count++] = static_cast<u8>(c);
192 startTime = fl::millis(); // Reset timeout on successful read
193 }
194 }
195 }
196
197 return count;
198}
int available()
Check how many bytes are available to read.
int read()
Read next byte from serial input.
fl::UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
unsigned char u8
Definition stdint.h:131
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.

References available(), length(), fl::millis(), mTimeoutMs, and read().

+ Here is the call graph for this function: