Page 1 of 1
Newbie UART write
Posted: Wed Feb 27, 2019 5:17 pm
by urbanze
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);
}
Re: Newbie UART write
Posted: Wed Feb 27, 2019 7:34 pm
by chegewara
How did you init uart with:
Code: Select all
uart_driver_install(UART, RX_BUF_SIZE, TX_BUF_SIZE, QUEUE_SIZE, &uart_queue, FLAGS);
Re: Newbie UART write
Posted: Wed Feb 27, 2019 11:04 pm
by urbanze
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);
Re: Newbie UART write
Posted: Thu Feb 28, 2019 1:53 am
by ESP_Sprite
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.
Re: Newbie UART write
Posted: Thu Feb 28, 2019 12:52 pm
by urbanze
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
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);
}
Re: Newbie UART write
Posted: Sun Mar 03, 2019 9:12 am
by ESP_Sprite
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.