0

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;
    }
rusty81
  • 53
  • 3
  • 10
  • Possible duplicate of [sprintf() with automatic memory allocation?](https://stackoverflow.com/questions/3774417/sprintf-with-automatic-memory-allocation) – followed Monica to Codidact Sep 22 '18 at 09:19
  • 1
    Do not use sizeof to determine the length of the string – 0___________ Sep 22 '18 at 09:39
  • all the code is working correctly with a standard gnu C compiler, i think the problem is in the handling of HAL_UART API. I'm starting to think that is better to work without HAL libraries, it is better to do it from scratch? – rusty81 Sep 22 '18 at 10:50
  • @rusty81 uC programming and malloc are not the best friends. Try to avoid as a plaque. – 0___________ Sep 22 '18 at 14:09
  • Thanks @P__J__ , so what would you suggest to solve this HAL_UART transmission? – rusty81 Sep 22 '18 at 14:59
  • Simplify until you get something that works, then start expanding. Since this looks like a modified example - did the original work? Try smaller changes, see which change breaks it. – domen Sep 24 '18 at 12:54
  • Thanks domen, this is written by me starting from simple HAL_UART_Trasmit. Apparently I can't pass nothing by a constant array of char, if I attempt to fill a buffer that is not fixed in length HAL won't take it right. – rusty81 Sep 24 '18 at 17:22
  • SOLVED. I have declared GNUC prototype and used printf. Much easier. – rusty81 Sep 24 '18 at 17:38

0 Answers0