2

I am writing a very simple code in Arduino Nano, in which I am reading data that I send on Serial. Here is the code snippet.

int incomingByte ;   

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

void loop() {


    if (Serial.available() > 0) {

            incomingByte = Serial.read();


            Serial.print("I received: ");
            Serial.println(incomingByte);
    }

}

When I send 'A' , I receive the following output:

I received: 65

I received: 13

I received: 10

What is wrong with the working of Serial Port here?

3

nothing is wrong, it's to do with operator overloading and the print function trying to be helpful.

if you change this line:

int incomingByte ;

to this:

char incomingByte ;

it should display correctly

And the extra characters are because the software on the computer is sending line endings. these may be a linefeed with or without a carriage return. you could either just ignore them, or make use of them to create multi character commands (if you need them)

| improve this answer | |
  • hi, thanks for your input , but when I change it to char I still receive following :I received: A I received: I received: – skii Jul 24 '17 at 12:02
  • I don't want trailing carriage return and enter in my output, I simply need the character. How to trim ? – skii Jul 24 '17 at 12:03
  • If you don't want any trailing carriage returns, just ignore them – Michel Keijzers Jul 24 '17 at 12:21
  • @James Kent ... sorry for editing your answer (I moved it to my own answer). But still upvoted yours (for the inconvenience). – Michel Keijzers Jul 24 '17 at 15:01
3

You send a string "A"

However, to denote the end, a carriage line (code 13) + line feed (code 10) is sent.

So what is sent is actually an 'A' character followed by a return. This way you also know when a new line starts

Just strip off the return (13,10) items if you do not need them.

If you do not want to send the return, try sending 'A' (which is the character A, and not the string A.

Update

You can ignore them by:

     if ((incomingByte != 10 && (incomingByte != 13))
     {
        Serial.print("I received: ");
        Serial.println(incomingByte);
     }

If you only want printable characters, check if you can use the isprint function (isprint function

so it should look like

    if (isprint(incomingByte))
    {
        Serial.print("I received: ");
        Serial.println(incomingByte);
    }
| improve this answer | |
  • can you please tell me how to strip return (13, 10)? – skii Jul 24 '17 at 12:04
  • I added it initially accidentally to the wrong answer, now added it to my own. – Michel Keijzers Jul 24 '17 at 15:01
  • The line feed/carriage return is used to terminate the line, not the string. You can send the "A" all by itself (no delimiter); with a single cr or lf; or both (cr & lf). – user31481 Jul 24 '17 at 16:13
2

this is just because you are typing a letter i the serial monitor ans in your code your are declaring it as a integer 'int' so you not need to worry about that just try the following code

int incomingByte ;   

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

void loop() {


    if (Serial.available() > 0) {

            incomingByte = Serial.read();
            Serial.write(incomingByte);
    }
}

-------------------------------- OR ---------------------------------------

char incomingByte ;   

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

void loop() {


    if (Serial.available() > 0) {

            incomingByte = Serial.read();
            Serial.print(incomingByte);
    }
}

Both of the programs will work try that.......

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.