Nextion v0.9.0 Library With Working GUI Events

For anyone who wants to use it, here are three modified files that fix the serial communications handling in the v0.9.0 Nextion library so that events actually work. The serial code in the Nextion library is atrocious - it discards messages at random, throws in long delays, and does other stupid things, so that GUI events and timers are basically unusable. This modification fixes all that, so events work AFAICT, perfecty.

There is one (optional) change in the API - I’ve added an optional baud rate argument to the nexInit function, to make it easier to change from the default 9600 baud to anything you want.

I’m just tossing these files out there for anyone who wants them, but make no claims it is 100% correct, though I’ve been using it for weeks with zero problems. If you find a problem, let me know, and I will try to correct it, but no guarantees.

Just copy the three files to your Nextion library folder, and, if you want a different baud rate, add the baud rate as an argument to your nexInit() call. Everything else should work just as before.

Regards,
Ray L.

NexConfig.h (1.38 KB)

NexHardware.cpp (12.4 KB)

NexHardware.h (1.85 KB)

1 Like

Thanks - I'll definitely be taking a look at this.

I don't think it works with 24V though.... :confused:

Oh!

All - I've updated the nexHardware.cpp file in the first post of this thread. The one I originally attached was using printf, and doing other things that won't work for some people.

Regards,
Ray L.

:o Simple bloke like me is still having trouble with printf with debugSerial enabled.

Thank you, RayLivingston! Now the NexNumber.getValue is working as they should . You saved my day!

Great works !!! Why You don’t ask to merge to main code ?

Thanks a lot.
I have problem with get values from nextion but when I use your upgraded library all works fine.

Hello,

thanks for this wonderfull upgrade.

But there seems to be a problem.

I am also using the numbers to get the font color of a text (pco).
In case the font color is e.g. 65534 which is white it will not get this number because it comes as 0xFF

And the counter for 0xFF starts (but will be read as the 3 times 0xFF at the end.

Any idea how to update this and get the Font Color PCO value as a number?

Thanks :slight_smile:

Thank you for the awesome fix to this library. I’m no expert, but reading the serial from the display once and then parsing it out seems so much better than the way itead was doing it. I can communicate both ways now.

However, I’m encountering a problem. When I send data to the display, for example updating a slider value, it seems like the code is waiting for a response from the nextion and not getting it. I get these messages in my debug window.

getResp timeout
recvRetCommandFinished err

This ends up slowing down the refresh rate of my sliders by the timeout delay. Can you please point me in the right direction?

-Alex

Hi There can someone help please i went use the nextion libary with the modified files from post 1 and all I get is the following message.

Multiple libraries were found for "Nextion.h"
Used: C:\Users\Bob\Documents\Arduino\libraries\ITEADLIB_Arduino_Nextion-master
Multiple libraries were found for "SPI.h"
Used: C:\Program
Multiple libraries were found for "SD.h"
Used: C:\Program
Multiple libraries were found for "SoftwareSerial.h"
Used: C:\Program
exit status 1
Error compiling for board Arduino/Genuino Uno
I dont know what is happening
thanks
Bob

Hi,

Since I'm using the nextion Intelligent screen my serial comm is completely rubbish. (way worse than on the enhanced one's i used.

So I am really desperate to get this working.
I hoped that this would fix my problems, but when I tried your files I can't compile anymore. I'm getting errors from the NexHardware.cpp

Any idea how i can fix this?

Arduino: 1.6.5 (Windows 8.1), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:69:19: error: 'parseTargets' is not a class or namespace
int parseTarget = parseTargets::PARSE_MSG;
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp: In function 'void readDataMsg()':
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:117:35: error: 'parseTargets' is not a class or namespace
parseTarget = parseTargets::PARSE_MSG;
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:148:23: error: 'parseTargets' is not a class or namespace
parseTarget = parseTargets::PARSE_MSG;
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp: In function 'void parseRx(byte*, int)':
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:164:25: error: 'parseTargets' is not a class or namespace
if ((parseTarget == parseTargets::PARSE_STRING) ||
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:165:25: error: 'parseTargets' is not a class or namespace
(parseTarget == parseTargets::PARSE_NUMBER) ||
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:166:25: error: 'parseTargets' is not a class or namespace
(parseTarget == parseTargets::PARSE_DISCARD))
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:184:35: error: 'parseTargets' is not a class or namespace
parseTarget = parseTargets::PARSE_STRING;
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:189:35: error: 'parseTargets' is not a class or namespace
parseTarget = parseTargets::PARSE_DISCARD;
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:202:35: error: 'parseTargets' is not a class or namespace
parseTarget = parseTargets::PARSE_NUMBER;
^
C:\Arduino\libraries\ITEADLIB_Arduino_Nextion-master\NexHardware.cpp:206:35: error: 'parseTargets' is not a class or namespace
parseTarget = parseTargets::PARSE_DISCARD;
^
Error compiling.

This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.

Thanks for your work on the library. I have been using it for some time and stumbled across a problem with the getValue function. When returning a number that contained the HEX bytes 'FF', the function fails with message getNumber timeout. It can be replicated by simply writing:

ndspSerial.setValue(255);
ndspSerial.getValue(&retval);

ndspSerial is a number variable in my HMI. I have repeated this with FF in the first, second, third, and fourth positions, values 255, 65280, 16711680, and -16777216. Other values fail as well, 65281 for example. Until the FF byte clears the function errors.

Hi Ray -

Thank you for these changes. I wanted to let you know that your changes also make SofwareSerial work properly with a Nextion display and the Nano. Very useful!

Regards,
Dean

thanks... it works like a charm!

It's unbelievable that the manufacturer does not even admit the existence of the problem in their official library...

Best Regards

Giampaolo

MERCI!!!

je peut enfin mettre setValue et getValue ou je veux dans le code sans que ça interfère avec les fonctions de rappel (PopCallBack())de la lib.

j'ai juste reglé tous les timeout à 200 dans NexHardware.h, sinon j'avais des erreurs de réception; un peu plus de latence mais pas critique pour mon usage.

Encore merci :slight_smile: :slight_smile:

Hi Ray -

If you are still monitoring!

Could you please send/post the hardware.cpp file that does not use printf? You noted that you updated the original post, but the downloaded file still uses printf for the the serial debug statements.

Thanks,

Dean

Hello Ray L.,
thank you so fu.king much for the Nex library.

Thanks Ray! I'm new at using Nextion, and have been struggling with nexLoop missing button pushes. Replaced the 3 files in the Nextion library that you provided, recompiled my code, and everything works great. Also nice that those annoying debugging messages don't scroll through when using the Serial Monitor. Now I only see debug messages that I embedded in my code.

I know this is old, but updating the library files worked great for me without having to change my code. I have a constant countdown timer updating on the display and that seemed to bogg down the send/receive comms and cause button presses and other events to be missed. Using these updated library files seemed to help clear up those issues and I no longer get lost events.

Initially had some issues when using the Nextion Simulator in Debug mode not communicating. Had to modify the NexHardware.cpp file to use "Serial" as communication to display, rather than "Serial2". Hope this helps anyone else that has this issue.