Hardware Reference
In-Depth Information
Reading Data
It isn't all about sending data through a serial connection; Arduinos can also
receive data. Receiving data can be used for many projects; computers can send
data, for example, to control the brightness of an LED. Some wireless devices
like Bluetooth also use serial ports to transmit data; maybe your telephone can
send data to unlock a door or to open a window. Arduinos can also talk to each
other over a serial connection, for example, a master Arduino telling a slave
Arduino to turn on the lights in the room it controls.
When the UART device receives data, it stores it in an internal buffer. This
buffer normally holds 64 characters; any more, and data will be lost. Don't
worry; in practice, 64 is more than enough because interrupts can be put in
place to tell the microcontroller to retrieve information from this buffer before
too much data arrives.
Starting Communications
The i rst part of any communications is to initiate the connection. Each side
must open up a serial port before being able to send and receive data. For the
Arduino to initialize a serial communication, you must use the begin() function:
Serial.begin(speed);
Serial.begin(speed, config);
This function requires one or two parameters; the speed parameter is the
baud rate for the serial communication. It must be the same on both devices,
otherwise they will not be able to communicate. It is expressed as an int , and
is the exact speed to use. By default, the Arduino IDE will use 9,600, but you
are free to choose a different value, so long as both the Arduino serial monitor
and the Arduino itself use the same speed.
Is Data Waiting?
You can check the number of bytes in the serial buffer by calling available() .
This can also let you know if there is any valid data waiting to be read.
int bytes = Serial.available();
There are two ways people typically use available() . One way is to return
the result to know the amount of bytes waiting to be read.
int inBytes = Serial.available();
 
Search WWH ::




Custom Search