Arduino Forum

Using Arduino => Programming Questions => Topic started by: SunnyBoy on May 13, 2012, 05:28 pm

Title: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 05:28 pm
Hello everyone,

I'm getting this problem that's driving me crazy, tried everything and googled for hours, now my last hope is this forum.

Here is the code:
Quote

int incomingByte = 0;   // for incoming serial data

void setup() {
        Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {

        // send data only when you receive data:
  while ( Serial.available( ) > 0 ) {

                incomingByte = Serial.read();
                
                // say what you got:
                Serial.print("I received: ");
                Serial.println(incomingByte);
                
  }
        delay(500);

}



When I enter an integer, I should get it back right?

Here is what I get when I enter "4" instead:

Serial monitor:

I received: 52
I received: 10

Here is "0":

Serial monitor:


I received: 48
I received: 10


What am I missing?

Please advise

Thanks
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 05:34 pm
You're missing the difference between the ASCII representation of a digit, and the digit itself.
Comes up about once a month here.
Title: Re: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 05:35 pm
I get that one, but it doesn't explain the systematic second line = 10.
It is the ASCII symbol for a new line and I don't know how to get rid of it.

Any ideas?
Title: Re: Serial.read crazy results
Post by: Nishant_Sood on May 13, 2012, 05:36 pm
You are going to get ASCII representation codes on Key stroke inputs , You will understand more on reading about ASCII values on respective Key Strokes, Google for it.
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 05:38 pm
Carriage return / line feed,
13 / 10
Title: Re: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 05:40 pm
and how can I fix this?
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 05:42 pm
What do you mean "fix"?
Title: Re: Serial.read crazy results
Post by: dxw00d on May 13, 2012, 05:43 pm
Use something different to input your data, that doesn't send CR/LF.
Title: Re: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 05:46 pm
thanks for your answer dxw00d, but man I'ma Newb :D

what you wrote is like chinese to me. could you please explain further? I'd be grateful to you

Thanks
Title: Re: Serial.read crazy results
Post by: dxw00d on May 13, 2012, 05:56 pm
How are you entering your "4"? Are you using the serial monitor?
Title: Re: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 05:59 pm
Yes
Title: Re: Serial.read crazy results
Post by: dkl65 on May 13, 2012, 06:09 pm
Maybe you can try Serial.write for incomingByte, and use if instead of while if you only need 1 byte. I might try it later.
Title: Re: Serial.read crazy results
Post by: dxw00d on May 13, 2012, 06:11 pm
There are two drop-downs at the bottom of the serial monitor. One of them selects what is sent at the end of whatever you type. It might say 'Newline', or 'Carriage return' currently. Try changing it to 'No line ending'.

If that doesn't work, you'll need a different terminal program.
Title: Re: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 06:18 pm
Works great! with: 'No line ending'

but now it reads only one digit. So if I type 10, I get :

I received: 1
I received: 0

what should I change?

PS: Thanks dxw00d :)
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 06:36 pm
Why not read the terminator....and ignore it?
Title: Re: Serial.read crazy results
Post by: dxw00d on May 13, 2012, 06:40 pm
Quote
but now it reads only one digit. So if I type 10, I get :


It will. You are reading one character and printing it back. If you want to read multi-character strings, you'll need to construct them in your sketch.
Title: Re: Serial.read crazy results
Post by: Arrch on May 13, 2012, 07:14 pm

Works great! with: 'No line ending'

but now it reads only one digit. So if I type 10, I get :

I received: 1
I received: 0

what should I change?

PS: Thanks dxw00d :)

You need start and stop bytes to know when you start receiving a new number and when you're done imputing an number in. Most people commonly use '<' and '>'. You read that into a buffer and then you can convert it to a number.
Title: Re: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 07:25 pm
how?
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 08:05 pm
Native American greeting?

You could search for PaulS's solution, which is quite popular.
Title: Re: Serial.read crazy results
Post by: dkl65 on May 13, 2012, 08:16 pm
Here my version of your "Arduino Parrot":
Quote

