Problems sending multiple strings to a Nextion display.

So im building a system wich recieves 10 bytes of data from a robotic cameraplatform every 33 milliseconds. The data is read from a serial port and then processed and sent as String via another serial port to a nextion display.
The problem is as soon as i try to send more than 2 Strings in a single loop, the data recieved by the display is screwed up and wrong. Anyone knows why this is?

I use the following code, and yes i know the variable calculations are uggly as shit…

void setup() {

// put your setup code here, to run once:
Serial.begin(9600);
Serial1.begin (2400);
Serial1.flush();

}

void loop() {

// put your main code here, to run repeatedly:

if (Serial1.available()>10)
{
int inData[10];
int i=0;

for (i=0; i<=9; i++)
{
inData*=Serial1.read();*

  • }*

  • int azLSB = inData[0];*

  • int azMSB = inData[1];*

  • int elLSB = inData[2];*

  • int elMSB = inData[3];*

  • int styrning =inData[8];*

  • String styr;*

  • unsigned int azTemp = (azMSB<<8) | azLSB;*

  • unsigned int elTemp = (elMSB<<8) | elLSB;*

  • long azTemp2 = azTemp-32768;*

  • long elTemp2 = elTemp-32768;*

  • float faktor = (360.000/65536.000);*
    _ float azTemp3 = (azTemp2 * faktor);_
    _ float elTemp3 = (elTemp2 * faktor);_

  • float azNextion = -azTemp3;*

  • float elNextion = elTemp3;*

  • String stringAz =String(azNextion,3);*

  • String stringEl = String(elNextion,3);*

  • if (styrning == 1)*

  • {*

  • styr = (“Joystick”);*

  • }*

  • if (styrning == 2)*

  • {*

  • styr = (“Radar”);*

  • }*

  • if (styrning == 3)*

  • {*

  • styr = (“UNIPOS”);*

  • }*

  • if (styrning == 4)*

  • {*

  • styr = (“Prediktor”);*

  • }*

  • Serial.print(“t2.txt=”);*

  • Serial.print(""");*

  • Serial.print(stringAz);*

  • Serial.print(""");*

  • Serial.write(0xFF);*

  • Serial.write(0xFF);*

  • Serial.write(0xFF);*

  • Serial.print(“t3.txt=”);*

  • Serial.print(""");*

  • Serial.print(stringEl);*

  • Serial.print(""");*

  • Serial.write(0xFF);*

  • Serial.write(0xFF);*

  • Serial.write(0xFF);*

  • Serial.print(“t4.txt=”);*

  • Serial.print(""");*

  • Serial.print(styr);*

  • Serial.print(""");*

  • Serial.write(0xFF);*

  • Serial.write(0xFF);*

  • Serial.write(0xFF); *
    }
    }

Serial1.begin (2400);
Serial1.flush();

RTFM. flush() waits until the outgoing buffer is empty. begin() initializes the outgoing buffer. flush() does NOTHING there, so you look stupid calling it.

if (Serial1.available()>10)
{
  int inData[10];
  int i=0;

If you are going to read 10 characters, you should use >=, not wait for 11 characters to arrive before you read 10.

Since you are reading bytes from the serial port, you should store them in a byte array, not waste memory using an int array.

There is NO reason to convert the floats to Strings. Stop pissing away resources unnecessarily. Serial.print() is perfectly capable of dealing with floats.

At 9600 baud, how long does it take to shuffle all that data out the serial port? Are you loosing incoming data while you are doing that?

Is the Nextion THAT slow?

@PaulS Thanks for replying. i know the different variable conversions are stupid, i had some trouble getting the nextion to accept the data so i tried different datatypes in an attempt to find what the problem was.

Can you do the same calculations with a byte array as an int? im fairly new to programming and this was the first way to pop up in my head.

Im fairly certain the problem lies in the outgoing buffer, or mabye the nextions ingoing buffer. i changed the code so that one of the Serial.print() commands only triggers once every 500 loops. this solved the problem. its not pretty but it works.

Can you do the same calculations with a byte array as an int?

No. Arrays and scalar variables are completely separate things. You CAN do the same calculations with elements of a byte array that you do with the elements of an int array.