nextion display function

hello

i have a nextion display, and i created a keypad so that you can set an analog value and send it to Arduino so i can control the speed of a dc motor for example.
now the value is stored in a text object, from the library i found this function:

uint16_t NexText::getText(char *buffer, uint16_t len)
{
    String cmd;
    cmd += "get ";
    cmd += getObjName();
    cmd += ".txt";
    sendCommand(cmd.c_str());
    return recvRetString(buffer,len);
}

would this function (getText) get the text from the nextion display to the arduino,
and what is the correct way to use, i don't understand what is the buffer for, i did something like this, where t1 is the text object

void b12Press (void *ptr)   {
  
t1.getText( char  *   buffer, 3);
}

thank you

i did something like this, where t1 is the text object

That didn't even compile, so, no that won't work.

The method writes to the buffer you supply, and returns some apparently useless value. You'd need to track down what recvRetString() returned to find out whether the value is actually useful, or not.

You must allocate an array, and pass the address (by specifying the name of the array) to that function.

thanks for your reply

this is the recvRetString

uint16_t recvRetString(char *buffer, uint16_t len, uint32_t timeout)
{
    uint16_t ret = 0;
    bool str_start_flag = false;
    uint8_t cnt_0xff = 0;
    String temp = String("");
    uint8_t c = 0;
    long start;

    if (!buffer || len == 0)
    {
        goto __return;
    }
    
    start = millis();
    while (millis() - start <= timeout)
    {
        while (nexSerial.available())
        {
            c = nexSerial.read();
            if (str_start_flag)
            {
                if (0xFF == c)
                {
                    cnt_0xff++;                    
                    if (cnt_0xff >= 3)
                    {
                        break;
                    }
                }
                else
                {
                    temp += (char)c;
                }
            }
            else if (NEX_RET_STRING_HEAD == c)
            {
                str_start_flag = true;
            }
        }
        
        if (cnt_0xff >= 3)
        {
            break;
        }
    }

    ret = temp.length();
    ret = ret > len ? len : ret;
    strncpy(buffer, temp.c_str(), ret);
    
__return:

    dbSerialPrint("recvRetString[");
    dbSerialPrint(temp.length());
    dbSerialPrint(",");
    dbSerialPrint(temp);
    dbSerialPrintln("]");

    return ret;
}

i think it only returns the number of strings??

i think it only returns the number of strings??

It returns the length of the string read, in case you can’t figure that out by using strlen().

Stupid code. There is no excuse for goto and labels in C++ code. That code was obviously written by someone that barely got a passing grade in whatever programming course they cheated their way through.