Skip to content

Instantly share code, notes, and snippets.

@DanGdl
Created November 9, 2023 16:11
Show Gist options
  • Save DanGdl/33eb4f981c6a21663f59a544503b52bc to your computer and use it in GitHub Desktop.
Save DanGdl/33eb4f981c6a21663f59a544503b52bc to your computer and use it in GitHub Desktop.
receive from console
void get_console_message(char* const buffer, unsigned int len_max) {
uint32_t count_symbols = 0;
printf("Please enter a message (max size 1024 symbols):\r\n");
while (1) {
uint8_t c;
HAL_StatusTypeDef status = HAL_UART_Receive(&huart3, &c, 1, HAL_MAX_DELAY);
if (c == '\r') {
HAL_UART_Transmit(&huart3, &c, 1, HAL_MAX_DELAY);
c = '\n';
HAL_UART_Transmit(&huart3, &c, 1, HAL_MAX_DELAY);
break;
} else {
HAL_UART_Transmit(&huart3, &c, 1, HAL_MAX_DELAY);
}
if (status == HAL_OK) {
buffer[count_symbols] = c;
count_symbols++;
if (count_symbols == (len_max - 1)) {
buffer[len_max] = '\0';
break;
}
}
else {
printf("Failed to receive data via UART3: %d, error 0x%"PRIX32"\r\n", status, HAL_UART_GetError(&huart3));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment