Page 1 of 1

UART should be send single bytes

Posted: Mon Jan 18, 2021 6:06 pm
by BergLoewe
Hello,


How I can send/receive single bytes with the UART?
The

Code: Select all

int uart_write_bytes(uart_port_t uart_num, const void *src, size_t size)
require a const char. To this I can't assign a '0xaa'.

In the RS485_ECHO example I've the variable 'data'. This I can't assign to uart_write_bytes because it is uint_8.

Are there a function to send or recieve in bytes not in ASCII-chars?

Re: UART should be send single bytes

Posted: Tue Jan 19, 2021 1:46 am
by ESP_Sprite
There's no reason a 'const char[]' needs to contain ascii characters. For instance:

Code: Select all

const char myData[4]={0, 0xFF, 0xAA, 0x55};
int uart_write_bytes(my_uart_num, myData, 4);
will happily do what you want. Wrt writing one character, you can use the trick that a pointer to a variable is the same as a pointer to the first item in an array containing a single item. (Sorry, can't explain it any easier.). As such, this will work:

Code: Select all

char mySingleCharacter;
mySingleCharacter='A'; //note single quotes
int uart_write_bytes(my_uart_num, mySingleCharacter, 1);
mySingleCharacter=0xA5; //non-ascii will also work
int uart_write_bytes(my_uart_num, mySingleCharacter, 1);