Go Down

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


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?


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?


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


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


With a simple 'char' cast.


could you show me a example?



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.println("serial test 0021"); // so I can keep track of what is loaded

void loop() {

       while (Serial.available()) {
         if (Serial.available() >0) {
       char c = Serial.read();
       readString += c;}
     if (readString.length() >0) {
Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0


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