Start a new topic

Negative Numbers

Hi I have a NX4024T032 using arduino I have the current version of the library 0.9.0 I still can not put negative numbers in a number object. My nextion editor is up to date Do I need to update the firmware on my screen if so could you give me the link 

I am not sure how to provide the link to your local library directory on your computer

But if you installed the library properly, it may be within documents/Arduino folders.


Please look at NexVariable.cpp and also NexNumber.cpp

the .setValue function should be available for both

Hi Patrick

Would you have an example I use the .setValue function it will show + numbers but i get 65513 when I use a negative numbers.


Thanks


If you are using the Library, then you can only send and receive uint32_t ( UNsigned 32bit integers)
See below.
If you want to send negative numbers, you will have to modify the library (NexNumber.h and NexNumber.cpp) to accept signed integers, or send the values without using the library.

 

From NexNumber.cpp

bool NexNumber::setValue(uint32_t number)
{
    char buf[10] = {0};
    String cmd;
    
    utoa(number, buf, 10);   // <-- converting UNsigned to ascii.  itoa() would change signed..
    cmd += getObjName();
    cmd += ".val=";
    cmd += buf;

    sendCommand(cmd.c_str());
    return recvRetCommandFinished();
}

 

 

From NexNumber.h
    /**
     * Get number attribute of component.
     *
     * @param number - buffer storing text returned. 
     * @return The real length of text returned. 
     */
    bool getValue(uint32_t *number);
    
    /**
     * Set number attribute of component.
     *
     * @param number - number buffer. 
     * @return true if success, false for failure. 
     */
bool setValue(uint32_t number);

 

 

 

A negative number is determined by the signed bit.


for a signed byte, this is in the 7 bit with an unsigned value of 128

for a signed word, this is in the 15 bit with an unsigned value of 32768

for a signed long, this is in the 31 bit with an unsigned value of 2147483648


A little Math allows you to deal between signed and unsigned,

even less so as the MCU can be instructed to deal with this for you.



Login or Signup to post a comment