Pages: [1]   Go Down
Author Topic: About Serial.read  (Read 16202 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

 :o
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: January 22, 2011, 11:32:18 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hint : you read it more than once........
Logged


0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thank u for reply!
last question,i know it returns int,so how to change it to character in the arduino?
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25820
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With a simple 'char' cast.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

could you show me a example?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 23
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

OK,I see now,thank u
Logged

0
Offline Offline
Tesla Member
***
Karma: 141
Posts: 9470
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is some simple test code that captures the serial input into a string and sends it back to the serial monitor.


Code:
// 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="";
      }
   }
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

0
Offline Offline
Newbie
*
Karma: 0
Posts: 16
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int x;
char y;
y = char(x) ;

to convert from asci to char
Code:

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

 

does this help :-/?
Logged

Pages: [1]   Go Up
Jump to: