Getting Wrong Output sending data through SERIAL PORT

I am trying to send data from one esp32 to another esp32. But when the receiving end, esp32, receives data, it is something different, so I am not getting how the data is changed there or what I am doing wrong.
Here is my code master which is send data .

#include <HardwareSerial.h>

HardwareSerial SerialPort(2);

String number[10];
void setup()  
{
  Serial.begin(115200);
  SerialPort.begin(15200, SERIAL_8N1, 16, 17); 
  for (int i=0;i<10;++i) {
      number[i] = i;
  }

  
    Serial.println(number[5]);
} 
void loop()  
{
 for(int i=0;i<9;++i) {
    SerialPort.print(number[i]); 
  } 
  
  
}

At void setup, I stored an array with an int and tested with Serial.println(number[5]); and I could verify that the data is present in that.

and at void loop i am sending a data

for(int i=0;i<9;++i) {
    SerialPort.print(number[i]); 
  }

This is the slave code here, receiving end -

#include <HardwareSerial.h>
HardwareSerial SerialPort(2);
String number[10];
void setup() {
  Serial.begin(115200);
  SerialPort.begin(15200, SERIAL_8N1, 16, 17); 
  
}
 
void loop() {
  if(SerialPort.available()) {
    for (int i=0;i<9;++i) {
      number[i] = SerialPort.read();
    }
  }
  Serial.println(number[5]);
}

Here I am checking if the data is received and, if yes, storing it in the array. but when I print

Serial.println(number[5]);

The output should be 5 but i am getting output as 185

The output should be 5

Thank You

If this a number, why do you defined an array as String?

No it shouldn't, it should be the fifth byte in the string ( start counting from zero)

I notice in the sending code you never set the number variable to anything. All the line does is declared an empty array.

Do you know about arrays?

are you sure?

for (int i=0;i<10;++i) {
      number[i] = i;
  }

I am inserting a number in array.

You did not answer to the question - why the array defined as String, if you intended to store integers?

I was practising I need to store a alphanumeric later so before doing that i was playing with this.

You can't use alphanumeric with this code because it reading only one byte to array element.

Yes exactly. I also figured out this is there anything i can do so that whole alphanumeric go at once. Literally confused on this one.

To practise try to replace the line

to this one:

byte number[10];

in both receiver and transmitter and test the new code.

No you are not. The "i" is an int and you are inserting an int into a String. God knows what the compiler is doing with that.
This is why

the output is not expected to be 5.

1 Like

You are right.
Class String doesn't redefine "=" operator...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.