# Serial communication = receiving wrong number

**URL:** <https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354>\
**Category:** Programming\
**Created:** [June 1, 2022, 7:19pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354 "2022-06-01T19:19:45Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![vermar](https://avatars.discourse-cdn.com/v4/letter/v/9fc348/32.png) [@vermar](https://forum.arduino.cc/u/vermar)\
**Post date:** [June 1, 2022, 7:19pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/1 "2022-06-01T19:19:45Z")

</div>

Hello!

I am trying to send a variable int (0-255) from an Node MCU V1 ESP 8266 to a Teensy 3.2.

My wiring is :  
ESP8266 TX --\> Teensy 9 (RX) ([Serial 2 on Teensy](https://www.pjrc.com/teensy/td_uart.html))  
ESP8266 RX --\> Teensy 10 (TX) (Serial 2 on Teensy)

I have used the code in [Serial input basics](https://forum.arduino.cc/t/serial-input-basics-updated/382007/2) but I am getting very strange results.

Basically I am receiving the 3 same numbers: 52 then 48 then 10 and it starts again.  
This is not at all what i am sending. I have tried sending a fixed value (400) and i still get the same result!

Help me please !!!

code ESP 8266 (sender)

```cpp
 
int val = 0;

void setup() {
  
  Serial.begin(9600);
 
}

void loop() {

     Serial.print(440);
       Serial.print('\n');
      
   
 }
    

```

code Teensy 3.2 (receiver)

```cpp
#include <Audio.h>
#include <SD.h>
#include <SerialFlash.h>

// set this to the hardware serial port you wish to use
#define HWSERIAL Serial2

bool S1 = false;
int val = 0; //my value

void setup() {
 
  Serial.begin(9600);
  Serial.print("teensy ready");
  HWSERIAL.begin(9600);
  

}

void loop() {
  
  receiveS1();
  showS1 ();

}

void receiveS1() {
  if (HWSERIAL.available() > 0) {
    val = HWSERIAL.read();

    S1 = true;
  }
}
  void showS1 () {
    if (S1 == true) {
     Serial.println("receving:");
      Serial.println(val, DEC);
      S1 = false;
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [June 1, 2022, 7:25pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/2 "2022-06-01T19:25:23Z")

</div>

> [@vermar](#):
>
> ` Serial.println(val, DEC);`

You are printing the decimal value of what is received. IS this not what you are seeing?

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [June 1, 2022, 8:01pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/3 "2022-06-01T20:01:26Z")

</div>

> [@vermar](#):
>
> `val = HWSERIAL.read();`

```arduino
val = HWSERIAL.parseInt();

```

---

<div class="post-metadata">

**Author:** ![vermar](https://avatars.discourse-cdn.com/v4/letter/v/9fc348/32.png) [@vermar](https://forum.arduino.cc/u/vermar)\
**Post date:** [June 1, 2022, 10:23pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/4 "2022-06-01T22:23:31Z")

</div>

I am receving : (with ```  
Serial.println("receving:");

```cpp

receiving:
48
receiving:
52
receiving:
10

in a loop, 
I should receive 
440
440
440
```

---

<div class="post-metadata">

**Author:** ![vermar](https://avatars.discourse-cdn.com/v4/letter/v/9fc348/32.png) [@vermar](https://forum.arduino.cc/u/vermar)\
**Post date:** [June 1, 2022, 10:24pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/5 "2022-06-01T22:24:04Z")

</div>

will try this and report back tomorrow,  
thanks 🙂

---

<div class="post-metadata">

**Author:** ![blh64](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blh64/32/605986_2.png) [@blh64](https://forum.arduino.cc/u/blh64)\
**Post date:** [June 1, 2022, 11:19pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/6 "2022-06-01T23:19:55Z")

</div>

> [@vermar](#):
>
> `Serial.print(440);`

the print() function turns your number into the ASCII representation so 440 turns into "440" which is a string of 3 chars with whose value is  
'4' == 0x34 (hex) == 52 (decimal)  
'0' == 0x30 (hex) == 48 (decimal)

and  
'\n' == 0x0A (hex) == 10 (decimal)

So it looks like you are getting what you should be.

---

<div class="post-metadata">

**Author:** ![vermar](https://avatars.discourse-cdn.com/v4/letter/v/9fc348/32.png) [@vermar](https://forum.arduino.cc/u/vermar)\
**Post date:** [June 1, 2022, 11:37pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/7 "2022-06-01T23:37:11Z")

</div>

that makes a lot of sense! thanks!

What should i use to get 440?

```cpp
Serial.println(val); // OR 
Serial.parseInt(); 

```

---

<div class="post-metadata">

**Author:** ![Paul\_KD7HB](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_kd7hb/32/1127958_2.png) [@Paul\_KD7HB](https://forum.arduino.cc/u/Paul_KD7HB)\
**Post date:** [June 1, 2022, 11:48pm UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/8 "2022-06-01T23:48:03Z")

</div>

> [@vermar](#):
>
> ` val = HWSERIAL.read();`

You are receiving a SINGLE byte whose decimal value cannot exceed 256.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [June 2, 2022, 7:37am UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/9 "2022-06-02T07:37:48Z")

</div>

bytewise reading is like that:

```cpp
void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    byte Char = HWSERIAL.read();
    if (Char > 47 && Char < 58) val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![vermar](https://avatars.discourse-cdn.com/v4/letter/v/9fc348/32.png) [@vermar](https://forum.arduino.cc/u/vermar)\
**Post date:** [June 2, 2022, 8:44am UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/10 "2022-06-02T08:44:40Z")

</div>

Ok, I have it working now, thank you everyone.  
Just a bit confused how that all works.

I have (to send)

```cpp
void setup() {
  
  Serial.begin(9600);
 
}

void loop() {
     Serial.print(255);
     Serial.print('\n'); 
     delay(100); 
     Serial.print(0);
     delay(100);  
     Serial.print('\n'); 
 }

```

and to receive :

```cpp
void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    val = HWSERIAL.parseInt();
    Char = HWSERIAL.read();
    if (Char > 47 && Char < 58) val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

  void showS1 () {
    if (S1 == true) {
     Serial.println("receving:");
      Serial.println(Char);
       Serial.println("val:");
      Serial.println(val);
      S1 = false;
    }
  }

```

In the serial monitor i get  
receiving:  
10  
val:  
255  
receiving:  
10  
val:  
0

So `parseInt()` returns my value but the byte reading

```cpp
void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    byte Char = HWSERIAL.read();
    if (Char > 47 && Char < 58) val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

```

returns 10 (which if i understand is the linebreak? `'\n' == 0x0A (hex) == 10 (decimal)`

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [June 2, 2022, 8:52am UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/11 "2022-06-02T08:52:32Z")

</div>

> [@vermar](#):
>
> ```arduino
> void receiveS1() {
> if (HWSERIAL.available() >= 1) {
> val = HWSERIAL.parseInt();
> Char = HWSERIAL.read();
> if (Char > 47 && Char < 58) val = val * 10 + (Char - 48);
> else S1 = true;
> }
> }
> 
> ```

this is nonsence. use one of the options

```arduino
void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    val = HWSERIAL.parseInt(); //this will get complete integer number until not digit character comes
     S1 = true;
  }
}

```

or

```arduino
void receiveS1() {
  if (HWSERIAL.available() >= 1) {
    Char = HWSERIAL.read();
    if (Char > 47 && Char < 58) val = val * 10 + (Char - 48);
    else S1 = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![vermar](https://avatars.discourse-cdn.com/v4/letter/v/9fc348/32.png) [@vermar](https://forum.arduino.cc/u/vermar)\
**Post date:** [June 2, 2022, 9:22am UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/12 "2022-06-02T09:22:31Z")

</div>

> [@kolaha](#):
>
> ```arduino
> void receiveS1() {
> if (HWSERIAL.available() >= 1) {
> Char = HWSERIAL.read();
> if (Char > 47 && Char < 58) val = val * 10 + (Char - 48);
> else S1 = true;
> }
> }
> 
> ```

Yes, I realised this now.

Thank you

---

<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:** [November 29, 2022, 9:22am UTC](https://forum.arduino.cc/t/serial-communication-receiving-wrong-number/998354/13 "2022-11-29T09:22:40Z")

</div>

Closed on November 29, 2022, 9:22am UTC

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