2018-05-02 08:24 AM
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 datamemcpy( &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);}#stm32f-uart2018-05-02 04:46 PM
Are you using the Cube ?
I had to enable the DMA in a circular buffer
then I read the buffer DMA counter, to check if a byte(s) has arrived.
2018-05-02 05:18 PM
What exactly does not work? Do you receive less than 5 bytes? Nothing at all?
-- pa
2018-05-03 12:46 AM
No, I am not using DMA. I tried but I was receiving only the first data value all the time.
2018-05-03 12:48 AM
I am receiving not the data that I expect to receive. I see the correct data on the Logis Analyzer, but when I log to UART2 I see something different.
2018-05-03 01:14 AM
I am receiving not the data that I expect to receive.
Then what do you expect, and what do you see ?
People here solely rely on the information you provide ...
... but when I log to UART2 I see something different.
Your debug output is not really a criteria. Better check what UART4 actually receives.
2018-05-07 10:12 AM
Thank You very much for Your time and hints.
I found the problem. Apparently it was in two following lines
sprintf(uart2Data,'Waiting for data \r\n');
HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);
The dta transfer is so fast that some data was lost during logging intermediate values to uart2. I removed that amd everything worked.
I appreciate Your help a lot.
2018-05-07 12:25 PM
You might want to consider a larger buffer, or pool of buffers which you release/manage in the callback function.
Also note that sprintf() returns the length of the string it creates.