cancel
Showing results for 
Search instead for 
Did you mean: 

Get one character from the USART

FLamb.2
Associate II

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

9 REPLIES 9
Peter BENSCH
ST Employee

The USART would receive characters serially, i.e. one after the other.

What do you mean with position x?

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
FLamb.2
Associate II

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.

TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".
FLamb.2
Associate II

How can I do it?

char getchar() {
	char data[1];
	HAL_UART_Receive(&huart2, (void*)data, 1, HAL_MAX_DELAY);
	return data[0];
}

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
FLamb.2
Associate II

@Community member​ 

@Pavel A.​ has the right solution?

Depends.

The polling/blocking solution falls over if you want concurrent full-duplex or multi-UART functionality. ​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
FLamb.2
Associate II

This solution doesn't work. I get strange characters. See attachement