FastLED 3.10.8
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 176 of file serial.cpp.hpp.

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

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

+ Here is the call graph for this function: