Skip to main content
jlecl.1
Associate III
Jun 6, 2023 8:53 PM UTC
Question

Atoi() and HAL_UART

Hello,

My question is about language C.

My nucleo receive some AT Command (from my pc) and i have write a function for compute the command.

Ex : AT+SPEED? > return "SPEED" (only).

>I 'm able to get only the command (by print over uart).

>My command is store inside a array.

command [0] = S

command [1] = P

...

Now a need to return this "value" from my function to another. One way is to convert this array into a string with atoi() function.

//------------------------------------------

return data =atoi(command);

//------------------------------------------

uint16_t Array[10];

sprintf( Array, "%c",command_value) ;

HAL_UART_Transmit(&huart1, Array, strlen(Array), 100);

it doesn't work, i mean no print.

I think is not a STM32 problem but a C problem.

Someone have an idea ?

This topic has been closed for replies.

4 replies

Tesla DeLorean
Guru
Jun 6, 2023 9:25 PM UTC

>>sprintf( Array, "%c",command_value) ;

command_value is what?

you are printing as a character?

command [0] = 'S'; // as a character

command needs to be NUL terminated

atoi() needs to process a string containing a number, ie "123" not "SPEED"

Consider learning C on a PC platform, then move to embedded.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
jlecl.1
jlecl.1Author
Associate III
Jun 7, 2023 12:38 PM UTC

You have right, my problem is not a bug from STM32 but a C acknowledge issue.

I'm not a complete newbie and i have all documentation next me... the transition from theory to practice is not always easy.

My data is store like that :

uint8_t command[10]={0};

where command[0] = 'S' / command[1] = 'P' / ...

i have write this following lines :

uint8_t data;
data = atoi(command);
 
uint8_t str[10];
sprintf(str, "\r\n %u", data); 
HAL_UART_Transmit(&huart1, (uint8_t*)str, strlen(str), 100);

the result is '0' in my terminal.

Graduate II
Jun 7, 2023 6:03 PM UTC

Hello

If you allready have this answer-string in command-array, you can send it directly:

HAL_UART_Transmit(&huart1, command, strlen(command), 100);

Or if you need to add Line feed /carriage return in the beginning (which is bit odd, usually it is in the end), you can do like this:

 uint8_t str[20];
 sprintf(str, "\r\n %s", command); 
 HAL_UART_Transmit(&huart1, str, strlen(str), 100);

ONadr.1
Senior III
Jun 7, 2023 1:29 PM UTC

atoi() convert string to integer. For example atoi("193") is converted as integer value 193.

Atoi