Newbie UART write

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Newbie UART write

Postby urbanze » Wed Feb 27, 2019 5:17 pm

How to convert uint8_t to const char* ?

I trying to write some bytes (>127) but uart_write_bytes() only accept const char (<128). Like this:

Code: Select all


uint8_t SS[44];
for (int16_t i = 0; i < 44; i++)
{
	uart_write_bytes(UART_NUM_0, (const char*)SS[i], 1);
}


chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: Newbie UART write

Postby chegewara » Wed Feb 27, 2019 7:34 pm

How did you init uart with:

Code: Select all

    uart_driver_install(UART, RX_BUF_SIZE, TX_BUF_SIZE, QUEUE_SIZE, &uart_queue, FLAGS);

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Newbie UART write

Postby urbanze » Wed Feb 27, 2019 11:04 pm

chegewara wrote:
Wed Feb 27, 2019 7:34 pm
How did you init uart with:

Code: Select all

    uart_driver_install(UART, RX_BUF_SIZE, TX_BUF_SIZE, QUEUE_SIZE, &uart_queue, FLAGS);
I not understanding, This not use any parameter with const char...? :(

Code: Select all

uart_driver_install(UART_NUM_0, 1024, 0, 0, NULL, 0);

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

Re: Newbie UART write

Postby ESP_Sprite » Thu Feb 28, 2019 1:53 am

Force-casting them to a char* should work. No specific need to add the const here, the const in this case is a promise from the function that it won't change the data a caller passes onto it.

User avatar
urbanze
Posts: 301
Joined: Sat Jun 10, 2017 9:55 pm
Location: Brazil

Re: Newbie UART write

Postby urbanze » Thu Feb 28, 2019 12:52 pm

ESP_Sprite wrote:
Thu Feb 28, 2019 1:53 am
Force-casting them to a char* should work. No specific need to add the const here, the const in this case is a promise from the function that it won't change the data a caller passes onto it.
Well, no logic, but worked :lol:

Code: Select all

for (int16_t i = 0; i < 44; i++)
{
	const char x = (int8_t)SS[i];
	uart_write_bytes(UART_NUM_0, &x, 1);
}

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

Re: Newbie UART write

Postby ESP_Sprite » Sun Mar 03, 2019 9:12 am

Eh, it's not that pretty but it'll work, and the compiler will probably optimize it to the same instructions as if it would if you casted it.

Who is online

Users browsing this forum: akasaka_spk and 86 guests