I'm trying to send a variable length string via UART, using HAL function. There is no way to send a string that is changing its length runtime, I have tried with various declarations, inside and outside while loop, but if I don't declare a fix length string (char buffer[30] for example), HAL is not taking it. char buffer; char buffer[] = "", even char buffer = malloc(sizeof(char)), nothing is working. I have on terminal only some character or nothing.
Is there a way to pass a string that is variable in length to HAL?
Thanks.
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
uint32_t index1 = 0, index2 = 0;
const char message[] = "Hello from Nucleo64";
const char message2[] = "Pressed!";
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)){
char *buffer;
sprintf(buffer, "%s - index=%d\n", message, index1);
HAL_UART_Transmit(&huart2, (uint8_t *)buffer, sizeof(buffer)-1, 10);
index1 += 1;
} else
{
char *buffer2;
sprintf(buffer2, "%s - index=%d\n", message2, index2);
HAL_UART_Transmit(&huart2, (uint8_t *)buffer2, sizeof(buffer2)-1, 10);
index2 += 1;
}
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
HAL_Delay(500);
}
/* USER CODE END 3 */
}
As suggested, I tryed also snprintf, on the terminal I see nothing but [00] when I reset, then nothing is transmitted.
uint32_t index1 = 0, index2 = 0;
char message[] = "Hello from Nucleo64";
char message2[] = "Pressed!";
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin)){
size_t needed = snprintf(NULL, 0, "%s - index=%d\n", message, index1) + 1;
char *buffer = malloc(needed);
snprintf(buffer, needed, "%s - index=%d\n", message, index1);
HAL_UART_Transmit(&huart2, (uint8_t *)buffer, strlen(buffer), 10);
index1 += 1;
free(buffer);
} else
{
size_t needed = snprintf(NULL, 0, "%s - index=%d\n", message2, index2) + 1;
char *buffer2 = malloc(needed);
snprintf(buffer2, needed, "%s - index=%d\n", message2, index2);
HAL_UART_Transmit(&huart2, (uint8_t *)buffer2, strlen(buffer2), 10);
index2 += 1;
free(buffer2);
}
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
HAL_Delay(500);
}
/* USER CODE END 3 */
}
SOLVED *****************************
Used printf after declaring prorotype GNUC
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
and passing it to HAL_UART_Trasmit
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}