Go Down

Topic: Problem with Serial.read and bigger numbers (Read 3141 times) previous topic - next topic

odometer

#15
Oct 07, 2013, 06:30 pm Last Edit: Oct 07, 2013, 07:12 pm by odometer Reason: 1
My mistake! Here's the corrected version:

Code: [Select]

unsigned long Eingabe(){
 unsigned long value = 0;
 char char_in = -1;
 while (true) {
    char_in = Serial.read();
   if (char_in != -1) {       // check to make sure there is a character
     if ((char_in >= '0') && (char_in <= '9')) {  // is it a digit?
       value *= 10;                               // multiply total by 10
       value += (char_in - '0');                  // add numeric value of new character
     }
     else return value;   // if the new character is not a digit, then we are done
   }
 }
}


NOTE: I fixed another stupid bug. And another.
I can't see shit with this new layout.

PaulS

That still doesn't address the issue of serial data trickling in slowly. It still assumes that the entire packet is available to be read when the function is called.
The art of getting good answers lies in asking good questions.

pbrouwer


That still doesn't address the issue of serial data trickling in slowly. It still assumes that the entire packet is available to be read when the function is called.


It's not really a problem if you can type way faster than the arduino can read.

Some terminal programs send stuff only after a linefeed is entered, it will mostly work in that case.
I agree checking for an end of transmission marker of some sorts is always a good idea, packing data in a verifiable format an even better one. It makes for a less challenging debugging experience, although it does take away the surprise factor.

Pieter

PaulS

Quote
It's not really a problem if you can type way faster than the arduino can read.

It is a problem UNLESS you can get serial data in faster than the Arduino can read it. And, that is not possible.

Quote
Some terminal programs send stuff only after a linefeed is entered, it will mostly work in that case.

So? Data is still sent, and received, one character at a time. Sssslllloooowwwwllllyyyy. Way slower than the Arduino can read it.

Quote
although it does take away the surprise factor.

You say that like that's a bad thing.  8)
The art of getting good answers lies in asking good questions.

pbrouwer

Quote
So? Data is still sent, and received, one character at a time. Sssslllloooowwwwllllyyyy. Way slower than the Arduino can read it.

Try it, you'll be surprised! (I guess there's some buffering going on, make sure the loop function has something to do or build in a delay)

Quote
You say that like that's a bad thing.  smiley-cool

It is. Writing / testing software that works as specified is really boring. Bugs create some excitement, especially if they come with bangs, flashes, sparks or smoke (or upset marketing people)!

Pieter

lar3ry


Try it, you'll be surprised! (I guess there's some buffering going on, make sure the loop function has something to do or build in a delay)

I did. At 19200, with a fairly small amout of code in loop(), the Arduino Uno, at 16 MHz, goes through the check for serial data 54 times between two characters, and that's at 19200 baud. And of course it goes through twice that number at 9600 baud.

Sure, you can do a whole lot of things in the loop, including adding totally useless delays, but why would you bother, when it's so easy to actually do it right, guaranteeing you get all the data you need?

Going ahead with multiple reads when you see Serial.available() > 0 is just foolish.

PaulS

Quote
Bugs create some excitement

Personally, I think you misspelled stress/unwanted attention/grief. Either that or you are one weird dude.
The art of getting good answers lies in asking good questions.

Raavgo

I read around quite a bit and figured out how to input 5 digits ( at least up to 32767, which is enough for my needs).

Code: [Select]

int frequenz;     
char Data[7];     
int i = 0;       

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

void loop()
{
  if( Serial.available())     
  {
    char ch = Serial.read();   //read serial
    if(i < 6 && isDigit(ch) )   //check if it is a digit
    {
      Data[i] = ch;                    //store it in data
      i++;
    }
    else
    {
      Data[i] = 0;                        //create end of string
      frequenz = atoi(Data);  //convert it to int
      i = 0;                                   
    }
  }
//do with the int whatever you want here.
// Max value = 32767



anyways thanks for the help
Raavgo

pbrouwer


Quote
Bugs create some excitement

Personally, I think you misspelled stress/unwanted attention/grief. Either that or you are one weird dude.

You can add frustration/despair/fear and quite a few other things to that, excitement comes in many disguises. I might have been a bit sarcastic here (and that could be the result of tracking down too many bugs).
Point I was trying to make is that it is really easy to make flawed code that seems to work fine, but then all of a sudden doesn't. It can create really weird behavior. And really: bugs should cause some commotion. If they don't, it means there's too many of them, or no-one cares about them being there.

Go Up