UART Send Raw Bytes

klteng
Posts: 10
Joined: Sat Feb 25, 2017 4:15 pm

UART Send Raw Bytes

Postby klteng » Sun Mar 25, 2018 4:54 am

The SDK UART API seems to send character only. How do I send a RAW byte? For example, I want to send data 0xAA.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: UART Send Raw Bytes

Postby kolban » Sun Mar 25, 2018 4:58 am

Might the uart_write_bytes API do the trick?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

klteng
Posts: 10
Joined: Sat Feb 25, 2017 4:15 pm

Re: UART Send Raw Bytes

Postby klteng » Sun Mar 25, 2018 6:35 am

Hi Kolban,

How do you send the raw bytes? Do you have example code?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: UART Send Raw Bytes

Postby WiFive » Sun Mar 25, 2018 7:01 am


klteng
Posts: 10
Joined: Sat Feb 25, 2017 4:15 pm

Re: UART Send Raw Bytes

Postby klteng » Sun Mar 25, 2018 8:22 am

I have the following code:

int sendData(const char* logName, const char* data)
{
const int len = strlen(data);
const int txBytes = uart_write_bytes(UART_NUM_2, data, len);
ESP_LOGI(logName, "Wrote %d bytes", txBytes);
return txBytes;
}

static void tx_task()
{
static const char *TX_TASK_TAG = "TX_TASK";
while (1) {
sendData(TX_TASK_TAG, 0xAA);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}

How do I declare the 0xAA as a byte? Currently the code expecting a string.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: UART Send Raw Bytes

Postby kolban » Sun Mar 25, 2018 4:33 pm

Howdy my friend

how about

Code: Select all

char data[] = { 0xAA };
sendData(data, sizeof(data));
In this case, pass the size of your data buffer in as a parameter.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

klteng
Posts: 10
Joined: Sat Feb 25, 2017 4:15 pm

Re: UART Send Raw Bytes

Postby klteng » Mon Mar 26, 2018 12:40 am

Thank you very much. It works now!

Who is online

Users browsing this forum: Google [Bot] and 415 guests