[英]How to pass string/array to function correctly? (stm32)
I'm not first and not last I mean) And it's one more question about waring: assignment makes pointer from integer without a cast .我不是第一个,也不是最后一个,我的意思是)还有一个关于waring的问题:赋值使指针来自 integer 没有强制转换。
I've already read this and this and this and this and this and more and more.我已经读过这个, 这个, 这个,这个, 这个,越来越多。
I have NUCLEO-F070RB-STM32 devkit.我有 NUCLEO-F070RB-STM32 开发套件。 And I want to send different messages on PC for debugging.
我想在 PC 上发送不同的消息进行调试。
I use this construction:我使用这种结构:
uint8_t greeting[] = "Welcome to first circle of hell\n";
HAL_UART_Transmit(&huart2, greeting, sizeof(greeting)-1, 30);
//"sizeof(greeting)-1" cut "\0" from string
Here is a description:这是一个描述:
/**
* @brief Send an amount of data in blocking mode.
* @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
* the sent data is handled as a set of u16. In this case, Size must indicate the number
* of u16 provided through pData.
* @note When UART parity is not enabled (PCE = 0), and Word Length is configured to 9 bits (M1-M0 = 01),
* address of user data buffer containing data to be sent, should be aligned on a half word frontier (16 bits)
* (as sent data will be handled using u16 pointer cast). Depending on compilation chain,
* use of specific alignment compilation directives or pragmas might be required
* to ensure proper alignment for pData.
* @param huart UART handle.
* @param pData Pointer to data buffer (u8 or u16 data elements).
* @param Size Amount of data elements (u8 or u16) to be sent.
* @param Timeout Timeout duration.
* @retval HAL status
*/
And all work perfectly.一切都完美无缺。
But I want different message.但我想要不同的信息。 I'm a good programmer.
我是一个很好的程序员。
uint8_t greeting[] = "Welcome to first circle of hell\n";
uint8_t str[] = "just for UART\r\n";
void send_to_uart (uint8_t);
void send_to_uart (uint8_t it_will_be_send)
{
HAL_UART_Transmit(&huart2, it_will_be_send, sizeof(it_will_be_send)-1, 30);
}
int main(void)
{
send_to_uart(greeting);
wile (1)
{
send_to_uart(str);
}
}
And I have a warning and have no data on my screen.我有一个警告,我的屏幕上没有数据。 Bad bad bad programmer!
坏坏坏程序员!
I understand that devil in pointers.我理解指针中的魔鬼。 But can you explain slowly step by step for all newbies like me: How to pass...n arrays to functions.
但是您能否为像我这样的所有新手逐步解释:如何将...n arrays 传递给函数。 PLEEEAASEEE
讨回公道
PS附言
/* USER CODE BEGIN 0 */
void send_to_uart (uint8_t);
void send_to_uart (uint8_t it_will_be_send)
{
HAL_UART_Transmit(&huart2, it_will_be_send, sizeof(it_will_be_send)-1, 30);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE BEGIN WHILE */
uint8_t greeting[] = "\nWelcome to first circle of hell\n";
uint8_t some_string[] = "some string to UART\r\n";
send_to_uart(greeting);
HAL_UART_Transmit(&huart2, greeting, sizeof(greeting)-1, 30);
while (1)
{
HAL_UART_Transmit(&huart2, "\nHAL some string to UART\n" , 25, 30);
send_to_uart(some_string);
HAL_UART_Transmit(&huart2, some_string, sizeof(some_string)-1, 30);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
make -j4 all
arm-none-eabi-gcc "../Core/Src/main.c" -mcpu=cortex-m0 -std=gnu11 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32F070xB -c -I../Core/Inc -I../Drivers/STM32F0xx_HAL_Driver/Inc -I../Drivers/STM32F0xx_HAL_Driver/Inc/Legacy -I../Drivers/CMSIS/Device/ST/STM32F0xx/Include -I../Drivers/CMSIS/Include -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -MMD -MP -MF"Core/Src/main.d" -MT"Core/Src/main.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/main.o"
../Core/Src/main.c: In function 'send_to_uart':
../Core/Src/main.c:67:29: warning: passing argument 2 of 'HAL_UART_Transmit' makes pointer from integer without a cast [-Wint-conversion]
67 | HAL_UART_Transmit(&huart2, it_will_be_send, sizeof(it_will_be_send)-1, 30);
| ^~~~~~~~~~~~~~~
| |
| uint8_t {aka unsigned char}
In file included from ../Core/Inc/stm32f0xx_hal_conf.h:288,
from ../Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_uart.h:1491:73: note: expected 'uint8_t *' {aka 'unsigned char *'} but argument is of type 'uint8_t' {aka 'unsigned char'}
1491 | HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
| ~~~~~~~~~^~~~~
../Core/Src/main.c: In function 'main':
../Core/Src/main.c:112:16: warning: passing argument 1 of 'send_to_uart' makes integer from pointer without a cast [-Wint-conversion]
112 | send_to_uart(greeting);
| ^~~~~~~~
| |
| uint8_t * {aka unsigned char *}
../Core/Src/main.c:65:28: note: expected 'uint8_t' {aka 'unsigned char'} but argument is of type 'uint8_t *' {aka 'unsigned char *'}
65 | void send_to_uart (uint8_t it_will_be_send)
| ~~~~~~~~^~~~~~~~~~~~~~~
../Core/Src/main.c:117:29: warning: pointer targets in passing argument 2 of 'HAL_UART_Transmit' differ in signedness [-Wpointer-sign]
117 | HAL_UART_Transmit(&huart2, "\nHAL some string to UART\n" , 25, 30);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| char *
In file included from ../Core/Inc/stm32f0xx_hal_conf.h:288,
from ../Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal.h:30,
from ../Core/Inc/main.h:31,
from ../Core/Src/main.c:21:
../Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_uart.h:1491:73: note: expected 'uint8_t *' {aka 'unsigned char *'} but argument is of type 'char *'
1491 | HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, uint8_t *pData, uint16_t Size, uint32_t Timeout);
| ~~~~~~~~~^~~~~
../Core/Src/main.c:118:15: warning: passing argument 1 of 'send_to_uart' makes integer from pointer without a cast [-Wint-conversion]
118 | send_to_uart(some_string);
| ^~~~~~~~~~~
| |
| uint8_t * {aka unsigned char *}
../Core/Src/main.c:65:28: note: expected 'uint8_t' {aka 'unsigned char'} but argument is of type 'uint8_t *' {aka 'unsigned char *'}
65 | void send_to_uart (uint8_t it_will_be_send)
| ~~~~~~~~^~~~~~~~~~~~~~~
arm-none-eabi-gcc -o "max6675_2.0.elf" @"objects.list" -mcpu=cortex-m0 -T"C:\stm32\cubeMX_workspace\max6675_2.0\STM32F070RBTX_FLASH.ld" --specs=nosys.specs -Wl,-Map="max6675_2.0.map" -Wl,--gc-sections -static --specs=nano.specs -mfloat-abi=soft -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
Finished building target: max6675_2.0.elf
arm-none-eabi-objdump -h -S max6675_2.0.elf > "max6675_2.0.list"
arm-none-eabi-objcopy -O binary max6675_2.0.elf "max6675_2.0.bin"
arm-none-eabi-size max6675_2.0.elf
text data bss dec hex filename
8228 20 1804 10052 2744 max6675_2.0.elf
Finished building: default.size.stdout
Finished building: max6675_2.0.bin
Finished building: max6675_2.0.list
13:39:29 Build Finished. 0 errors, 4 warnings. (took 1s.941ms)
Look at this:看这个:
void send_to_uart (uint8_t);
So send_to_uart
expect an argument of type uint8_t
- fine.所以
send_to_uart
期望uint8_t
类型的参数 - 很好。
Now look at this:现在看看这个:
uint8_t greeting[] = "Welcome to first circle of hell\n";
int main(void)
{
send_to_uart(greeting); <--- What is greeting?
So is greeting
a uint8_t
?那么
greeting
uint8_t
吗?
No - it's not.不,这不对。 It's an array so you can't pass it to a function that expects a
uint8_t
这是一个数组,因此您不能将其传递给需要
uint8_t
的 function
Try like:试试看:
for (int i = 0; i < strlen(greeting); ++i) send_to_uart(greeting[i]);
You can use char
type for string literals.您可以将
char
类型用于字符串文字。 Just typecast them in HAL_UART_TRANSMIT
to avoid compiler warnings.只需在
HAL_UART_TRANSMIT
中对它们进行类型转换以避免编译器警告。
char greeting[] = "Welcome to first circle of hell\n";
char str[] = "just for UART\r\n";
void send_to_uart (const char *);
void send_to_uart (const char *it_will_be_send)
{
HAL_UART_Transmit(&huart2, (uint8_t *)it_will_be_send, strlen(it_will_be_send), 30);
}
int main(void)
{
/* Initialize UART */
/* ..... */
send_to_uart(greeting);
while (1)
{
send_to_uart(str);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.