Start a new topic

setText from charArray[]

Hello everybody,


I got a weird problem with the setText function.


I'm getting some data from processing inside a char Array 


char Bottle[10];


Each Bottle[] countain a bottle name after a query inside the database from the processing.


It's already a char! Why I'm not able to display it inside the screen? 


Here is the part of the code i'm using from the processing respond :


 while (p.available()>0) {

    avl = true;


Bottle[1] = p.read();

Console.print(Bottle[1]);

Bottle[2] = p.read();

Console.print(Bottle[2]);

Bottle[3] = p.read();

Console.print(Bottle[3]);

Bottle[4] = p.read();

Console.print(Bottle[4]);

Bottle[5] = p.read();

Console.print(Bottle[5]);

 

t1.setText("test");

t2.setText(Bottle[1]);

t3.setText(Bottle[2]);

t4.setText(Bottle[3]);

t5.setText(Bottle[4]);

}


The first setText to t1 worked fine with "test" (it confirmed that the comm with screen is not the problem)

But t2 t3 t4 and t5 .. nothing or sometime few little dot.


I also tried with separated variable (not inside an array). Same thing...


the bottles name inside the array is written correctly. It printed correctly inside the Console.


Do I need to convert the data? Which format if not a char? I think there is something I'm doing wrong...


Thank you for your help




I think your MCU is going to run your code as written. In fact, you confirm it.



void loop(void) {

 while (p.available()>0) { 
    avl = true; 

Bottle[1] = p.read(); 
Console.print(Bottle[1]); 
Bottle[2] = p.read(); 
Console.print(Bottle[2]); 
Bottle[3] = p.read(); 
Console.print(Bottle[3]); 
Bottle[4] = p.read(); 
Console.print(Bottle[4]); 
Bottle[5] = p.read(); 
Console.print(Bottle[5]); 
 
t1.setText("test"); 
t2.setText(Bottle[1]); 
t3.setText(Bottle[2]); 
t4.setText(Bottle[3]); 
t5.setText(Bottle[4]); 
}

}

 

Examine what you wrote line by line (my assumption it is inside loop() function)

- line by line, what do you think is happening

This code is not inside the loop function. It's inside a Query routine. the process p is only available when something is written inside the console. 


The loop function only read the console and put it inside char c to became a sql query. 


The first part of the code to put the bottle name inside the Bottle[] array is perfect. I'm playing with that since long time without any problem. 


The problem came from the setText function. I tried a lot of different way but it never display correctly. One time, I used atoi to see what happen. The array was correctly converted to a INT number and this number display properly on the t2 field!


I already play a lot with getValue ans setValue and it work perfectly. It's the first time I'm playing with setText.


So I'm sure it's some kind of Datatype problem or the screen didn't recognize the data from the processing. 


I cannot convert it to a String or something other because setText only accept char or Int. I tried to read the process with a String and convert it with toInt but same result only few dot flick once on the t2 field. Not the expected text...


I really need this processing result from my database to come to the screen.


Let see the complete test code, 90% of it is the Nextion structure. There is a bug there, a char supposed to be written on the field!


Thank you

ino
(3.3 KB)

Again my process would be to list the code and ask

  - what you think happens line by line?


Flimsy argument that 90% of code is Nextion means bug is Nextion side

Some forum users may like the weekend challenge to debug other's code

 - but this is user MCU coding Error in Arduino.


When I set t1 to t5 to value without the other 10% non Nextion code does it not work?

 - so then I would have to argue it is the other 10% of code where error occurs.

Hey Sir, I'm asking question on a free chat forum it's to ask other people for the same problem with the Nextion screen. If you dont want to answer, dont do it.


I lost an entire week just because the screen don't want to receive a text from a variable. I didn't ask at the moment I'm getting a problem for the other to solve it. I'm asking everybody to help me understand what I didn't clearly understand with my problem.


It's with the Nextion screen that I tried to communicate, not the moon. I'm asking question inside a free chat Nextion Forum,because i'm using a Nextion screen. I think I'm at the right place. Yes lot of question can be solve with Arduino Forum and I'm sure everybody working with Nextion have both of them open inside their broswer.


After all my test I finally make the link of what it's wrong. The screen didn't recognize the text if it didn't have the guillemet. When it's time to use a variable it's impossible to use it alone. We have to add those " to it. When I realized that I made a search on arduino forum to know how to do it.


It is not Arduino that need that, it's NEXTION need them for text to be displayed but not with value. It explained why I never had this problem before.


Sometime just to talk to other help to resolve other problems.

@Stephane,


You have already pointed out the issues with your code and can't see your answer!


