0
\$\begingroup\$

I am working with Modbus communication using an external device (Aster TDS Meter) connected via an RS485 module. The Modbus settings for the external device are: • Baud rate: 9600 • Data bits: 8 • Parity: None • Stop bits: 1

The communication works perfectly when I use UART4, but when I switch to UART2, I do not receive any data. Both UART configurations are as follows:

UART4 Configuration:

static void MX_USART4_UART_Init(void) {
    huart4.Instance = USART4;
    huart4.Init.BaudRate = 9600;
    huart4.Init.WordLength = UART_WORDLENGTH_8B;
    huart4.Init.StopBits = UART_STOPBITS_1;
    huart4.Init.Parity = UART_PARITY_NONE;
    huart4.Init.Mode = UART_MODE_TX_RX;
    huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart4.Init.OverSampling = UART_OVERSAMPLING_16;
    huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
    huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart4) != HAL_OK) {
        Error_Handler();
    }
}

This configuration works fine with the RS485 module connected to UART4.

UART2 Configuration:

static void MX_USART2_UART_Init(void) {
    huart2.Instance = USART2;
    huart2.Init.BaudRate = 9600;
    huart2.Init.WordLength = UART_WORDLENGTH_8B;
    huart2.Init.StopBits = UART_STOPBITS_1;
    huart2.Init.Parity = UART_PARITY_NONE;
    huart2.Init.Mode = UART_MODE_TX_RX;
    huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart2.Init.OverSampling = UART_OVERSAMPLING_16;
    huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
    huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
    huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
    if (HAL_UART_Init(&huart2) != HAL_OK) {
        Error_Handler();
    }
    if (HAL_UARTEx_SetTxFifoThreshold(&huart2, UART_TXFIFO_THRESHOLD_8_8) != HAL_OK) {
        Error_Handler();
    }
    if (HAL_UARTEx_SetRxFifoThreshold(&huart2, UART_RXFIFO_THRESHOLD_8_8) != HAL_OK) {
        Error_Handler();
    }
    if (HAL_UARTEx_DisableFifoMode(&huart2) != HAL_OK) {
        Error_Handler();
    }
}

When I connect the RS485 module to UART2, I cannot receive any data, even though the transmit and receive buffers are the same for both UARTs.

Question:

What could be causing UART2 to fail while UART4 works perfectly? Is there any specific configuration or hardware difference I should look in to?! UART2

UART4

enter image description here

\$\endgroup\$
10
  • \$\begingroup\$ Are you sure about correct UART hardware? Did you checked cross functionality? \$\endgroup\$ Commented Jan 12 at 6:11
  • \$\begingroup\$ Need a schematic, please post one \$\endgroup\$ Commented Jan 12 at 7:32
  • \$\begingroup\$ Thank you for taking your time to answer my question appreciate it. I will add the UART hardware in the question pls check \$\endgroup\$ Commented Jan 12 at 8:03
  • 2
    \$\begingroup\$ You are not showing code that inits the IO pins and not the MCU side of schematics, not the code you use to write and read the UARTS. Yet you initialize them differently, uart 2 is an USART with fifos, uart4 ia a simpler UART with no fifos. \$\endgroup\$ Commented Jan 12 at 9:03
  • 1
    \$\begingroup\$ The IO pin init code is still not shown. We cannot know if you simply enable UART without connecting it to IO pins, or if you enable IO pins to begin with. Do not post screenshots of code, post the code. Why your schematics have weird reset that's against ST suggestions, and why you don't have a crystal, and why you use the weak PC13..PC15 pins as current sourcing outputs? \$\endgroup\$ Commented Jan 12 at 17:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.