Page 1 of 1

UART Send Raw Bytes

Posted: Sun Mar 25, 2018 4:54 am
by klteng
The SDK UART API seems to send character only. How do I send a RAW byte? For example, I want to send data 0xAA.

Re: UART Send Raw Bytes

Posted: Sun Mar 25, 2018 4:58 am
by kolban
Might the uart_write_bytes API do the trick?

Re: UART Send Raw Bytes

Posted: Sun Mar 25, 2018 6:35 am
by klteng
Hi Kolban,

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

Re: UART Send Raw Bytes

Posted: Sun Mar 25, 2018 7:01 am
by WiFive

Re: UART Send Raw Bytes

Posted: Sun Mar 25, 2018 8:22 am
by klteng
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.

Re: UART Send Raw Bytes

Posted: Sun Mar 25, 2018 4:33 pm
by kolban
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.

Re: UART Send Raw Bytes

Posted: Mon Mar 26, 2018 12:40 am
by klteng
Thank you very much. It works now!