'sql' is defined as Arduino String class, and for sure you can construct and store Strings in an array exactly as you have it in your code. So why would setText() be expected to work when your rightly point out it needs a null terminated 'C' string array???


So you need to construct such a 'C' string with Console.read() and that is not as simple as writing 

sql += c;

  

State things correctly, and your answers become more correct

 - people will understand your problem better.


You did not lose a week because Nextion doesn't want to receive text from a variable.

 - Nextion can receive text from a variable as it did in t1.setText

 - if any strings are processed by Nextion, it is not Nextion, it is how you are parsing.


Your issue is in setting up what you are passing - this is an Arduino MCU code issue.

 - and I am a firm believer that

    - if I have an MCU issue with STM32F103 - ask ST

    - and if I have a compiler question with mikroPascal - ask mikroe

In this case you don't understand your programming language

   and the fastest place to get answers for your programming language is Arduino.cc

If you had understanding of your chosen programming language 

  - this conversation would not be occurring


The logic that I use Nextion so all my MCU questions need be Nextion forum

 - does that mean since DHT is in my project I should ask at DHT?

 - t1 showed not Nextion issue to accept, but error is in how t2 is handled.


I certainly understand less why Arduino users repel the notion to seek code help from Arduino

 - especially in the light that Arduino C++ is specific flavour of C not used elsewhere.


Furthermore .available > 0 is I'm my opinion formed poorly and questionable.

   if there is only 1 you have 5  .read()s causing reads before data is available.

   such causes undefined errors - perhaps even the type you are experiencing


What is the aversion to using technique demonstrated in CompText example

   which is included in the examples folder of Nextion library on your hard drive


or examining the NexText.h to discover it is looking for char *buffer as parameter

   bool setText(const char *buffer);

and examining the NexText.cpp setText function to see that double quote is parsed already.

    bool NexText::setText(const char *buffer) {

      String cmd;

      cmd += getObjName();

      cmd += ".txt=\"";

      cmd += buffer;

      cmd += "\"";

      sendCommand(cmd.c_str());

      return recvRetCommandFinished();

   }


Surely this takes less than a week

Last comment


Nextion needs t0.txt="Hello World"ÿÿÿ or page0.t0.txt="Hello World"ÿÿÿ

Declaring component t0 

   NexText myText = NexText(0,1,"page0.t0");

has done three quarters of the work for you as the code shows

   the page0.t0 is pulled from your component declaration

   the .txt= and leading double quote is added to this

   and after your parameter the trailing double quote is also added

   sendCommand() will append the needed trailing three byte 0xFF 0xFF 0xFF

  you only need to pass the pointer to where your null terminated string resides 


in one of several methods given in code and examples either via Iteadlib Library

or without the library as many users have done so many times already

The forum already contains 100 threads on .setText()

  - each looking for individual code debugging to their MCU side code

