APPENDIX 3
Advanced Serial Programming
In nearly all of the book’s projects, we’ve used the Arduino’s serial port.
Sometimes we only emitted debug messages to monitor the current state of
our sketches, but often we needed it to actually output information or to send
commands. And the fact is, we’ve used the
Serial
class without explaining how
serial communication actually works. We’ll catch up on that in this appendix.
To communicate with an Arduino, we mainly used JavaScript, and in some
cases we used Processing. But many developers prefer other languages, and
in this appendix, you’ll also learn how to use C/C++, Java, Ruby, Python,
and Perl to talk to an Arduino.
Learning More About Serial Communication
In Chapter 2, Creating Bigger Projects with the Arduino, on page 23, you saw
that you need only three wires for serial communication: a common ground,
a line for transmitting data (TX), and one for receiving data (RX). (See the
diagram on page 28.)
Data is transmitted as electrical pulses, so both communication partners
need a reference for the voltage level, and that’s what the common ground is
for. The transmission line is used to send data to the recipient and has to be
connected to the recipient’s receiving line. This enables full-duplex communi-
cation where both partners can send and receive data simultaneously.
(Wouldn’t it be great if people could also communicate full-duplex?)
We now know how to connect two devices, but we still have to transmit some
data. Therefore, both communication partners have to agree on a protocol,
and on page 254, you can see what a typical serial communication looks like.
The different states of a bit are represented by different voltage levels. Usually,
report erratum • discuss
www.it-ebooks.info