Hi. I am trying to write a code on page0 that presses a button on page1. There is code behind the button on page1 and I’d like to run it once every five seconds, even if page1 is not the active page.
I have a timer on page0 that clicks the button on page1 every five seconds. But the code on page1 doesn’t run.
I am assuming that the code won’t run unless the page is active. Is there a way to have one central code that runs all the codes on all the pages?
Unfortunately, at least for the moment, it‘s not possible. The Nextion firmware always unloads all pages but the active one from memory. Only code contained in the active page, or - with restrictions - code in program.s before a page is loaded, can be executed at runtime.
Thank you. So if I put code in program.s, how can i cause it to run? I had the impression that code only ran at the screen initialization.
Yep. No other way except for copying your code to every single page on which you want to use it.
Side note: if you want to run functions (meaning code without a visible component on screen), you can use hotspots. They have the smallest memory footprint since they have no color, picture, … attributes.
Thanks Max, that’s what I was afraid of. I’m in reparse protocol mode and I have to read and parse the serial buffer on every page where there are values coming from the serial port. It’s not a ton of code, but it is cumbersome because some of my data is 8 bit, some is 16, and some is 32. All data come in as integers, but some with scaling of 1, some with 10, some with 100. So every datum has to be considered for length, scaling, and destination.
What I wanted to do is for each display box put a code behind the click function which loaded a global register with the length of the incoming word and the scaling. I’d use a timer to click each box, the box would load the registers and cause a serial read and parse. The destination, of course, would be the box.
Why this won’t work is because if I have a display box on page 2, and I run the code on page 1, even if my page 1 code included code to click on the page 2 box, the code behind the box wouldn’t run, and the registers wouldn’t get loaded.
Any ideas on how to do this without copying the code onto every page?
I don‘t fully understand what exactly you‘re trying to do or why it has to be that way. So I can‘t really suggest any alternatives. The only thing I can guarantee you is that there are exactly 2 sources from which code can be executed. The current active page and the serial port (unless you‘ve reparse mode activated). And there‘s absolutely no way around this limitation. Variables can be global. Code not.
Believe me, I wish it was different because I could use it myself. Out of the 8000 lines of nextion code my HMI has, about 6000 are copy paste (there are more cases where you have no other way than copying).
Wow. 6000 lines of copy paste. I guess I’ll stop complaining. My code to parse the serial port is only 20 lines.