Skip to main content
jm.2
Associate III
November 18, 2022
Question

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

  • November 18, 2022
  • 7 replies
  • 15845 views

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.

This topic has been closed for replies.

7 replies

AScha.3
Super User
November 18, 2022

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""."
jm.2
jm.2Author
Associate III
November 18, 2022

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
Super User
November 18, 2022

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""."
jm.2
jm.2Author
Associate III
November 18, 2022

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.

AScha.3
Super User
November 18, 2022

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
November 18, 2022

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?

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
jm.2
jm.2Author
Associate III
November 20, 2022

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.

Karl Yamashita
Lead III
November 20, 2022

What sensor are you talking about? What makes the sensor send 10 bytes? You need to give us more information

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Bob S
Super User
November 18, 2022

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.

thundertronics.com
Visitor II
November 21, 2022

Enable interrupts for both USART ports in STM32 Cube configurator (put checkboxes on usart1/2/.. global interrupt/etc. on relevant tabs). HAL driver algorithms use interrupts.

jm.2
jm.2Author
Associate III
November 21, 2022

Hi,

I have enabled both side interrupts, still now I am getting 0 values only.

S.Ma
Principal
November 21, 2022

First test your sensor with bluetooth HC-05 or similar from a pc.

If this passes, implement a sw fifo tx and rx between both uart to be clean. Use byte uart interrupt. Gas sensor are usually slow response time, high bitrate probably not needed.