Fix SerialManager not reading the entire message

This commit is contained in:
Lorow
2025-05-10 21:02:30 +02:00
parent 899e00a5fb
commit 06c307a821
2 changed files with 20 additions and 4 deletions

View File

@@ -7,7 +7,6 @@
#define ECHO_UART_PORT_NUM (1)
#define ECHO_UART_BAUD_RATE (115200)
#define ECHO_TASK_STACK_SIZE (3072)
#define BUF_SIZE (1024)
@@ -21,16 +20,32 @@ void SerialManager::setup()
usb_serial_jtag_driver_install(&usb_serial_jtag_config);
// Configure a temporary buffer for the incoming data
this->data = (uint8_t *)malloc(BUF_SIZE);
this->temp_data = (uint8_t *)malloc(256);
}
void SerialManager::try_receive()
{
int len = usb_serial_jtag_read_bytes(this->data, (BUF_SIZE - 1), 10000);
if (len)
int current_position = 0;
int len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
// since we've got something on the serial port
// we gotta keep reading until we've got the whole message
while (len)
{
memcpy(this->data + current_position, this->temp_data, len);
current_position += len;
len = usb_serial_jtag_read_bytes(this->temp_data, 256, 1000 / 20);
}
if (current_position)
{
// once we're done, we can terminate the string and reset the counter
data[current_position] = '\0';
current_position = 0;
auto result = this->commandManager->executeFromJson(std::string_view((const char *)this->data));
auto resultMessage = result.getResult();
usb_serial_jtag_write_bytes(resultMessage.c_str(), resultMessage.length(), 10000);
usb_serial_jtag_write_bytes(resultMessage.c_str(), resultMessage.length(), 1000 / 20);
}
}

View File

@@ -27,6 +27,7 @@ private:
QueueHandle_t serialQueue;
std::shared_ptr<CommandManager> commandManager;
uint8_t *data;
uint8_t *temp_data;
};
void HandleSerialManagerTask(void *pvParameters);