cancel
Showing results for 
Search instead for 
Did you mean: 

Atoi() and HAL_UART

jlecl.1
Associate III

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 ?

4 REPLIES 4

>>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
Up vote any posts that you find helpful, it shows what's working..
jlecl.1
Associate III

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.

ONadr.1
Senior III

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

Atoi

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);