cancel
Showing results for 
Search instead for 
Did you mean: 

I am having difficulty receiving data on USART2. It is receiving one character when I use HAL_UART_Receive_IT interrupts. I am trying to receive some characters from ADUCM360 USART.

cjaya.1
Associate II

I can see transmitting characters from ADUCM360 to STM32.But it does not capture the incoming characters from STM32 using interrupts. Any help on this very much appreciated. Thank you

18 REPLIES 18
  • What are the strings that you are trying to receive?
  • I see you are looking for the '\n' character but how do you know you did?
  • What do you do with your Receive[] buffer before you receive another string of characters?

> Can you see any problem with this code?

At least call HAL_UART_IRQHandler(&huart2); before HAL_UART_Receive_IT()

Using HAL_UART_Receive_IT to receive one byte is a problem because it disables interrupt after each received byte and you can lose interrupts.

Using HAL_UART... functions at all is a problem.

I need to receive 19 strings . Each string ends with '\n' character .

I know that I receive the string when I go into the debugging stage and I can see the Receive buffer is filled.

Before I receive another string of characters, I displayed on the API.

Are the 19 strings sent one after the other without any pause? Or are you waiting to send the next set of strings after you display what is currently in the receive buffer?

Also, your variable "ch" already holds the incoming character. Doing "ch = USART->DR" is not needed.

Use the HAL_USART drivers as it does everything for you. You don't have to check for flags or clear flags, it's done in the driver.

Use the HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) callback. If that is called then there are no errors. All you have to do is read the character from the "ch" variable from that callback and save to your Receive[] buffer.

The 19 strings question I asked in the previous reply still stands?

the 19 strings sent one after the other without any pause. So I have to capture those 19 strings.

AScha.3
Chief II

HAL_UART_Receive_IT(&huart2, (uint8_t*)ch, 1); -- need pointer here ->

HAL_UART_Receive_IT(&huart2, &ch, 1);

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

Thank you for all of your replies. after increasing the buffer size. I am able to receive 19 strings. But the 19 strings I received is static. It needs to be change every time i receive. Every time I receive it should be different set of data. Is something I need to reset the buffer everytime I receive and the buffer pointer. Any thoughts about this.

Ok, so your Receive[] buffer is done incorrectly. You actually need a ring buffer to hold a bunch of characters which requires a In pointer and Out pointer. The ring buffer should hold at least the largest string size plus 15% more characters. But if you have a lot of background stuff going on then you may need to increase the size even more.

I have a project that does that but it takes in a text file of hundreds of lines of text, 80 characters for each line of text one after the other with no pause. Actually someone posted on this forum wanting to read a text file and look for the word "PLLR1". I already had a project that i was working on, so I added few lines of code to accomplish what they wanted. So i have buffer of 90 characters so that an Rx character can't overwrite a previous line of text before i extract it and save it to a message buffer which i can hold up to 4 messages (lines of text). I check if the Message buffer contains a certain string and act upon that. With those two buffers working together, you'll never miss a Rx character. Because receiving a UART character is slow compared to the many instruction cycles of the MCU, the MCU can already extract the string up to the '\n' character and save it to a message buffer before that new character is saved.

See my project here.

https://github.com/karlyamashita/STM32ParseTextFile