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 Answers 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)
-
hi, thanks for your input , but when I change it to char I still receive following :I received: A I received: I received:skii– skii2017年07月24日 12:02:22 +00:00Commented Jul 24, 2017 at 12:02
-
I don't want trailing carriage return and enter in my output, I simply need the character. How to trim ?skii– skii2017年07月24日 12:03:51 +00:00Commented Jul 24, 2017 at 12:03
-
If you don't want any trailing carriage returns, just ignore themMichel Keijzers– Michel Keijzers2017年07月24日 12:21:54 +00:00Commented Jul 24, 2017 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– Michel Keijzers2017年07月24日 15:01:26 +00:00Commented Jul 24, 2017 at 15:01
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);
}
-
can you please tell me how to strip return (13, 10)?skii– skii2017年07月24日 12:04:59 +00:00Commented Jul 24, 2017 at 12:04
-
I added it initially accidentally to the wrong answer, now added it to my own.Michel Keijzers– Michel Keijzers2017年07月24日 15:01:52 +00:00Commented Jul 24, 2017 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– user314812017年07月24日 16:13:15 +00:00Commented Jul 24, 2017 at 16:13
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.......