AnsweredAssumed Answered

UART in STM32F407

Question asked by Lyubomyr D on May 2, 2018
Latest reply on May 7, 2018 by Clive One

Hello

 

I run into a stupid problem. I have a data that is sent by a device with bitrate115200. I need to read it with STM2F4. The data is five HEX numbers.

I was trying to do this in different ways but I failed. 

I am reading from UART4 and sending logs to UART2. All the setting is dome with CubeMX. Interrupts are on.

I am using a Discovery board.

 

I am using the Logic analyzer and I see that the device is sending correct data. But I receive not what I am seeing in Logic analyzer.

 

Please, help me.

 

Version 1:

struct_type data1;
struct_type*data = &data1;

sprintf(uart2Data,"Waiting for data \r\n");

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

 

HAL_UART_Receive_IT(&huart4, (uint8_t *)data, sizeof(struct_type)); // reading data

 

sprintf(uart2Data,"struct_elem1 = %d \r\n", data->struct_elem1);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem2 = %d \r\n", data->struct_elem2);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem3 = %lu \r\n",data->struct_elem3);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem4= %lu \r\n",data->struct_elem4);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem5 = %d \r\n",data->struct_elem5);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

 

Version 2

 

struct_type data;
uint8_t buffer[sizeof(struct_type )];

sprintf(uart2Data,"Waiting for data \r\n");

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

 

HAL_UART_Receive_IT(&huart4, (uint8_t *)&buffer, sizeof(struct_type )); // reading data

memcpy( &data, &buffer, sizeof(struct_type));

 

sprintf(uart2Data,"struct_elem1 = %d \r\n", data.struct_elem1);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem2 = %d \r\n", data.struct_elem2);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem3 = %lu \r\n",data.struct_elem3);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem4= %lu \r\n",data.struct_elem4);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
sprintf(uart2Data,"struct_elem5 = %d \r\n",data.struct_elem5);
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

Version 3

 

struct_type data1;

uint8_t buffer[sizeof(struct_type )];

uint8_t = 0;
while ( i<sizeof(struct_type ))
{
    HAL_UART_Receive_IT(&huart4, (uint8_t *)buffer[i], sizeof(uint8_t));
    i++;
}

for (i=0; i<sizeof(struct_type ); i++)
{
    sprintf(uart2Data,"buffer[%d] = %x \r\n", i, buffer[i]);
    HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
}

Outcomes