Skip to main content
FLamb.2
Associate II
January 11, 2021
Question

Get one character from the USART

  • January 11, 2021
  • 9 replies
  • 2460 views

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

This topic has been closed for replies.

9 replies

Peter BENSCH
ST Technical Moderator
January 11, 2021

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
FLamb.2Author
Associate II
January 11, 2021

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
January 11, 2021

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
FLamb.2Author
Associate II
January 11, 2021

How can I do it?

Pavel A.
January 11, 2021
char getchar() {
	char data[1];
	HAL_UART_Receive(&huart2, (void*)data, 1, HAL_MAX_DELAY);
	return data[0];
}

Tesla DeLorean
Guru
January 11, 2021

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
FLamb.2
FLamb.2Author
Associate II
January 12, 2021

@Community member​ 

@Pavel A.​ has the right solution?

Tesla DeLorean
Guru
January 12, 2021

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
FLamb.2
FLamb.2Author
Associate II
January 24, 2021

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