cancel
Showing results for 
Search instead for 
Did you mean: 

UART receive garbage data, though it looks ok on oscilloscope

Ashraf_minime
Visitor

I am using stm32f411 MCU to receive data over UART.

The MCU acts as a bridge between two devices. PC & Meter device. both uses UART, but the physical connection is different. 

the connection between the MCU & meter is current mode communication.

Speed is 9600 bps.

I tested it with 2400 & 4800 & both were fine.

When I tested it with 9600, the signal on the receive pin of the stm32 is ok. There are some garbage character in between two messages, but it works fine on 2400 & 4800 baud-rate.

 

The code is very simple, receive data from meter & forward it to pc & vice versa. It does that when a complete message is received.
I added a delay to start receiving only after the sending and there is a small character appears when the MCU & the meter switch the ownership of the current mode bus. The meter reply come after 5ms of switching occurs.

 

I attached snapshot of what is received on the RX pin
The debug data
& snippet from the code for the UART.

void MX_USART2_UART_Init(void)
{

  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = 9600;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_EVEN;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */
}


///// on the bottom
else if (uartHandle->Instance == USART2)
  {
    /* USER CODE BEGIN USART2_MspInit 0 */

    /* USER CODE END USART2_MspInit 0 */
    /* USART2 clock enable */
    __HAL_RCC_USART2_CLK_ENABLE();

    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**USART2 GPIO Configuration
    PA2     ------> USART2_TX
    PA3     ------> USART2_RX
    */
    GPIO_InitStruct.Pin = USART2_TX_Pin | USART2_RX_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    /* USART2 interrupt Init */
    HAL_NVIC_SetPriority(USART2_IRQn, 10, 0);
    HAL_NVIC_EnableIRQ(USART2_IRQn);
    /* USER CODE BEGIN USART2_MspInit 1 */

    /* USER CODE END USART2_MspInit 1 */
  }
HAL_UARTEx_ReceiveToIdle(&huart6, &received_from_pc, sizeof(received_from_pc), &received_from_pc_len, 10000);
    if (received_from_pc_len > 0)
    {
      /*--------------------------------------------------------------------------------*/
      // HAL_Delay(500);
      HAL_GPIO_WritePin(TX_ENABLE_GPIO_Port, TX_ENABLE_Pin, GPIO_PIN_RESET);
      HAL_Delay(25);
      HAL_UART_Transmit(&huart2, received_from_pc, received_from_pc_len, 3000); // for now.
      HAL_Delay(10);
      HAL_GPIO_WritePin(TX_ENABLE_GPIO_Port, TX_ENABLE_Pin, GPIO_PIN_SET);

      /*--------------------------------------------------------------------------------*/
      // discard garbage invalid characters until we receive a valid start of frame character (0x15, 0x06 or 0x02) due to communication problems
      uint16_t total_rec = 0;
      do
      {
        HAL_UARTEx_ReceiveToIdle(&huart2, &received_from_meter, sizeof(received_from_meter), &received_from_meter_len, 20);
        total_rec += received_from_meter_len;
      } while (received_from_meter_len > 0 && (received_from_meter[0] != 0x15 && received_from_meter[0] != 0x06 && received_from_meter[0] != 0x02 && received_from_meter[0] != '/'));

      if (total_rec > 0)
      {
        HAL_UART_Transmit(&huart6, &received_from_meter, total_rec - 1, 3000);
      }
    }
  }

SDS00013.jpg

 

Receive Data buffer

0xf
0x73
0x7e
0x5b
0x7b
0x6e
0xc
0xc
0 REPLIES 0