2021-01-11 03:36 AM
I want to get one character at position x from the USART.
Here is my current code:
What do I have to do differently?
char *getchar(int position){
char data[1];
data[0]=HAL_UART_Receive_IT(&huart2, data, 1);
return data;
}
2021-01-11 03:55 AM
The USART would receive characters serially, i.e. one after the other.
What do you mean with position x?
/Peter
2021-01-11 05:28 AM
I mean, that the first character of the USART is at position 1, the second character is at position 2 etc.
If on the USART is the text "abc" displayed then a is on position 1.
2021-01-11 05:56 AM
You have to receive characters as they come in, one by one. You can't skip ahead without losing information.
If you want, you could read them into a buffer and then use that buffer to read out values in whatever position you like.
2021-01-11 06:00 AM
How can I do it?
2021-01-11 12:19 PM
char getchar() {
char data[1];
HAL_UART_Receive(&huart2, (void*)data, 1, HAL_MAX_DELAY);
return data[0];
}
2021-01-11 12:44 PM
The interrupt version completes with a callback, returning immediately, but not with data.
You can't use a local/auto variable to store the character in this fashion.
You've either got to do some buffer management via the interrupt/callback, or use the non-IT version which blocks, and might miss things if you aren't listening at the right time.
2021-01-11 07:07 PM
@Community member
@Pavel A. has the right solution?
2021-01-11 11:06 PM
Depends.
The polling/blocking solution falls over if you want concurrent full-duplex or multi-UART functionality.
2021-01-23 05:07 PM