2015-10-21 09:36 AM
I tested the HAL function inside of main using something like this:
uint8_t Heading[]=''\n\r Send some text\n\r''; if(HAL_UART_Transmit(&UartHandle, (uint8_t *)Heading, sizeof Heading -1, 0xFFFF) != HAL_OK) { Error_Handler(); }This implementation worked great.However, I wanted to use the function call within a secondary function so I didn't have to write out the whole thing over and over throughout the code. So I created this:void Usart_Send_String (uint8_t *str){ if(HAL_UART_Transmit(&UartHandle, str, sizeof str -1, 0xFFFF) != HAL_OK) { Error_Handler(Err_Tx); }} /* end of function Usart_Send_String */When I use the following, it stops working: uint8_t Heading[]=''\n\r Send some text\n\r'';Usart_Send_String(Heading);I've tried changing the pointers and such but not much luck. Am I missing something? There isn't much documentation to the use of the HAL.I'm used to the old STDFWLIB and USART_SendData used to work on prior systems.2015-10-21 10:01 AM
I've tried changing the pointers and such but not much luck. Am I missing something? There isn't much documentation to the use of the HAL.
What do you think sizeof str does? How many bytes does a 32-bit pointer use? For a string use strlen(), this is C stuff.
2015-10-21 11:55 AM
I reviewed the example again.
I saw that COUNTOF(aTxBuffer) - 1 was how the size was calculated in the example where COUNTOF is defined as #define COUNTOF(__BUFFER__) (sizeof(__BUFFER__) / sizeof(*(__BUFFER__)))So it essentially does calculate the overall buffer length using a macro. Odd that ''sizeof'' worked fine outside of the function though, not within. Using COUNTOF within the function didn't work when I retried it within the function but, typing in the actual length of the string manually did. So now I know where my issue is occurring.strlen() requires a const char. The HAL requires a length as an unsigned short integer. This would require a cast to make this work.2015-10-21 12:49 PM
sizeof() is a compile time operation, with a one-to-one measurement of a specific object. You can't apply it to random parameters passed to a subroutine, with data from multiple sources. You could pass the size as a second parameter, ie foo(bar, sizeof(bar));
Yes, if you have different types you might have to cast them, strlen() specifies a const type because it only reads the characters, it doesn't not change anything. In the function sizeof(str) is going to tell you how big an (char *) pointer is, as that's the type it's being used on.2015-10-23 06:49 AM
Yes. That explains it. Thank you sir.