Page 1 of 1

Hi , is there any sample code for uart in esp32..??

Posted: Thu Apr 20, 2017 10:12 am
by Reeshma
My objective is ., when i send the data through uart , it should be able to process some function.
For example , when am send the char '1' or a string "ON" through uart , the led should blink like that .
Am a newbie to this platform , can anyone please say..?

Re: Hi , is there any sample code for uart in esp32..??

Posted: Thu Apr 20, 2017 11:22 am
by Reeshma
And one more thing , how to read the received char and string from uart ..?

Re: Hi , is there any sample code for uart in esp32..??

Posted: Thu Apr 20, 2017 12:08 pm
by ESP_krzychb

Re: Hi , is there any sample code for uart in esp32..??

Posted: Thu Apr 20, 2017 1:23 pm
by Reeshma
I have tried all those codes , i cannot able to read the string in esp32 , am just able to read only the length of the string, what modification will am need to do in order to read a string..?

Re: Hi , is there any sample code for uart in esp32..??

Posted: Thu Apr 20, 2017 7:27 pm
by martinayotte
You need to read character by character, fill a separate buffer with them, increment an index, and continue until you find a delimiter, usually CR, then compare buffer content with your available commands and execute the command found.

Re: Hi , is there any sample code for uart in esp32..??

Posted: Thu Apr 20, 2017 8:27 pm
by rudi ;-)
Reeshma wrote:I have tried all those codes , i cannot able to read the string in esp32 , am just able to read only the length of the string, what modification will am need to do in order to read a string..?
string is only a container with bytes, nothing more
if you talk about "string" you mean a char variable with idx len of the string, right?

Code: Select all

char myString[] = "hello world";

myString[0]
would be 'h'

myString[1]
would be 'e'

myString[2]
would be 'l'

and so on.


have you test the Uart echo example?
part of code is:

https://github.com/espressif/esp-idf/bl ... main.c#L60

Code: Select all

 uint8_t* data = (uint8_t*) malloc(BUF_SIZE);
    while(1) {
        //Read data from UART
        int len = uart_read_bytes(uart_num, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        //Write data back to UART
        uart_write_bytes(uart_num, (const char*) data, len);
    }
}
this code is all what you need to test it.
you send to the uart
and the uart will echo (const char*) data
if you send "hello world" and push enter
then the uart gives echo "hello world"

best wishes
rudi ;-)