UART should be send single bytes

BergLoewe
Posts: 17
Joined: Wed Mar 18, 2020 10:34 am

UART should be send single bytes

Postby BergLoewe » Mon Jan 18, 2021 6:06 pm

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?

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: UART should be send single bytes

Postby ESP_Sprite » Tue Jan 19, 2021 1:46 am

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);

Who is online

Users browsing this forum: Baidu [Spider] and 398 guests