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

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

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.
jm.2
Associate II

Hi ,

Thanks for the reply.

Actually I am using Co2 Sensor, from that Co2 sensor I want to read the sensor data from one UART to another UART. The Co2 sensor is UART communication only. I am connecting Co2 sensor for one UART to receive the sensor data and same sensor data I want to transmit to another UART in stm32 controller. This is the task, can you please suggest some code to read exact data from sensor.

I have used the below code  

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

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

But its showing 0 value.

You're posting the same question and not giving enough information. I've asked what kind of sensor and you saying its a Co2 sensor means nothing. A part number will tell us a whole lot more information.

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.
jm.2
Associate II

hi,

0693W00000WIvUgQAL.pngplease find the attachment of co2 sensor datasheet image.

jm.2
Associate II

HI

I AM WAITING FOR YOUR REPLY.

The reason your reading 0 is because you didn't send a command to read a register. Your HAL_UART_Receive(&huart2,(uint8_t *)BUFFER,sizeof(BUFFER),100) times out in 100ms. You also don't read the HAL return status so you didn't see it return HAL_TIMEOUT. So you may think the STM32 received 10 bytes but it didn't.

Then there are return values are in ASCII and they are not consistent in length so reading 10 bytes is not going to work. HAL_UART_Receive_IT(&huart2,(uint8_t *)BUFFER, 1) is going to work better for you. You need to fill your buffer one character at a time until until you receive a CR and LF.

You need to read the data sheet more thoroughly especially UART COMMAND PROTOCOL section.

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.
jm.2
Associate II

Hi,

Can you please tell me how to implement to fill buffer character.

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.

Hi,

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

jm.2
Associate II

Hi,

Please tell me how to send a command to read a register