Seithan / EasyNextionLibrary

A simple library for Nextion display that uses only four functions. You can easily benefit from Nextion's wide range of features and advantages in just a few easy steps. The library uses a custom protocol that can prove to be a powerful tool for advanced users as it can be easily modified to meet one’s needs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Read negative number 2.

s56jsc opened this issue · comments

commented

Dear
I was away for long time. I'm sorry, for not respond for a long time.
Problem was not solved. Like I say before. When you send to display it work fine and you got negative number on display
But when you wont to read this number from display you got overflow (display give you example for -1 --> F6 FF FF FF)
image
I think that command myNex.readNumber("nastavitve.pop_sonda.val"); cannot handle this

Have you declare the variable as uint32_t on your code?
try to use standard prefix for Nextion attributes (page name, object name) like n11 instead of nastavitve.pop_sonda

commented

I use uint32_t and if I use +1 I got answer 10 (correct)
If I use -1 I got 4294967286 (in hex FF FF FF F6 ) this is also correct respond from nextion. Look my previews mail.
I still think that there is bug in lib.
I can recalculate this in negative number, but if you fix this in lib, it will be great also for other users

can you please send me Arduino code and HMI for Nextion?
seithagta@gmail.com

commented

Will be send

Solution:
convert unsinged variableuint32_tto signed using long type or int32_t to print the negative integer with a minus sign (-) in a serial monitor.
example:

int32_t myValue = myNex.readNumber("n0.val");
Serial.print(myValue);

Sending a negative unsigned variable via Serial.print() you will see the overflow number for the negative integer, this is default for Serial.print()
Note:
Regardless of the value you see on a serial display, the value stored in the Arduino is the negative number you want.
Arduino will handle the variable as follows:

Math with unsigned variables may produce unexpected results, even if your unsigned variable never rolls over.
The MCU applies the following rules:
The calculation is done in the scope of the destination variable. E.g. if the destination variable is signed, it will do signed math, even if both input variables are unsigned.
However with a calculation which requires an intermediate result, the scope of the intermediate result is unspecified by the code. In this case, the MCU will do unsigned math for the intermediate result, because both inputs are unsigned!

unsigned int x = 5;
unsigned int y = 10;
int result;

result = x - y; // 5 - 10 = -5, as expected
result = (x - y) / 2; // 5 - 10 in unsigned math is 65530!  65530/2 = 32765

// solution: use signed variables, or do the calculation step by step.
result = x - y; // 5 - 10 = -5, as expected
result = result / 2;  //  -5/2 = -2 (only integer math, decimal places are dropped)

copied from here: https://www.arduino.cc/reference/en/language/variables/data-types/unsignedint/

commented

Thanks for all your help
Works now