Receiving unwanted characters between LCD and Arduino

run this on your arduino and look at the Serial monitor

void setup() {
  Serial.begin(115200);
  for (int i = 0; i <= 255; i++) {
    Serial.print(i);
    Serial.write(' ');
    Serial.write(byte(i));
    Serial.println();
  }
}

void loop() {}

you'll see how each byte value is represented as a symbol. I get the symbol for any value strictly above 127

0
1 
2 
3 
4 
5 
6 
7 a
8 
9 	
10 

11 
12 
13 

14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 e
28 
29 
30 
31 
32  
33 !
34 "
35 #
36 $
37 %
38 &
39 '
40 (
41 )
42 *
43 +
44 ,
45 -
46 .
47 /
48 0
49 1
50 2
51 3
52 4
53 5
54 6
55 7
56 8
57 9
58 :
59 ;
60 <
61 =
62 >
63 ?
64 @
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
91 [
92 \
93 ]
94 ^
95 _
96 `
97 a
98 b
99 c
100 d
101 e
102 f
103 g
104 h
105 i
106 j
107 k
108 l
109 m
110 n
111 o
112 p
113 q
114 r
115 s
116 t
117 u
118 v
119 w
120 x
121 y
122 z
123 {
124 |
125 }
126 ~
127 
128 ⸮
129 ⸮
130 ⸮
131 ⸮
132 ⸮
133 ⸮
134 ⸮
135 ⸮
136 ⸮
137 ⸮
138 ⸮
139 ⸮
140 ⸮
141 ⸮
142 ⸮
143 ⸮
144 ⸮
145 ⸮
146 ⸮
147 ⸮
148 ⸮
149 ⸮
150 ⸮
151 ⸮
152 ⸮
153 ⸮
154 ⸮
155 ⸮
156 ⸮
157 ⸮
158 ⸮
159 ⸮
160 ⸮
161 ⸮
162 ⸮
163 ⸮
164 ⸮
165 ⸮
166 ⸮
167 ⸮
168 ⸮
169 ⸮
170 ⸮
171 ⸮
172 ⸮
173 ⸮
174 ⸮
175 ⸮
176 ⸮
177 ⸮
178 ⸮
179 ⸮
180 ⸮
181 ⸮
182 ⸮
183 ⸮
184 ⸮
185 ⸮
186 ⸮
187 ⸮
188 ⸮
189 ⸮
190 ⸮
191 ⸮
192 ⸮
193 ⸮
194 ⸮
195 ⸮
196 ⸮
197 ⸮
198 ⸮
199 ⸮
200 ⸮
201 ⸮
202 ⸮
203 ⸮
204 ⸮
205 ⸮
206 ⸮
207 ⸮
208 ⸮
209 ⸮
210 ⸮
211 ⸮
212 ⸮
213 ⸮
214 ⸮
215 ⸮
216 ⸮
217 ⸮
218 ⸮
219 ⸮
220 ⸮
221 ⸮
222 ⸮
223 ⸮
224 ⸮
225 ⸮
226 ⸮
227 ⸮
228 ⸮
229 ⸮
230 ⸮
231 ⸮
232 ⸮
233 ⸮
234 ⸮
235 ⸮
236 ⸮
237 ⸮
238 ⸮
239 ⸮
240 ⸮
241 ⸮
242 ⸮
243 ⸮
244 ⸮
245 ⸮
246 ⸮
247 ⸮
248 ⸮
249 ⸮
250 ⸮
251 ⸮
252 ⸮
253 ⸮
254 ⸮
255 ⸮

Mine is equal to yours less this values:

yeah - my point was that you don't really know what value is behind the so the line of code has zero chance to work

if(C != "⸮")

I agree, because the arduino don't know that symbol ⸮.

we would need to see all your code to understand what you are doing ...

It's a closed project, I can't post here yet. If you want I cound send by email. fabio.dma24@gmail.com

well, the point of the forum is to help and by doing so help others who could be stumbling upon the same roadblocks... so not so much about closed projects.

That's why I contribute here

May be you can recreate a minimal version of the code demonstrating what you are facing / trying to solve...

Sorry I understand the politics.
The problem is here somewhere.

void setup() {
  Serial.begin(115200);
  Serial.println("Serial monitor started");
  Serial3.begin(38400, SERIAL_8N1);         //Open serial port
}

void ReadNextionData() {
  String RX_Buffer = "";
  if (Serial3.available())
  {
    delay(100);
    while (Serial3.available())
    {      
      RX_Buffer += char(Serial3.read()); 
    }    
    Serial.println(RX_Buffer);
    AckLcdPage(RX_Buffer);        //Para o arduino saber em que página o lcd está    
    DataTreatmentLCD(RX_Buffer);  //Atualiza e trata os dados apenas da página onde está.    
  }
}

I think the problem it's not on the rest of the code, becaus the rest start after I received wich page is my Nextion lcd.

well we discussed that code already

if you get more than 64 bytes back on Serial3, the delay(100); will make sure you lose some

you should not try to second guess the timing of an asynchronous protocol

what you could do - if you don' want to handle that properly with end markers etc - would be to use one of the Stream function dealing with getting data with a timeout

readBytes()
readBytesUntil()
readString()
readStringUntil()

as you can get non ASCII characters, you might want to use one of the first two.


void ReadNextionData() {
  char buffer[101];
  if (Serial3.available()) {
    size_t count = Serial3.readBytes((byte*) buffer, 100);
    buffer[count] = '\0';
    Serial.println(buffer);

    String RX_Buffer = String(buffer);  // ==> si realmente necesitas una String... :-(
    AckLcdPage(RX_Buffer);              //Para o arduino saber em que página o lcd está
    DataTreatmentLCD(RX_Buffer);        //Atualiza e trata os dados apenas da página onde está.
  }
}


void setup() {
  Serial.begin(115200);
  Serial.println("Serial monitor started");
  Serial3.begin(38400, SERIAL_8N1);         //Open serial port}
  Serial3.setTimeout(250); // https://www.arduino.cc/reference/en/language/functions/communication/stream/streamsettimeout/
}

may be something like this (not sure how the String class will handle non ASCII bytes from the buffer)

I do some changes, but now I receive the data that way:

void ReadNextionData() {
  String RX_Buffer = "";
  char buffer[101];
  if (Serial3.available()) {
    size_t count = Serial3.readBytes((byte*) buffer, 100);
    buffer[count] = '\0';
    Serial.println(buffer);
    RX_Buffer = String(buffer);  // ==> si realmente necesitas una String... :-(    
    AckLcdPage(RX_Buffer);              //Para o arduino saber em que página o lcd está
    DataTreatmentLCD(RX_Buffer);        //Atualiza e trata os dados apenas da página onde está.
  }
}

In the serial port I print one caracter at the time:
image
But I want all in the string like this in one string.

"$30d&"

But when I do on the code, doesn't increment the string

RX_Buffer = String(buffer);  // ==> si realmente necesitas una String... :-(    

Careful, the 100 there was the usable size of the buffer in my code

It should be 9 for you so that you keep space for the trailing null char since your buffer has 10 slots.

Did you use

Or did you change that value?

If you see the prints going byte by byte it means the data is coming in very slowly

Try printing the count

void ReadNextionData() {
  String RX_Buffer = "";
  char buffer[101];
  if (Serial3.available()) {
    size_t count = Serial3.readBytes((byte*) buffer, 100);
    buffer[count] = '\0';
    Serial.print(“got “); Serial.println(count); Serial.print(“ bytes => “);
    Serial.println(buffer); 
    RX_Buffer = String(buffer);  // ==> si realmente necesitas una String... :-(    
    AckLcdPage(RX_Buffer);              //Para o arduino saber em que página o lcd está
    DataTreatmentLCD(RX_Buffer);        //Atualiza e trata os dados apenas da página onde está.
  }
}

I try with your code and timeout 250.
I got this data:

so you get lots of data that is not ASCII... I don't know enough about their protocol to know if that's to be expected

I don't know where the problem is, because the code I have on LCD when I enter the page is:

print "$"  
print "3"  
print "0"  
print "d"  
print "&" 

And I got this:

where does this come from? you understand that without seeing your code, you need to provide more info..

you still get the $30d&, but also tons of crap afterwards. May be there is a full buffer sent with the [] and three back question mark being field separators saying there is nothing there...

All the code on that page when initialize that page is:

print "$"  // start char
print "3"  // page no
print "0"  // send the ID
print "d"  // dummy char to match BT0
print "&"  // end char
va0.val=0
repo rxl.val,EEM_RXL
repo rxll.val,EEM_RXLL
repo disarm.val,EEM_DISARM
repo arming.val,EEM_ARM
repo bfccl.val,EEM_BFCCL
repo bfcl.val,EEM_BFCL
repo bdcl.val,EEM_BDCL
repo bdl.val,EEM_BDL
repo gpss.val,EEM_GPSS
if(rxl.val==1)
{
  RXL_Btn.val=1
}else if(rxl.val==0)
{
  RXL_Btn.val=0
}
if(rxll.val==1)
{
  RXLL_Btn.val=1
}else if(rxll.val==0)
{
  RXLL_Btn.val=0
}
if(disarm.val==1)
{
  DISARM.val=1
}else if(disarm.val==0)
{
  DISARM.val=0
}
if(arming.val==1)
{
  ARMING.val=1
}else if(arming.val==0)
{
  ARMING.val=0
}
if(bfccl.val==1)
{
  BFCCL.val=1
}else if(bfccl.val==0)
{
  BFCCL.val=0
}
if(bfcl.val==1)
{
  BFCL.val=1
}else if(bfcl.val==0)
{
  BFCL.val=0
}
if(bdcl.val==1)
{
  BDCL.val=1
}else if(bdcl.val==0)
{
  BDCL.val=0
}
if(bdl.val==1)
{
  BDL.val=1
}else if(bdl.val==0)
{
  BDL.val=0
}
if(gpss.val==1)
{
  GPSS.val=1
}else if(gpss.val==0)
{
  GPSS.val=0
}

I don’t know enough about their stuff to help out, sorry

The only page this not ocure it's one page that I send information from arduino to LCD. The rest of them, that only I send information from LCD to arduino I recieve garbage.

Well you receive binary data… you would have to dig into the protocol to see how to decode what you get

Hello thanks for all the help and suport.
I resolve the problem like this:

void ReadNextionData() {
  String RX_Buffer = "";
  int EndCom = 0; 
  int SaveData = 0; 
  String C = "";
  if (Serial3.available())
  {
    delay(100);
    while (Serial3.available() && EndCom == 0)
    {      
      C = char(Serial3.read());
      if(C == "$"){        
        SaveData = 1;
      }     
      if(C == " "){        
        SaveData = 0;
        EndCom = 1;
      }  
      if(SaveData == 1 && RX_Buffer != " ")
      {
        RX_Buffer += C;
      }
      if(C == "&"){ 
        SaveData = 0;       
        EndCom = 1;
      }         
    }    
    if(RX_Buffer != ""){
      Serial.println(RX_Buffer); 
    }      
  }
}

The caracter $ is the start of the save data and & the end of save data to RX_Buffer.
I chante the title of the topic, makes more sense. Thanks on more time.