1
\$\begingroup\$

I am trying to develop a sensor driver on the STM32F401CCU6. I have already worked out the sensor data acquisition on the STM. The problem I am facing is getting that over USB to my host system. Before I start sending the actual sensor data over, I am trying to send a simple "Hello World!\n" over to my host computer. I want to first figure out the correct way to receive and decode the data on my host system.

Here is how I have declared the string to send:

const char* start_byte = "Hello World!\n";

Here is how I am sending it over USB to the host system in the main() function:

while (1)
{
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
      CDC_Transmit_FS((uint16_t*)start_byte, sizeof(start_byte));
}

When I read the serial data on my host system (using python, CoolTerm, Putty), it only shows the string "Hell".

Here is the sample output on the serial monitors:

llHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHellHell

The string is never-ending. It terminates after the second 'L'. Any help would be appreciated. Thanks in advance!

\$\endgroup\$
4
  • 2
    \$\begingroup\$ You're taking the sizeof a pointer to your string, not of the string itself. Declare your string as an array like const char start_byte[] = "Hello World!\n"; and it'll work. \$\endgroup\$
    – brhans
    Sep 30, 2022 at 13:50
  • \$\begingroup\$ @brhans thanks! I changed sizeof to strlen and it worked. I also declared the start_byte as uint8_t* instead of const char* \$\endgroup\$
    – Harit
    Sep 30, 2022 at 14:09
  • \$\begingroup\$ Yeah that'll work just as well. Maybe better to use strlen since then you don't unnecessarily send the string's \0 terminator byte. \$\endgroup\$
    – brhans
    Sep 30, 2022 at 14:17
  • \$\begingroup\$ @brhans Thanks for that bit of information! I was unaware of the \0 terminator for strings. Now the raw data makes more sense to me. I was seeing this terminator throughout my raw data and was so confused. Appreciate your insights :) \$\endgroup\$
    – Harit
    Oct 4, 2022 at 11:34

1 Answer 1

0
\$\begingroup\$

Thanks to @brhans for pointing out my mistake. When I use sizeof, I was getting the size of the pointer to my string in return. Instead, I needed to use strlen to get the length of the string. With strlen, the correct number of bytes are transmitted and I am able to receive my string on the host machine.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Note: you can mark your own answer as accepted to indicate the problem has been solved. \$\endgroup\$
    – Glorfindel
    Oct 4, 2022 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.