0

I have a problem with an STM23F103 I'm using UART to transmit and receive data.the data is sent and another board replies. I can see the communication with a Logic Analyzer which looks fine on it.but the received data on STM32 have the first byte either from the first byte of the packet send or from last byte of the last received packet.I don't know what's wrong with my code but I can't figure out how to solve the issue

Here is the code in the main

uint8_t b[5] = {0xAA,0xBB,0xCC,0xDD,0xEE};
HAL_UART_Transmit(&huart3,b, sizeof(b), 100);
uint8_t r[5]={0,0,0,0,0};
HAL_UART_Receive(&huart3, r, sizeof(r), 100); 
HAL_Delay(100);
uint8_t d[5] = {0x11,0x22,0x33,0x44,0x55};
HAL_UART_Transmit(&huart3,d, sizeof(d), 100);
uint8_t r2[5]={0,0,0,0,0};
HAL_UART_Receive(&huart3, r2, sizeof(r2), 100);
HAL_Delay(100);

and here the Init

static void MX_USART1_UART_Init(void)
{
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 10400;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
}

The packet that appears on the logic analyzer is 0xA1,B2,C3,D4,E5 but on the STM32 it receives AA,A1,B2,C3,D4 the first byte is always the same as the packet that I sent.but only on the first packet.

On the other packets are received like this 0xE5,66,77,88,99 while it should be 66,77,88,99,AA but I get E5 from the last received packet. I thought that that packet has not being received so I thought by increasing the size of r and r2 from 5 to 6 it would solve the issue but it doesn't. I receive AA,A1,B2,C3,D4,E5 for r and 0xE5,66,77,88,99,AA for r2.

I hope its detailed enough to see the issue.

I'm using STM32CubeIDE

Jwdsoft
  • 431
  • 3
  • 9

1 Answers1

0

Sounds like this may be a settings issue as your code looks fine.

I would start by double checking the UART properties in your UART_Init(). Make sure they're the same for both the sender and receiver (baud rate, parity etc). Your logic analyser will also need to know the correct UART properties.

Post a link to your code and I may be able to help you further.

JFirmware
  • 21
  • 7
  • please take a look at the post I've added the init method I don't think that there is a problem with the Init.I've used STM32Cube to generate the project and I didn't edit anything except for the baudrate to 10400 which also edited in the logic analyzer. also if I use an arduino board to do this part of sending the request it is done without issue and I receive the packets as expected.but I need to use STM32 instead – Jwdsoft Nov 27 '21 at 21:58
  • You have posted your Init for UART1. Your main code uses a UART3 handle (huart3). – JFirmware Nov 30 '21 at 10:06