Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« on: December 21, 2012, 01:25:46 pm » |
Not clear the meaning of Serial.available() function. Can someone help!?
thanks
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #1 on: December 21, 2012, 01:29:07 pm » |
It tells you how many characters are available in the receive buffer for you to read.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9401
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: December 21, 2012, 02:25:45 pm » |
See - http://arduino.cc/en/Serial/Available - Tip: never read more bytes (chars) than Serial.available() indicates 
|
|
|
|
|
Logged
|
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #3 on: December 22, 2012, 05:43:14 am » |
So, typing: if (Serial.available()==3){ ... } Arduino gets data from computer, if you insert three characters only? is that correct?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: December 22, 2012, 06:23:10 am » |
Yes, but the problem is, if you don't check often enough, you may find that you have found four characters available, and the test will fail. >= 3 would be a better test.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #5 on: December 22, 2012, 07:55:22 am » |
Thanks a lot! but, sorry, for me it's still a bit misleading... //in this case: void loop(){ if (Serial.available()>2){ int inByte=Serial.read(); Serial.println(inByte); } }
/* after uploading the sketch, typing a number of char, in the serial window, lower than 3, no replies are obtained. actually, after 4 or more characters are entered, even if just new one is typed, Arduino sends the corresponding ASCII code back. How can I change the sketch, so only if 2 or more characters are entered, replies are sent, at any moment? I hope i've been clear...*/
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #6 on: December 22, 2012, 08:20:32 am » |
typing a number of char, in the serial window, lower than 3, no replies are obtained. Think about what you have. Imagine you start "loop" with three characters in the buffer. Think about what will happen to Serial.available after you have read out one character.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #7 on: December 22, 2012, 08:23:13 am » |
/* after uploading the sketch, typing a number of char, in the serial window, lower than 3, no replies are obtained. The Arduino does not have a serial window. The PC has an application called Serial Monitor that the Arduino IDE can launch. That Serial Monitor application may, or may not append data to the serial stream it is sending, depending on what (unspecified) option you have selected. That application does not send data as you type. It sends data only when you press the enter key or press the send button. It would be really helpful if you would explain what application you are using to send the data, what options you have set, and how, exactly, you are sending the data.
|
|
|
|
|
Logged
|
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #8 on: December 22, 2012, 10:33:45 am » |
It would be really helpful if you would explain what application you are using to send the data, what options you have set, and how, exactly, you are sending the data. To send data to Arduino, i use the serial monitor launched from the Arduino IDE. My purpose is to send ASCII codes (so composed by one ore more digits) to Arduino and get back the corresponding character....is it possible...? Think about what you have. Imagine you start "loop" with three characters in the buffer. Think about what will happen to Serial.available after you have read out one character. I'm not able to figure it out...no ideas..
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: December 22, 2012, 10:42:04 am » |
I'm not able to figure it out. Imagine you start "loop" with three characters in the buffer. You see that Serial.available returns three. Three is greater than two, so you read one character out. "loop" exits, gets called again, and you call Serial.available again What happens next?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #10 on: December 22, 2012, 10:57:03 am » |
The best ratio of answers to questions is 1. Higher than 1 is much better than lower than 1. Now, we know that you are using the Serial Monitor application, but not what options you have set, what you are sending or when. My purpose is to send ASCII codes (so composed by one ore more digits) to Arduino and get back the corresponding character....is it possible...? Yes, it is. It is quite easy, in fact. There are two ways to go about it. One is to always send three characters, and hope like hell none get lost (not a realistic thing to do). The other is to send the value followed by a delimiter. The Serial Monitor makes this option easy. Then, you read the data on the Arduino, storing it in a NULL terminated char array until the delimiter arrives. When that happens, you convert the array contents to an int, using atoi(), send a response, and reset the array index to 0 and put a NULL in that position of the array. Though why you want to do this, versus looking in an ASCII table, is unclear.
|
|
|
|
|
Logged
|
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #11 on: December 22, 2012, 01:13:44 pm » |
Imagine you start "loop" with three characters in the buffer. You see that Serial.available returns three. Three is greater than two, so you read one character out. "loop" exits, gets called again, and you call Serial.available again What happens next? void setup(){ Serial.begin(9600); }
void loop(){ if(Serial.available()>2){ char inBytes=Serial.read(); Serial.println(inBytes); } }
first three characters typed: "567" Serial returns "5" (as you said) going on with serial input, for example with "sg89" i get "67sg" ecc... so, opening for the very first time the serial monitor, while less than 3 char are typed, no signal is returned. When 3 or more char are entered, serial data began to flow from Arduino to pc. Buffer does not reset each time the loop() cycle begin, for this reason, just when i open for the first time the serial port, this happens. Right?... my goodness, i'm sweating...
|
|
|
|
« Last Edit: December 22, 2012, 01:22:39 pm by gianlucalongoni »
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 138
Posts: 19066
I don't think you connected the grounds, Dave.
|
 |
« Reply #12 on: December 22, 2012, 01:33:26 pm » |
I don't see anything in your observations that I don't expect. What are you expectations?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 313
Posts: 35507
Seattle, WA USA
|
 |
« Reply #13 on: December 22, 2012, 01:57:47 pm » |
When 3 or more char are entered, serial data began to flow from Arduino to pc. No, it does not. No serial data is sent until you press the Send button or hit the enter key. Buffer does not reset each time the loop() cycle begin True. It would be useless if it did. just when i open for the first time the serial port, this happens. No. The buffer is cleared because opening the serial port resets the Arduino. The buffer is set up after the reset, empty. my goodness, i'm sweating... You wouldn't be here. It's freezing.
|
|
|
|
|
Logged
|
|
|
|
|
Casorezzo, Milan, Italy
Offline
Newbie
Karma: 0
Posts: 22
|
 |
« Reply #14 on: December 23, 2012, 05:35:57 am » |
When 3 or more char are entered, serial data began to flow from Arduino to pc. No, it does not. No serial data is sent until you press the Send button or hit the enter key. Yep, sorry, i meant "after pressing" enter key... I don't see anything in your observations that I don't expect. What are you expectations? i was trying to get the meaning of Serial.available function, and what seem to be obvious statements, for my experience, are enormous obstacles! Thanks foor helping me, Your all advices have been precious for me!
|
|
|
|
|
Logged
|
|
|
|
|
|