Reading data from serial port

Hi,
I’m trying to read data from the port on a Nextion display. I want to use a display button to tell the nextion to read 10 characters from the serial port. I can’t find any instructions for reading the port, and I’m sure there are instructions for that. Please help.

Here’s what I want to do… Button b1 is pressed, activating an event code that reads 10 characters from the serial port. The data gets moved into text box t1.txt.

This is not related to arduino or raspberry pi.

Thank you for your help

Andy

You’ll habe to put the Nextion into protocol reparse mode with the recmod setting. It will then stop interpreting the serial input as Nextion language ascii commands and write incoming bytes directly to a buffer, the so called u array which you’ll have to setup with the usize command, and where you can easily retrieve the bytes and handle them to taste with the ucopy and afterwards the udelete commands.
All these are well described in the Nextion Instruction Set document.

Thank you for your help. I have one other, general question. I am using this display to interface with a pre-made device that outputs a serial stream. This is not a processor I can get into. I can just read the stream. Therefore, the reparse mode should work fine.

But it seems like 99% of all posts about this device include the arduino or the raspberry pi. Are these screens specifically intended for use with a microprocessor platform, such as these?

In opposite to a simple, “classic” LCD display which would always need an external processor to be driven, the Nextion HMIs are somewhat “smarter”, since they have a built in CPU and memory. The primary goal of using a Nextion is to take off all the resource consuming burden of rendering (shapes, fonts, etc.) and driving the physical touch interface from the external CPU (Arduino, STM, PIC, whatever). Over the years, their internal CPU and the built-in programming language interpreter have become more and more powerful, so that, in certain specific and limited cases, the external CPU can be omitted and the Nextion used as a stand-alone solution. That’s not what they are designed for, it’s an interesting and sometimes challenging side effect.

1 Like

I have written the code for the display to read from the buffer and fill in the text boxes , I have also written code that lets the display do CRC16 in various polynomials not just modbuss here you will find code Inverter Remote Screen with a Nextion - AEVA Forums
Note it is also possible to write text directly to an area of the screen without a text box.
I am using it on its own with devices that expect a command + crc and send back an answer data + crc.

Thank you. This is very helpful.

Did u got the command to read serial port data
If u got then please tell me
I too stuck in same problem

Tell me what you are looking for specifically and maybe I can help you.

Here’s my code using ucopy to read from the buffer.

vis t0,0
ucopy BufferData.txt,0,usize,0
spstr BufferData.txt,TempString.txt,“\r”,18
substr TempString.txt,Checksum.txt,0,8
//t1.txt=Checksum.txt
substr BufferData.txt,PID.txt,2,3
//t2.txt=PID.txt
if(Checksum.txt==“Checksum”&&PID.txt==“PID”)
{
for(k1=1;k1<20;k1++)
{
spstr BufferData.txt,SplitString.txt,“\r”,k1-1
b[k1].txt=SplitString.txt
}
}
code_c

Actually I am looking to read the data “A1.Colour=Red” from serial to change the colour of box and similarly to change name by receiving data “A1.Name=C1” from serial

Are you unable to parse the stream, or to change the properties of the box?

I am unable to read data from serial

Did you set the baud rate in program.s?

//The following code is only run once when power on, and is generally used for global variable definition and power on initialization data
int sys0=0,sys1=0,sys2=0,AmpRated,Lithium,InUpdate,Load,PV,k1,ktrend,kreg,kcalc,NoSleep,BVR,MaxPwr,MaxPVolts,MaxBVolts,MinBVolts,Yield,BTime,FTime,ATime,BVavg,PVavg,Disp,SleepMinutes
baud=19200
recmod=1
Disp=100
SleepMinutes=5
page 0 //Power on start page 0

I need that command to read serial data

//TIMER 1
// Check the ReadBuffer
covx usize,buffersize.txt,0,0
ucopy n4.val,6,1,0
ucopy n3.val,5,1,0
ucopy n2.val,5,2,2
if(usize>=3)
{
usize=usize-1
va1.val=130
crcrest 1,0xffff // reset CRC
crcputu 0,4 // check of the serial buffer
sys0=0 // sys0 is a 4-byte shaped data. The CRC result is only 2 bytes, so the extra bytes of data are cleared first.
va2.val=0
ucopy va4.val,129,2,0 // assigns the CRC result received in the receive buffer to sys0
covx va4.val,va3.txt,0,0
n0.val=va4.val
n1.val=crcval
if(va4.val==crcval)
{
t11.txt=“CRC verification passed”
}else
{
t11.txt=“CRC Check failed”
}
usize=usize-2
// Status update
result.txt=“Reply received”
// Data received
ucopy data.txt,1,usize,0
spstr data.txt,t3.txt," “,0
spstr data.txt,t4.txt,” “,1
spstr data.txt,t5.txt,” “,2
spstr data.txt,t6.txt,” “,3
spstr data.txt,t7.txt,” “,4
spstr data.txt,t8.txt,” “,5
spstr data.txt,t9.txt,” “,6
spstr data.txt,t10.txt,” ",7
// Disable timers
tm0.en=0
tm1.en=0
// Disable Protocol Reparse Mode (this clears the buffer)
recmod=0
}

Can u tell which are the tools u have taken. Like what is n2,n3,n4,buffersize etc.

Trying to build this project.
It uses rare lib NeoNextion.

Brute-force approach would be to re-write the entire project with Nextion lib… but I am lazy :slight_smile:
The problem is in reading data from SoftwareSerial of HMI.

    SoftwareSerial nextionSerial(D5, D6); // RX, TX
    Nextion nex(nextionSerial);
    uint32_t number;
    nex.sendCommand("get rtc0");
    nex.receiveNumber(&number));
    Serial.println(" >> number: " + String(number));

This always returns >> number: 4277137406

    SoftwareSerial nextionSerial(D5, D6); // RX, TX
    Nextion nex(nextionSerial);
    char * res = '000000';
    nex.sendCommand("get rtc0");
    nex.receiveString(res, 7));
    Serial.println(" >> res: " + String(res));

This just crashes.

Tried also print rtc0 and prints rtc0,4 with the same results.
Please, share Arduino-like example how to read RTC or other valiable value from Nextion.