cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive and transmit one UART data to another UART in STM32f051c8t6tr

jm.2
Associate II

Hi,

I am working on stm32f051c8t6tr microcontroller, from this I want to read some sensor data with UART1 and same data I want to transmit to another UART. Can you please suggest some example code related to my task. I am new to this controller.

I have tried one code also but its not working, I will share the code in below

HAL_UART_Receive(&huart1,(uint8_t *)buffer,10,10);

HAL_UART_Transmit(&huart2,(uint8_t *)buffer,10,10);

The above two line of code only I tried, buts its not giving any output; Please help me on this.

20 REPLIES 20
AScha.3
Chief III

try:

uint8_t buffer[5] ;

 while (1)

 {

if(HAL_OK == HAL_UART_Receive(&huart1,buffer,1,10))

HAL_UART_Transmit(&huart2,buffer,1,10);

}

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

Hi,

Thanks for the reply,.

I tried this method now, but still I am not getting any result. please help me on this. how to solve this issue, bcz its little emergency work

AScha.3
Chief III

serial settings ok ? speed, 8n1 ...

look in debug : is there anything received ? error on uart ? what show the uart registers (in SFR viewer) ?

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

Yeah all settings are ok only. In debug nothing is running.

For me If I use your code, nothing is showing

uint8_t buffer[5] ;

 while (1)

 {

if(HAL_OK == HAL_UART_Receive(&huart1,buffer,1,10))

HAL_UART_Transmit(&huart2,buffer,1,10);

}

and If I use my HAL_UART_Receive(&huart1,(uint8_t *)buffer,10,10);

HAL_UART_Transmit(&huart2,(uint8_t *)buffer,10,10)

two line code means, its reading 0 value only.

Can you please send me is there any other code or alternate way of code. please solve this issue.

AScha.3
Chief III

did you...?

> serial settings ok ? speed, 8n1 ...

> look in debug : is there anything received ? error on uart ?

> what show the uart registers (in SFR viewer) ?

+ check with scope on input-pin, is there signal?

+ your circuit, hardware ..proto board ?

If you feel a post has answered your question, please click "Accept as Solution".
Karl Yamashita
Lead III

You weren't clear if you received sensor data on UART1 and now you're trying to transmit the same data on UART2 but it's failing?

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.
Bob S
Principal

What @Community member​ said. First verify that you can receive data from the sensor. And @AScha.3​ is this your own custom board (I don't see a NUCLEO with an F051 on it). Is the sensor UART using 3.3V levels? How to you have your F051 connected to the sensor (which pins connected to which pins of what sensor)?

uint8_t buffer[5] ;
while (1)
{
  if ( HAL_OK == HAL_UART_Receive(&huart1,buffer,1,10) )
     HAL_UART_Transmit(&huart2,buffer,1,10);
}

That will not work. If the sensor sends another byte to UART1 while you are sending the previous byte to UART2 you will miss that byte. I would expect the sensor to send one entire multi-byte response with no gaps between bytes. If you need to receive on one port and at the same time transmit that data another port you need to use interrupts or DMA (perhaps https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx).

If the sensor data is fixed length and there a delay between sending each "packet", then you might get away with reading HAL_UART_Receive() to receive the entire packet, the HAL_UART_Transmit() to send it out the other port. But only if you can guarantee the TX will complete before the next sensor data arrives.

Hi,

Thanks for the reply..

 uint8_t BUFFER[10];

 HAL_UART_Receive(&huart2,(uint8_t *)BUFFER,sizeof(BUFFER),100);

 if(HAL_UART_Transmit(&huart1,(uint8_t *)BUFFER,sizeof(BUFFER),100)!=HAL_OK);

If I using the above code means, the sensor reading 0 value only, i want to read data's. Is there anything I want to add. please help me.