fix(lightware): consume exactly the parsed frame (#29167)

The GRF and SF45 parsers trimmed the buffer one byte past each frame, cutting the next frame's start byte, and not at all when the frame ended the buffer. Half the frames were lost and, for GRF at its default rate, each kept reading was published twice, the second time 200 ms stale with a fresh timestamp.

Co-authored-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
trestonandJacob Dahl authored and GitHub committed 2026-10-13 10:08:10 -06:00
1 parent e7445c4acf
commit 1341c655e7
2 files changed
+16 -10

No files matched your search

@@ -345,6 +345,7 @@ void GRFLaserSerial::grf_get_and_handle_request(const int payload_length, const
}
int index = 0;
int frame_end = 0;
while (index <= _linebuf_size - payload_length && _crc_valid == false) {
bool restart_flag = false;
@@ -455,6 +456,8 @@ void GRFLaserSerial::grf_get_and_handle_request(const int payload_length, const
_parsed_state = GRF_PARSED_STATE::START;
_calc_crc = 0;
restart_flag = true;
// the whole frame: start byte, two flag bytes, data_len payload bytes (id included), two crc bytes
frame_end = index + rx_field.data_len + 5;
break;
} else {
@@ -474,11 +477,11 @@ void GRFLaserSerial::grf_get_and_handle_request(const int payload_length, const
index++;
}
// If we parsed successfully, remove the parsed part from the buffer if it is still large enough
if (_crc_valid && index + payload_length < _linebuf_size) {
unsigned next_after_index = index + payload_length;
memmove(&_linebuf[0], &_linebuf[next_after_index], _linebuf_size - next_after_index);
_linebuf_size -= next_after_index;
// Remove the parsed frame from the buffer, also when it ends the buffer, so it is not parsed and
// published again on the next poll and the start byte of the next frame is kept
if (_crc_valid && frame_end > 0 && frame_end <= _linebuf_size) {
memmove(&_linebuf[0], &_linebuf[frame_end], _linebuf_size - frame_end);
_linebuf_size -= frame_end;
}
// The buffer is filled. Either we can't keep up with the stream and/or it contains only invalid data. Reset to try again.
@@ -341,6 +341,7 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons
}
int index = 0;
int frame_end = 0;
while (index <= _linebuf_size - payload_length && _crc_valid == false) {
bool restart_flag = false;
@@ -453,6 +454,8 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons
_parsed_state = SF45_PARSED_STATE::START;
_calc_crc = 0;
restart_flag = true;
// the whole frame: start byte, two flag bytes, data_len payload bytes (id included), two crc bytes
frame_end = index + rx_field.data_len + 5;
break;
} else {
@@ -472,11 +475,11 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons
index++;
}
// If we parsed successfully, remove the parsed part from the buffer if it is still large enough
if (_crc_valid && index + payload_length < _linebuf_size) {
unsigned next_after_index = index + payload_length;
memmove(&_linebuf[0], &_linebuf[next_after_index], _linebuf_size - next_after_index);
_linebuf_size -= next_after_index;
// Remove the parsed frame from the buffer, also when it ends the buffer, so it is not parsed and
// published again on the next poll and the start byte of the next frame is kept
if (_crc_valid && frame_end > 0 && frame_end <= _linebuf_size) {
memmove(&_linebuf[0], &_linebuf[frame_end], _linebuf_size - frame_end);
_linebuf_size -= frame_end;
}
// The buffer is filled. Either we can't keep up with the stream and/or it contains only invalid data. Reset to try again.