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

Reeshma
Posts: 37
Joined: Tue Mar 28, 2017 12:40 pm

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

Postby Reeshma » Thu Apr 20, 2017 10:12 am

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..?

Reeshma
Posts: 37
Joined: Tue Mar 28, 2017 12:40 pm

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

Postby Reeshma » Thu Apr 20, 2017 11:22 am

And one more thing , how to read the received char and string from uart ..?


Reeshma
Posts: 37
Joined: Tue Mar 28, 2017 12:40 pm

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

Postby Reeshma » Thu Apr 20, 2017 1:23 pm

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..?

User avatar
martinayotte
Posts: 141
Joined: Fri Nov 13, 2015 4:27 pm

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

Postby martinayotte » Thu Apr 20, 2017 7:27 pm

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.

User avatar
rudi ;-)
Posts: 1727
Joined: Fri Nov 13, 2015 3:25 pm

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

Postby rudi ;-) » Thu Apr 20, 2017 8:27 pm

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 ;-)
-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

Who is online

Users browsing this forum: No registered users and 89 guests