Go Down

Topic: About Serial.read (Read 18421 times) previous topic - next topic

coos

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.

AWOL

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


AWOL

#3
Jan 22, 2011, 05:26 pm Last Edit: Jan 22, 2011, 05:32 pm by AWOL Reason: 1
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?
"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.

pluggy

Hint : you read it more than once........
http://pluggy.is-a-geek.com/index.html

coos

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

AWOL

With a simple 'char' cast.
"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.

coos

could you show me a example?

coos


zoomkat

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="";
     }
  }
Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

cellage

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 :-/?

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy