cancel
Showing results for 
Search instead for 
Did you mean: 

How to transmit data using RS485 with uart?

MÇETİ.1
Associate III

Hi everyone,

I'm trying to transmit and receive data through rs485 with uart. Using Uart with DMA in normal mode and referance from this project.

https://controllerstech.com/rs485-module-and-stm32/

However, in this project there are 2 different stm. I want to use 1 stm 2 uarts and 2 module. Therefore I combine these two codes. It writes SEND to tx buffer but neither transmit nor receive. When I check serial port to see data, only 00 or null

void dataSend (uint8_t *data)
{
           if(huart.Instance == UART1)
      {           
		HAL_GPIO_WritePin(UART1_EN_GPIO_Port, UART1_EN_Pin, 1);
		HAL_UART_Transmit_DMA(&huart1, (uint8_t*)rx1_buffer, rx_buff);
		HAL_GPIO_WritePin(UART1_EN_GPIO_Port, UART1_EN_Pin, 0);
 
       }
        else if(huart.Instance == UART2)
       {
		HAL_GPIO_WritePin(UART2_EN_GPIO_Port, UART2_EN_Pin, 1);
		HAL_UART_Transmit(&huart2, rx2_buffer, sizeof(rx_buff),1000);
		HAL_GPIO_WritePin(UART2_EN_GPIO_Port, UART2_EN_Pin, 0);
          }
 
}
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
	if(huart->Instance == USART1)
	{
	  HAL_UARTEx_ReceiveToIdle_DMA(&huart1, rx1_buffer, rx_buff);
 
	}
	else if(huart->Instance == USART2)
	{
			rx3_buffer[0] = "F";
			rx3_buffer[1] = "P";
			rx3_buffer[2] = "G";
			rx3_buffer[3] = "A";
 
			dataSend(rx2_buffer);
			HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rx2_buffer, rx_buff);
 
	}
}
 
 
int main()
{
 
  HAL_UARTEx_ReceiveToIdle_DMA(&huart1, rx1_buffer, rx_buff);
  HAL_Delay(100);
  HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rx2_buffer, rx_buff);
 
 
 
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
	  sprintf(tx1_buffer, "SEND %d",indx++);
	  dataSend(tx1_buffer);
	  HAL_Delay(100);
  }
}

0 REPLIES 0