0

I want to send a string over UART but the function that sends data over UART accepts uint8_t as an argument.

The function prototype: UART2_Write(uint8_t txData);

I wish to send an 8-character long string such as "12345678" and I have tried the following ways:

#define UID ("12345678")
UART2_Write(UID);

char UID[8] = "12345678";
UART2_Write(UID);

const char *UID = "12345678";
UART2_Write(UID);

Unfortunately, none of the above-mentioned methods yielded a successful result.

If there is a way to send a string in this situation, kindly let me know.

Thanks

1
  • 1
    Sounds like you need to loop over all the characters in your string, calling UART2_Write multiple times, once for each character. Commented Oct 5, 2022 at 18:42

1 Answer 1

2

Sending an entire 8-byte string in a single UART serial communication is not strictly possible, since UART sends data in blocks of 5-9 bits at a time. Based on the input parameter type of UART2_Write(), this transmission appears to be done in 8-bit mode. You must send each of the bytes in the string individually.

This can be done by looping over the characters in the string.

for instance:

for(size_t i = 0; UID[i] != '\0'; ++i)
  UART2_Write(UID[i]);

then, if the serial receiver is only expecting 8-bytes you're done, otherwise you may need to send a terminating character, like 0, which corresponds to NULL in ASCII, or perhaps you may want to send EOT (end of transmission), which corresponds to 0x4 in ASCII. To find out what you should send to signal that the transmission is over, you may need to read documentation on the device being communicated with via UART.

Sign up to request clarification or add additional context in comments.

7 Comments

I see. On the receiver side, will the string appear as one continuous string? Or will it be one character at a time?
The receiver will get the string 1-byte at a time, but if it's expecting a string, that is how it will expect to receive it and it shouldn't be a problem
Even modern internet connections work by sending data 1-byte at a time. This is just how digital connections tend to work
strlen() is redundant here. size_t i = 0; do UART2_Write(UID[i]) while(UID[i++]); Or const char *s = UID; do UART2_Write(*s) while(*s++); - if null terminator also must be sent.
This is true, but any compiler worth its weight in parmesan cheese is going to optimize strlen(<macro that expands to a string>) into an integer. It is definitely worth noting that neither strlen() nor the while loop @dimich suggests will work for the second example where the char[] is only 8-bytes long and therefore not null-terminated
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.