Not searching and reading any of these before looking for code debugging

     surely makes it a waste of time to type any answers

     as none of the already provided answers are read anyway.


  when t1.setText() has worked in the past for you

  (if it hasn't, you have followed and done none of the examples)

  follow through on the method that worked in the past already.


When you deviate from what works, your error is in the deviation.


Arduino also has Serial Monitor to pass print statements for debugging


So when

  - debugging via Serial Monitor unused

  - answers posted already in this forum and others (Arduino.cc the authority of Arduino)

  - code given to user, and indeed placed on local drive when lib installed

  - examples provided in the library example folder

  - and all other sources are ignored

Is there any reason to repost duplicate answers over and over again?

Oh, I guess I will provide a useful tip.


When code does not compile

 - look at the first line of the error messages

   only the first is useful, as others dependent on this first error may resolve

   automatically when first error is corrected.

 - take error such as "char to const char*" site:http://forum.arduino.cc

   Google or use Arduino.cc forum search for this error in double quotes

 - see how such was solved in the relevant threads


Line 5

      int text = "test";

       -- a string style constant will never be an integer.  Not even when they grow up.

          At best case possible, it results in a weird conversion,

          but "test5" will most likely exceed the two or four bytes of an int holder


What board and platform makes a HUGE difference when asking another to compile

 - when there are more than 100 boards in Board Manager, which one

No one can guess, (sorry, everyone can guess - but odds of being correct is slimmer)

 - every board comes with different set of available functions that are built in


I did all what you told before asking anything there! 

I'm using Arduino Mega 2560 with Yun Shield


I tried to read the process line by line inside different variable. It always print correctly inside the console but nothing inside the screen.


I saw that "" are already added but when I'm sending t1.setText("test") I saw test inside the screen but t1.setText(test) wont return the content of the variable test exept if it's something inside ""  inside this test variable...


I tried created a second variable and copy the reading from the process to it. The second char copy perfectly with the right value printed inside the Console but not inside screen. 


I'm not getting any error from the compiler.


There is something weird with that. I cannot explain my problem after several day of testing different solutions and reading several page on arduino forum.


Sometime it could be clear for one and totally not for other. It's why I posted a question there!


Finally the "" is not the answer to my problem because it didn't change anything to add more of them.


I didn't found any other thread about the same problem like mine... I did that search too...


So if the data is a char, have the good value inside (from the process confirmed by the console.print), the "" is already added by the nextion library, it suppose to display correctly no? 


the parameter to setText is const char *buffer

 - working with anything but is senseless


Serial.print() ... how does this give you the same parameter?

   Ponder - research

 - if it isn't strictly the same parameter

     - its use is not reflectively of pass or fail for use of const char *buffer parameter.


Where is your question on Arduino Forum?

  - I fail to believe google searches as suggested earlier have no results


Also truly find hard to believe that no thread leads to Reference Pages, or code examples.

https://www.arduino.cc/en/Reference/HomePage

  * dereference pointer in *buffer

  string - char array  

    https://www.arduino.cc/en/Reference/String


No compiler error

 - then MCU will do exactly as told by your code

 - circling back to my original, your code will run as written.


You must learn about reference and dereference pointers

You must learn about char arrays  Reference/String link above

   You have Arduino Mega and Yun

   more likely to be Arduino C++ to program in

You will need to become familiar with this language.


I placed 5 Text Components (and 1 font) in Nextion HMI

  - in preinitialize event bauds=115200 (my baud rate)

  my NexHardware nexInit() function has dbSerialBegin(115200) and nexSerial,begin(115200);


The following code toggles two lists of 5 every ~3 seconds using setText


 

#include "Nextion.h"

NexText bn1 = NexText(0,1,"t0");
NexText bn2 = NexText(0,2,"t1");
NexText bn3 = NexText(0,3,"t2");
NexText bn4 = NexText(0,4,"t3");
NexText bn5 = NexText(0,5,"t4");

char bottle1[10];
char bottle2[10];
char bottle3[10];
char bottle4[10];
char bottle5[10];

void otherfunctionA(void) {
  strcpy(bottle1,"Hello");
  strcpy(bottle2,"There");
  strcpy(bottle3,"World");
  strcpy(bottle4,"setText");
  strcpy(bottle5,"Works");  
  bn1.setText(bottle1);
  bn2.setText(bottle2);
  bn3.setText(bottle3);
  bn4.setText(bottle4);
  bn5.setText(bottle5);
}

void otherfunctionB(void) {
  strcpy(bottle1,"Apple");
  strcpy(bottle2,"Maple");
  strcpy(bottle3,"Palm");
  strcpy(bottle4,"Oak");
  strcpy(bottle5,"Cactus");  
  bn1.setText(bottle1);
  bn2.setText(bottle2);
  bn3.setText(bottle3);
  bn4.setText(bottle4);
  bn5.setText(bottle5);
}

void setup() {
  // put your setup code here, to run once:
  nexInit();
  Serial.print(test);
}

void loop() {
  // put your main code here, to run repeatedly:
  otherfunctionA();
  delay(3000);
  otherfunctionB();
  delay(3000);
}

 

bottle1[10] is a char array to hold 1 value,

The compiler initializes it to zeros in start

   zero is a null termination at end of string

   strcpy takes care of ensuring this

bottle1[10] has to be large enough to hold value + 1 for nul

   therefore max length of 9 in this fashion


Your previous code makes no null terminations

Regardless how you get your value into the char array

  the position after the last char would need be set to zero

This technique is indeed used in library code (located on your hard drive)


The remainder is up to you to learn and apply


Char is also just a byte

if the Linux on your Yun uses any other character sets other than the one

you have chosen for your HMI encoding (in Device Settings ie iso-8859-1)

then you will need to also be concerned with translating between charsets.


chars 32 to 126 known as lower case ascii is common amongst most.

Whatever falls outside of this range will most likely need attention

- especially if Linux on Yun is using UTF-8

Since we don't know what the screen looks like I would assume that the t1 field is on the screen somewhere and t2, t3, t4, and t5 are next to each other.  So by setting them it will spell out a work just like the console code does.  Since p.read() only reads one character each Bottle contains one character of the answer.


The problem as I see it is that the setText function wants a char array terminated by a null and not a character in an array.


setText("Test") and setText(Bottle) will work but the last character in Bottle[6] must be a 0x00.


This would mean there is no need for t2, t3, t4, and t5.  Just one text field 10 long.


When the only tool in your tool box is a hammer then all your problems look like a nail when really your problem is your thumb.


Login or Signup to post a comment