diff --git a/src/drivers/distance_sensor/lightware_grf_serial/lightware_grf_serial.cpp b/src/drivers/distance_sensor/lightware_grf_serial/lightware_grf_serial.cpp index bb88f1fac5d..82cabca08ca 100755 --- a/src/drivers/distance_sensor/lightware_grf_serial/lightware_grf_serial.cpp +++ b/src/drivers/distance_sensor/lightware_grf_serial/lightware_grf_serial.cpp @@ -345,7 +345,6 @@ 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; @@ -456,8 +455,6 @@ 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 { @@ -477,11 +474,11 @@ void GRFLaserSerial::grf_get_and_handle_request(const int payload_length, const 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; + // 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; } // The buffer is filled. Either we can't keep up with the stream and/or it contains only invalid data. Reset to try again. diff --git a/src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.cpp b/src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.cpp index fd2b9b5fde2..561d2bc5554 100755 --- a/src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.cpp +++ b/src/drivers/distance_sensor/lightware_sf45_serial/lightware_sf45_serial.cpp @@ -341,7 +341,6 @@ 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; @@ -454,8 +453,6 @@ 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 { @@ -475,11 +472,11 @@ void SF45LaserSerial::sf45_get_and_handle_request(const int payload_length, cons 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; + // 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; } // The buffer is filled. Either we can't keep up with the stream and/or it contains only invalid data. Reset to try again.