serial available()

hi

i have been playing with the serial port, used an example programme and modified a little but i have been thinking...

i understand that serialbegin() sets the baud rate but why exactly is the serial available doing? i have looked around but nothing actually says what this is doing. in the context i've seen it in, says serial available > 0 so i guess this means it is looking for some sort of data or something. i'm not a computer genius so in simple terms what is this command really doing??

you'd think that serialread() is really just doing the same thing just reading the serial port. you could just say serialread and be done with it, if a zero or a zero V is coming down the serial port the programme would just ignore it? i just don't know!!

as a final question...

the arduino language is like c. i've bought the getting started with arduino book and another book but is there some online FREE training course to help me understand the language? i have done a C++ course a few years ago and this has helped me a lot with understanding the arduino language. the arduino language has its own commands distinctive to it.

as a final point, the arduino is an amazing piece of equipment and have been telling anyone who will listen to me about it!!!

ps

i was reading that the arduino understands when a series of commands are being used to run continuously like pwm on a motor. normally the programme moves from top to bottom runnnig each command sequentially. if you had the programme putting out pwm signals if delays were written in then by rights the whole programme would otherwise stop sending signals to run a motor. the arduino is smart enough to understand that you intend the programme to keep sending pwm signals to a motor regardless. i haven't written anything up yet to see if this really is the case!!

the beauty of the arduino is that there is fanatastic backup, it simplifies the whole process of controlling physical stuff, you don't need to have studied computing programming for 20 years to know enough stuff to make something work. other products are truly nightmarish.

thanks again

as a further very final point i suppose it wouldn't be bad idea starting an arduino club in my home town, theres at least one other person i have introduced it to that seem to be very enthused over it. face to face contact and meetings could be a good learning tool?

why exactly is the serial available doing?

I presume you mean "What is the". Incoming serial data is stored in a ring buffer. The Serial.available() function returns the number of bytes of data that are in the buffer. In other words, it tells you whether there is data to be read, and how much.

Serial.read(), when there is no data to read, returns a -1, so you know that the value is not valid. So, theoretically, the Serial.available() call is not required, but it makes looping to collect all the serial data much easier.

the arduino language is like c.

That's because it is. Well, C++, which is a super-set of C.

the arduino language has its own commands distinctive to it.

No. The Arduino has its own libraries and functions.

right,

so when using serialavailable() this is really making the arduino being aware that information is in a memory storage.

the "if serialavailable()>0" command is telling the arduino to pick off data - if there's something in there. otherwise if you tell the arduino to serialread() it will start picking off no data ( a -1) value i guess, probably taking time and power to do it.

if you use the "if serialavailable()>0" command right at the start of the programme, when the serialread() is used, there really will be some data in the buffer (i understand the buffer as a memory location that stores information coming in quickly - the data is then taken from it at the arduinos leisure. as the data is read it frees up space for new information coming in - like a register that shifts the data along before dumping it at the end?)

the serialavailable() doesn't tell me anything as such but tells the arduino? when i use the serialread() command, the information on the buffer is read and taken into the arduinos programme and it does something with it.

i suppose one way to find out is to delete the serialavailable from my programme and see what happens..?

kind of makes sense now, i worry when i use commands that i don't understand, initially i use them to make something work but i then go back and try and understand what they do and why

the "if serialavailable()>0" command is telling the arduino to pick off data

No. The available() function is reporting whether there is data to pick off.

available() returns a value. That value is the number of bytes in the buffer (that can be read). It does not alter the buffer in any way. Typically, the returned value is not stored. It is simply tested, using if or while.

read() removes the first byte from the buffer, and returns that byte. If the buffer was empty, read() fails, and returns a -1 to indicate failure.

if you use the "if serialavailable()>0" command right at the start of the programme, when the serialread() is used, there really will be some data in the buffer

No. Using available() will not make data available. It simply reports whether or not there is data available.

It's like checking your e-mail. You can tell whether or not there is e-mail to read.

If there is, you read one e-mail. Then, if there is still e-mail to read, you read another one.

ok gotcha

Serial.available() is reporting that data is or isn't there (to the programme/ arduino)

they have used this command....

if (Serial.available()>0)
{
............
stuff involving serialread()
.............
}

because they are being choosy they only want data with ones in it not all zeroes (no data), the programme doesn't want to read the buffer if no data is in it.

because they are being choosy they only want data with ones in it not all zeroes (no data), the programme doesn't want to read the buffer if no data is in it.

If you perform a "read" when there is no data in the buffer, the "read" will return -1 (all ones).

A sidebar on the very useful "serial.available":

It isn't provided in the SoftwareSerial library.... but fear not... it IS in the NewSoftSerial library, and the latter is not a hassle to install or use.

i was reading that the arduino understands when a series of commands are being used to run continuously like pwm on a motor. normally the programme moves from top to bottom runnnig each command sequentially. if you had the programme putting out pwm signals if delays were written in then by rights the whole programme would otherwise stop sending signals to run a motor. the arduino is smart enough to understand that you intend the programme to keep sending pwm signals to a motor regardless. i haven't written anything up yet to see if this really is the case!!

The PWM output is performed in hardware (electronics), and once initiated (e.g. by an analogWrite) it will continue for as long as the Arduino is powered. It isn't anything special in the way it executes programs.

HTH
GB-)

because they are being choosy they only want data with ones in it not all zeroes (no data), the programme doesn't want to read the buffer if no data is in it.

Not quite. A valid character can be received that is all zeros. Data avalible means that a valid serial character of any value from 0 to 0xFF was received and is ready to be read from the buffer.

Lefty

When data comes into the serial port it is temporarily stored in memory (a 'buffer') until the program you write pulls it out and uses it. The buffer has limited capacity (128 bytes). If it fills up before you take it out the oldest bytes are dropped and the newest are added.

Serial.available() returns the count of the number of bytes that are waiting in the buffer for your code to pull out.

If it fills up before you take it out the oldest bytes are dropped and the newest are added.

Actually, if the buffer fills up, the code stops adding the newer bytes, and just throws them away.