KEMBAR78
(Cheatsheet) Nextion Get Text and Number | PDF | Science & Mathematics | Computers
100% found this document useful (1 vote)
371 views2 pages

(Cheatsheet) Nextion Get Text and Number

The code defines functions to send commands to a serial device, receive numeric responses, and parse the responses. sendCommand() sends the given command string and delimiter bytes. recvRetNumber() receives up to 8 bytes of response data within a timeout period, checks for a valid response header, and extracts a 32-bit number from the payload bytes if valid. It returns true if a number was successfully received.

Uploaded by

Ali Fajar Hadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
371 views2 pages

(Cheatsheet) Nextion Get Text and Number

The code defines functions to send commands to a serial device, receive numeric responses, and parse the responses. sendCommand() sends the given command string and delimiter bytes. recvRetNumber() receives up to 8 bytes of response data within a timeout period, checks for a valid response header, and extracts a 32-bit number from the payload bytes if valid. It returns true if a number was successfully received.

Uploaded by

Ali Fajar Hadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

String cmd = "get t1.

val"
sendCommand (cmd.c_str());
return recvRetNumber(number);

//function yang harus dipenuhi:

//recvRetNumber(uint32_t *number);
//sendCommand
/* example return: 70 30 30 33 FF FF FF in hex, p003\xFF\xFF\xFF in str
the first byte p (is a text) q(is a number) note: IN A NUMBER, there isn't any
difference between float and int,
example, float 7.0 will be translated to 70, but to be consider, it also depends on
comma point setting in hmi file

for multiple message we can cut the string using "ÿÿÿ" (or "\xFF\xFF\xFF"), then
see the first byte is it "q" or "p" to determine type
*/

void sendCommand(const char* cmd);

void sendCommand(const char* cmd)


{
while (nexSerial.available())
{
nexSerial.read();
}

nexSerial.print(cmd);
nexSerial.write(0xFF);
nexSerial.write(0xFF);
nexSerial.write(0xFF);
}

bool recvRetNumber(uint32_t *number, uint32_t timeout){


bool ret = false;
uint8_t temp[8] = {0};

if (!number){
goto __return;
}

nexSerial.setTimeout(timeout);
if (sizeof(temp) != nexSerial.readBytes((char *)temp, sizeof(temp))){
goto __return;
}

if (temp[0] == NEX_RET_NUMBER_HEAD
&& temp[5] == 0xFF
&& temp[6] == 0xFF
&& temp[7] == 0xFF
){
*number = ((uint32_t)temp[4] << 24) | ((uint32_t)temp[3] << 16) | (temp[2]
<< 8) | (temp[1]);
ret = true;
}

__return:
if (ret) {
dbSerialPrint("recvRetNumber :");
dbSerialPrintln(*number);
}
else{
dbSerialPrintln("recvRetNumber err");
}

return ret;
}

You might also like