# Getting Wrong Output sending data through SERIAL PORT

**URL:** <https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340>\
**Category:** Programming\
**Created:** [November 9, 2022, 5:43am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340 "2022-11-09T05:43:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![rootofpi](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@rootofpi](https://forum.arduino.cc/u/rootofpi)\
**Post date:** [November 9, 2022, 5:43am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/1 "2022-11-09T05:43:07Z")

</div>

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 .

```cpp
#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

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

```

This is the slave code here, receiving end -

```cpp
#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

```cpp
Serial.println(number[5]);

```

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

The output should be 5

Thank You

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 9, 2022, 5:58am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/2 "2022-11-09T05:58:50Z")

</div>

> [@rootofpi](#):
>
> `String number[10];`

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

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [November 9, 2022, 8:01am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/3 "2022-11-09T08:01:37Z")

</div>

> [@rootofpi](#):
>
> ```arduino
> Serial.println(number[5]);
> 
> ```
> 
> The output should be 5 but i am getting output as `185`
> 
> The output should be 5

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?

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 9, 2022, 8:40am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/4 "2022-11-09T08:40:55Z")

</div>

> [@Grumpy\_Mike](#):
>
> I notice in the sending code you never set the number variable to anything.

are you sure?

> [@rootofpi](#):
>
> ```arduino
> for (int i=0;i<10;++i) {
> number[i] = i;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![rootofpi](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@rootofpi](https://forum.arduino.cc/u/rootofpi)\
**Post date:** [November 9, 2022, 8:55am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/5 "2022-11-09T08:55:50Z")

</div>

> [@rootofpi](#):
>
> ```arduino
> for (int i=0;i<10;++i) {
> number[i] = i;
> }
> 
> ```

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

```

I am inserting a number in array.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 9, 2022, 8:59am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/6 "2022-11-09T08:59:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rootofpi](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@rootofpi](https://forum.arduino.cc/u/rootofpi)\
**Post date:** [November 9, 2022, 9:03am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/7 "2022-11-09T09:03:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 9, 2022, 9:04am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/8 "2022-11-09T09:04:54Z")

</div>

> [@rootofpi](#):
>
> I need to store a alphanumeric later

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

---

<div class="post-metadata">

**Author:** ![rootofpi](https://avatars.discourse-cdn.com/v4/letter/r/c67d28/32.png) [@rootofpi](https://forum.arduino.cc/u/rootofpi)\
**Post date:** [November 9, 2022, 9:06am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/9 "2022-11-09T09:06:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 9, 2022, 9:06am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/10 "2022-11-09T09:06:56Z")

</div>

To practise try to replace the line

> [@rootofpi](#):
>
> `String number[10];`

to this one:

```arduino
byte number[10];

```

in both receiver and transmitter and test the new code.

---

<div class="post-metadata">

**Author:** ![Grumpy\_Mike](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/grumpy_mike/32/213521_2.png) [@Grumpy\_Mike](https://forum.arduino.cc/u/Grumpy_Mike)\
**Post date:** [November 9, 2022, 9:33am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/11 "2022-11-09T09:33:39Z")

</div>

> [@rootofpi](#):
>
> ```arduino
> for (int i=0;i<10;++i) {
> number[i] = i;
> }
> 
> ```
> 
> I am inserting a number in array.

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

> [@rootofpi](#):
>
> The output should be 5 but i am getting output as `185`  
> The output should be 5

the output is not expected to be 5.

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [November 9, 2022, 9:44am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/12 "2022-11-09T09:44:10Z")

</div>

> [@Grumpy\_Mike](#):
>
> The "i" is an int and you are inserting an int into a String. God knows what the compiler is doing with that.

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 8, 2023, 9:44am UTC](https://forum.arduino.cc/t/getting-wrong-output-sending-data-through-serial-port/1051340/13 "2023-05-08T09:44:55Z")

</div>

Closed on May 8, 2023, 9:44am UTC

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