Hardware Reference
In-Depth Information
As with “Local Variables” on page 74 , the variable i in the ex-
ample above can only be accessed within the looping block of
code. After the for loop has executed its last iteration, the
variable will be destroyed.
More Serial
In Chapter 2 , you learned how Galileo can send data via serial to a computer.
But serial can act as a two-way channel of communication between your
Galileo and computer (or other device). There are a few Serial functions that
help you process the data that's sent to your board.
Serial.available() and Serial.read()
On the Galileo, there's a serial buffer which stores all the bytes of serial data
it receives. When you use your sketch to read a byte from serial, you're ac-
tually reading the first byte that's “at the front of the line” in the buffer. After
reading that byte, it's removed from the buffer, and the next byte (if there is
one) is ready to be read next.
In other words, the serial buffer follows a first in, first out , or FIFO, convention.
The first byte received into the buffer is the first byte out when read by your
sketch.
There is a limited amount of space for data in the serial buffer,
so you want to make sure that your sketch is reading the data
at least as frequently as the transmitting device is sending it.
If the device that's transmitting bytes sends a lot of data and
your sketch doesn't call Serial.read() frequently enough, the
buffer will overflow and you may get unexpected results.
One way to avoid this problem is to use a system of “call and
response.” When the Galileo is ready for data, it sends a byte
to the other device. The other device will respond with some
data. The Galileo can then take its time processing the data
and acting on it. When it's ready for more data, it will send
another byte.
To try out reading serial information, upload Example 5-1 to your board and
open the serial monitor.
Example 5-1. Serial receive example
#define LED 13
void setup () {
 
Search WWH ::




Custom Search