int incomingByte = 0;   // for incoming serial data
String inString = ""; //to store serial data
void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  while (Serial.available() > 0) {
    incomingByte = Serial.read();
    inString += (char)incomingByte; //add data to string
  }
  if (inString != ""){ //if the string isn't empty...
    // say what you got:
    Serial.print("I received: ");
    Serial.println(inString);
    inString = ""; //clear string for new input
  }
}


Tell me if it works! :)
Title: Re: Serial.read crazy results
Post by: SunnyBoy on May 13, 2012, 08:28 pm
Really nice of you to take the time to write this, but it outputs exactly the same result. each byte on a similar line.

I have the feeling that this function is the answer, but I can't implement it:

http://arduino.cc/en/Reference/StreamReadBytes
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 08:43 pm
You could search for PaulS's solution, which doesn't use String, and works.
Title: Re: Serial.read crazy results
Post by: Arrch on May 13, 2012, 08:52 pm
Here, this should help you do what you need, and more importantly, LEARN more about what you are trying to do:
Quote

/*
* Example for Serial2Int
* When reading from the serial monitor, there are two important things to note:
* (1) Bytes are read one at a time. So sending "246" will be read by your
* code as '2', then '4', then '6'. If you want to identify them as related in some
* way, you need a way to determine that. This example uses start and stop bytes.
* (2) Sending a number through the monitor sends it's ASCII representation, not
* the value itself. So typing 3 and hitting enter would send '3' or 51 as per the
* ascii table. To account for this, we will be using atoi(), which takes a null
* terminated array of chars, also known as a string, and produces the int equivalent.
*/

// To send a number through the serial monitor, put it between brackets
const char startByte = '<';
const char stopByte = '>';

// Maximum characters in an int + null terminated character
const short maxBuffer = 6;

void setup() {
 Serial.begin(57600);
 Serial.println("[Serial2Int]");
}

void loop() {
 // Stores the characters between the start and stop bytes
 static char buffer[maxBuffer];
 // Keeps track of spot in buffer
 static short index=0;
 
 if (Serial.available() > 0 ) {
   char inChar = Serial.read();
   
   if (inChar==startByte) { // If start byte is received
     index=0; // then reset buffer and start fresh
   } else if (inChar==stopByte) { // If stop byte is received
     buffer[index] = '\0'; // then null terminate
     processData(buffer); // and process the data
     index=0; // this isn't necessary, but helps limit overflow
   } else { // otherwise
     buffer[index] = inChar; // put the character into our array
     index++; // and move to the next key in the array
   }
   
   /* Overflow occurs when there are more than 5 characters in between
    * the start and stop bytes. This has to do with having limited space
    * in our array. We chose to limit our array to 5 (+1 for null terminator)
    * because an int will never be above 5 characters */
   if (index>=maxBuffer) {
     index=0;
     Serial.println("Overflow occured, next value is unreliable");
   }
 }
}

void processData(char buffer[]) {
 unsigned int value = atoi(buffer); // convert string to int
 Serial.print("Value: ");
 Serial.println(value);
}

Title: Re: Serial.read crazy results
Post by: dkl65 on May 13, 2012, 09:00 pm
Some more discussion: http://arduino.cc/forum/index.php/topic,105451.msg791114.html#msg791114. Or, you can use other people's solutions.... Who's solution worked, and where is PaulS's solution?
I want to try to do the same thing in a more simple way:
Quote

int incomingByte = 0;   // for incoming serial data
boolean written = false;
void setup() {
  Serial.begin(9600);     // opens serial port, sets data rate to 9600 bps
}

void loop() {
  // send data only when you receive data:
  if (Serial.available() > 0) Serial.print("I received: ");
  while (Serial.available() > 0) {
    incomingByte = Serial.read();
    Serial.print((char)incomingByte);
    written = true;
  }
  if(written){
    Serial.println();
    written = false;
  }
}


