0

I'm currently working on a distance sensor with stm32f103cbt6 which transmits data serially using UART. I used HAL_UART_Receive function to receive the data but I cannot see any data on Serial Monitor. Moreover, the sensor works fine with Arduino.

This is the code snippet of the uart receive function.

  while (1)
  {
  HAL_UART_Receive(&huart1, (uint8_t*)RxBuff, strlen(RxBuff), HAL_MAX_DELAY);
  sprintf(txBuffer, "distance: %s\n", RxBuff);
  HAL_UART_Transmit(&huart1, (uint8_t *)txBuffer, strlen(txBuffer), HAL_MAX_DELAY);
  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  HAL_Delay(100);
}

I also tried changing the data types of variables but nothing happened.

char txBuffer[txBuff];
char RxBuff[RxBuff_size];
uint8_t distance[1024];

Any help or guidance to this issue would be highly appreciated. Thanks in advance.

artless noise
  • 21,212
  • 6
  • 68
  • 105
Maip N.
  • 1
  • 2
  • You can't use `strlen(RxBuff)` in `HAL_UART_Receive()` to get the size of the buffer, if it starts off empty. You should use `RxBuff_size` or `sizeof RxBuff`. – pmacfarlane Jun 19 '23 at 16:03
  • Also note that `HAL_UART_Receive()` will not return until it has completely filled your receive buffer. This may (or may not) be what you want. If it's not, you'll probably have to read the bytes one at a time until you have "enough", e.g. you receive a newline, or whatever your criteria is. – pmacfarlane Jun 19 '23 at 16:07
  • I [think] I found a workaround [hack] for the blocking issue. See my answer: [How to connect Quectel L89H module to STM32WL55JC1 and get the data Via UART?](https://stackoverflow.com/a/76417652/5382650) – Craig Estey Jun 19 '23 at 16:11
  • You start immediately with the receive function. How does the sensor know it's supposed to send something? Does it send stuff automatically at some rate? Or do you need to send it command first for it to reply? Does transmit function work? (This question would verify UART peripheral is working) – Ilya Jun 20 '23 at 09:17
  • @Ilya sensor does not need any triggering for sending data it is already calibrated, so that after receiving power it starts sending data – Maip N. Jun 23 '23 at 15:14
  • Are you sure that the code before the pieces you posted does everything that needs to be done? – Ilya Jun 23 '23 at 16:30
  • Wait a sec. You're supposed to receive stuff from the IC and then resend it to PC? Are your UART1 TX and RX connected to chip and PC at the same time? RX and TX to different devices? Because if not, you need to send to PC with a different UART, otherwise you just write things to the sensor, not to PC – Ilya Jun 23 '23 at 16:37
  • I just connect the rx pin of stm32 to sensor and tx of stm to pc and which I don't think creates any problem. – Maip N. Jun 24 '23 at 12:25
  • Also my issue is the code only if you think any changes should be made please let me know. – Maip N. Jun 24 '23 at 12:25

0 Answers0