# Serial Port Doesn't Work As Expected

**URL:** <https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176>\
**Category:** Programming\
**Created:** [January 3, 2020, 2:07pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176 "2020-01-03T14:07:21Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abran](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@Abran](https://forum.arduino.cc/u/Abran)\
**Post date:** [January 3, 2020, 2:07pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/1 "2020-01-03T14:07:21Z")

</div>

I'm trying to read a number from the serial port, but every time I enter a number, the Arduino reads the number and reads 0 after that. Can someone help me, please? (I'm using the Arduino Uno R3)

```cpp
int myNumber;
String msg1 = "Please enter your number";
String msg2 = "Your number is ";

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(msg1);
  while(Serial.available()==0){
    
  }
  myNumber = Serial.parseInt();
  Serial.print(msg2);
  Serial.println(myNumber);
  delay(1000);
}[code]

```

 ![serial monitor.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/c/9/9c96d3e2f4c8e2f76d933398291e0596819012d3.png)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 3, 2020, 2:14pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/2 "2020-01-03T14:14:00Z")

</div>

> every time I enter a number, the Arduino reads the number and reads 0 after that.

What have you got the Line ending set to in the Serial monitor ?

---

<div class="post-metadata">

**Author:** ![Abran](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@Abran](https://forum.arduino.cc/u/Abran)\
**Post date:** [January 3, 2020, 2:38pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/3 "2020-01-03T14:38:03Z")

</div>

There's a png to download at the bottom of the post.

 ![serial monitor.png](https://europe1.discourse-cdn.com/arduino/original/4X/9/c/9/9c96d3e2f4c8e2f76d933398291e0596819012d3.png)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 3, 2020, 2:41pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/4 "2020-01-03T14:41:42Z")

</div>

> Abran:  
> There's a png to download at the bottom of the post.

My question was intended as a clue to you as to what might be causing the problem

What do you suppose gets sent after the number that you enter ?

---

<div class="post-metadata">

**Author:** ![Abran](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@Abran](https://forum.arduino.cc/u/Abran)\
**Post date:** [January 3, 2020, 2:47pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/5 "2020-01-03T14:47:24Z")

</div>

A blank number?

---

<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:** [January 3, 2020, 2:54pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/6 "2020-01-03T14:54:57Z")

</div>

(deleted)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [January 3, 2020, 6:49pm UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/7 "2020-01-03T18:49:15Z")

</div>

> Abran:  
> A blank number?

Why would anyone be so perverse?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [January 4, 2020, 12:49am UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/8 "2020-01-04T00:49:30Z")

</div>

With Serial.parseInt() you have two choices:

1. Set 'line ending' to 'none' and let the .parseInt() time out (default 1 second).  
Or:
2. Set 'line ending' to 'newline' or some other setting that adds a non-digit at the end of the string.

Choice 2 is more responsive, since you don't have to wait for the timeout, but then you have to deal with the extra character.

This method works either way:

```cpp
void loop() {
  // put your main code here, to run repeatedly:
  while (Serial.available()) Serial.read();  // Empty the input buffer
  Serial.println(msg1);  // Prompt for input
  while(Serial.available() < 1){} // Wait until at least one character is available
  myNumber = Serial.parseInt();  // Read the number until timeout or non-digit
  Serial.print(msg2);
  Serial.println(myNumber);
  delay(1000);
}

```

---

<div class="post-metadata">

**Author:** ![Abran](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@Abran](https://forum.arduino.cc/u/Abran)\
**Post date:** [January 4, 2020, 1:11am UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/9 "2020-01-04T01:11:19Z")

</div>

I changed the serial monitor from Newline to No line ending, and it worked. Thanks, everyone!

---

<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 6, 2021, 12:13am UTC](https://forum.arduino.cc/t/serial-port-doesnt-work-as-expected/629176/10 "2021-05-06T00:13:45Z")

</div>

Closed on May 6, 2021, 12:13am UTC


