Arduino Forum

Forum 2005-2010 (read only) => General => Frequently-Asked Questions => Topic started by: coos on Jan 22, 2011, 05:13 pm

Title: About Serial.read
Post by: coos on Jan 22, 2011, 05:13 pm
The document show me that Serial.read returns the first byte of incoming serial data available .
ex:  I send "hello" to arduino,it returns me ony "h".
Is there any way to get more byte per one time?
thanks.
Title: Re: About Serial.read
Post by: AWOL on Jan 22, 2011, 05:14 pm
No.
Title: Re: About Serial.read
Post by: coos on Jan 22, 2011, 05:18 pm
:o
Title: Re: About Serial.read
Post by: AWOL on Jan 22, 2011, 05:26 pm
If you look at Serial.read, it returns an int.
The only reason it doesn't return a char is because if there is no data to read, it returns -1.
It can't return more than one characer at a time.

Is that a problem for you?
Title: Re: About Serial.read
Post by: pluggy on Jan 22, 2011, 05:31 pm
Hint : you read it more than once........
Title: Re: About Serial.read
Post by: coos on Jan 22, 2011, 05:34 pm
thank u for reply!
last question,i know it returns int,so how to change it to character in the arduino?
Title: Re: About Serial.read
Post by: AWOL on Jan 22, 2011, 05:42 pm
With a simple 'char' cast.
Title: Re: About Serial.read
Post by: coos on Jan 22, 2011, 05:48 pm
could you show me a example?
Title: Re: About Serial.read
Post by: coos on Jan 22, 2011, 05:50 pm
OK,I see now,thank u
Title: Re: About Serial.read
Post by: zoomkat on Jan 22, 2011, 06:00 pm
Below is some simple test code that captures the serial input into a string and sends it back to the serial monitor.


Code: [Select]
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
     Serial.begin(9600);
       Serial.println("serial test 0021"); // so I can keep track of what is loaded
       }

void loop() {

       while (Serial.available()) {
       delay(10);  
         if (Serial.available() >0) {
       char c = Serial.read();
       readString += c;}
       }
       
     if (readString.length() >0) {
     Serial.println(readString);
     
     readString="";
     }
  }
Title: Re: About Serial.read
Post by: cellage on Jan 23, 2011, 12:25 pm
int x;
char y;
y = char(x) ;

to convert from asci to char
Code: [Select]

char c;
c = Serial.read(); // assume it reads N
y = c -'0'; // or else it will be 78



does this help :-/?