Will this work? I don't have time to try it. I'm also learning here!
P.S. If it doesn't work, first try changing Serial.print((char)incomingByte); to Serial.write(incomingByte);
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 09:44 pm
Quote
Will this work?

Is this a new parlour game?
Title: Re: Serial.read crazy results
Post by: dxw00d on May 13, 2012, 09:46 pm

Quote
Will this work?

Is this a new parlour game?

If it is, it's not a very good one.
Title: Re: Serial.read crazy results
Post by: dkl65 on May 13, 2012, 09:53 pm
I'm not playing games. What's a parlour game?
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 09:54 pm
A trivial or pointless diversion.
Title: Re: Serial.read crazy results
Post by: dkl65 on May 13, 2012, 09:59 pm
Are you implying something negative about my post?
Title: Re: Serial.read crazy results
Post by: AWOL on May 13, 2012, 10:03 pm
20 questions now?

Generally, and for very good reasons, it is best to use code tags, and not quote tags when posting code.
Title: Re: Serial.read crazy results
Post by: dkl65 on May 13, 2012, 10:09 pm
I think that you are making a pointless diversion from the actual topic and questions. ;) I pressed "Copy for Forum". That makes the code (not quote) colorful. :) And some questions are still unanswered. :(
Title: Re: Serial.read crazy results
Post by: AWOL on May 14, 2012, 09:38 am
If you think not using the correct posting technique is pointless, consider the following (contrived, I admit) example:
Quote

void setup ()
{
  for (int s = 0; s < S_MAX; ++s) {
    int pre = image ;
    for (int i = 0; i < I_MAX ; ++i) {
      int sub = table
  ;
      for (su = 0; su < SU_MAX; ++su) {
        table  = fn (image [su], 8);
      }
    } 
  } 
}

and here, in plain:
Code: [Select]
void setup ()
{
  for (int s = 0; s < S_MAX; ++s) {
    int pre = image [s];
    for (int i = 0; i < I_MAX ; ++i) {
      int sub = table [pre] [i] [u];
      for (su = 0; su < SU_MAX; ++su) {
        table [b] = fn (image [su][sub], 8);
      }
    }
  }
}

Title: Re: Serial.read crazy results
Post by: nickgammon on May 14, 2012, 09:44 am

I pressed "Copy for Forum". That makes the code (not quote) colorful. :)


Yes, <sigh>, we are trying to get rid of that.

Read this before posting a programming question (http://arduino.cc/forum/index.php/topic,97455.0.html)
Title: Re: Serial.read crazy results
Post by: PaulS on May 14, 2012, 11:29 am
Quote
Will this work? I don't have time to try it.

But you have time for half a dozen more posts on the forum? Get off the forum and try it yourself!
Title: Re: Serial.read crazy results
Post by: nickgammon on May 14, 2012, 11:41 am

Will this work? I don't have time to try it.


When people post a question I usually try it. It takes about 10 seconds. The forum is usually a lot slower to respond than that. And people might be asleep.

Quote
I'm also learning here!


You learn by trying things. Not by asking for the answers.
Title: Re: Serial.read crazy results
Post by: dkl65 on May 14, 2012, 05:48 pm
Quote
You learn by trying things. Not by asking for the answers.

Yes, I try things when I have time. Now, I will go do my homework.
Title: Re: Serial.read crazy results
Post by: AWOL on May 14, 2012, 07:00 pm
Quote
I will go do my homework.

And don't forget to tidy your bedroom.
Title: Re: Serial.read crazy results
Post by: dkl65 on May 14, 2012, 09:13 pm
Quote
And don't forget to tidy your bedroom.

No need to; I always keep it clean. :) Can we stop this "parlour game" or pointless diversion now, and get back on topic? Now, I can only do Arduino on weekends. I'll wait until July, and I can do it almost every day!
Title: Re: Serial.read crazy results
Post by: clvrmnky on May 15, 2012, 01:56 am

Quote
And don't forget to tidy your bedroom.

No need to; I always keep it clean. :) Can we stop this "parlour game" or pointless diversion now, and get back on topic?


There's a topic?

I like kittens.