Home > Back-end >  Passing String to a function that takes uint8_t in C
Passing String to a function that takes uint8_t in C

Time:10-06

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

CodePudding user response:

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.

  • Related