2022-11-18 01:16 AM
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.
2022-11-18 02:04 AM
try:
uint8_t buffer[5] ;
while (1)
{
if(HAL_OK == HAL_UART_Receive(&huart1,buffer,1,10))
HAL_UART_Transmit(&huart2,buffer,1,10);
}
2022-11-18 02:11 AM
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
2022-11-18 02:27 AM
serial settings ok ? speed, 8n1 ...
look in debug : is there anything received ? error on uart ? what show the uart registers (in SFR viewer) ?
2022-11-18 02:33 AM
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.
2022-11-18 02:34 AM
Can you please send me is there any other code or alternate way of code. please solve this issue.
2022-11-18 07:17 AM
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 ?
2022-11-18 07:55 AM
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?
2022-11-18 08:33 AM
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.
2022-11-19 10:25 PM
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.