2022-11-01 08:14 PM
I made a simple program on an ESP32 to send dummy data with something like this
$DUMMYDATA,0,1
I've tried using another ESP32 as a receiver and it works perfectly. My goals is implement the same thing to my STM32F103C8T6. Here's my code for the first attempt.
while (1)
{
HAL_UART_Receive(&huart2, receivedData, sizeof(receivedData), 100);
CDC_Transmit_FS((uint8_t *)receivedData, strlen(receivedData));
}
It works but the data is corrupted
⸮0kTԽ⸮5⸮⸮c 8 ⸮⸮ d⸮֩⸮&⸮⸮.⸮Z⸮�?|!Qi;⸮1⸮⸮z\⸮9⸮$DUMMYDATA,1,0
Then I changed the third line to something like this
HAL_UART_Receive(&huart2, receivedData, sizeof(receivedData), HAL_MAX_DELAY);
Now it works perfectly but with downside it just became really slow. Then I tried to add a simple logic to parse the data
while (1)
{
HAL_UART_Receive(&huart2, receivedData, sizeof(receivedData), HAL_MAX_DELAY);
strcpy(dataMode, strtok(receivedData, ','));
strcpy(latitude, strtok(NULL, ','));
strcpy(longitude, strtok(NULL, ','));
CDC_Transmit_FS((uint8_t *)dataMode, strlen(dataMode));
CDC_Transmit_FS((uint8_t *)latitude, strlen(latitude));
CDC_Transmit_FS((uint8_t *)longitude, strlen(longitude));
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
And now my STM didn't output anything at all. My questions are
I've attached my project if its useful. Thanks in advance!
Solved! Go to Solution.
2022-11-02 12:12 AM
I've downloaded your project and noticed several issues. You've set to receive 100 chars so you've have to wait to receive a string of "$DUMMYDATA,0,1" at least 8 times before HAL_UART_Receive() returns so that is why it seems slow before you send the data over USB.
If the string size will always be 14 characters then change the array to 14 instead of 100. Then you need to increase the latitude and longitude by 1 to allow for a null character.
Then you're trying to send all that data over CDC without checking to see if it's Transmit is still busy.
I've posted updated code to get you going.
uint8_t receivedData[14] = {0};
char dataMode[20], longitude[2], latitude[2];
while (1)
{
HAL_UART_Receive(&huart2, receivedData, sizeof(receivedData), HAL_MAX_DELAY);
strcpy(dataMode, strtok((char*)receivedData, ","));
strcpy(latitude, strtok(NULL, ","));
strcpy(longitude, strtok(NULL, ","));
while(CDC_Transmit_FS((uint8_t *)dataMode, strlen(dataMode))== USBD_BUSY);
while(CDC_Transmit_FS((uint8_t *)latitude, strlen(latitude))== USBD_BUSY);
while(CDC_Transmit_FS((uint8_t *)longitude, strlen(longitude))== USBD_BUSY);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
2022-11-02 12:12 AM
I've downloaded your project and noticed several issues. You've set to receive 100 chars so you've have to wait to receive a string of "$DUMMYDATA,0,1" at least 8 times before HAL_UART_Receive() returns so that is why it seems slow before you send the data over USB.
If the string size will always be 14 characters then change the array to 14 instead of 100. Then you need to increase the latitude and longitude by 1 to allow for a null character.
Then you're trying to send all that data over CDC without checking to see if it's Transmit is still busy.
I've posted updated code to get you going.
uint8_t receivedData[14] = {0};
char dataMode[20], longitude[2], latitude[2];
while (1)
{
HAL_UART_Receive(&huart2, receivedData, sizeof(receivedData), HAL_MAX_DELAY);
strcpy(dataMode, strtok((char*)receivedData, ","));
strcpy(latitude, strtok(NULL, ","));
strcpy(longitude, strtok(NULL, ","));
while(CDC_Transmit_FS((uint8_t *)dataMode, strlen(dataMode))== USBD_BUSY);
while(CDC_Transmit_FS((uint8_t *)latitude, strlen(latitude))== USBD_BUSY);
while(CDC_Transmit_FS((uint8_t *)longitude, strlen(longitude))== USBD_BUSY);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
2022-12-01 12:23 AM
Thank you for your feedback. I've implemented it and it works.