aboutsummaryrefslogtreecommitdiffstats
path: root/docs/source/lang/api/serial.rst
blob: 58002e3f46730372bf588ac6a8d5174127871840 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
.. _lang-serial:

Serial Ports (``Serial1``, ``Serial2``, ``Serial3``)
====================================================

Used for communication between the Maple board and a computer or other
devices.

.. contents:: Contents
   :local:

Introduction
------------

The Maple has three serial ports (also known as a UARTs or USARTs):
``Serial1``, ``Serial2``, and ``Serial3``. They communicate using the
pins summarized in the following table:

.. list-table::
   :header-rows: 1

   * - Serial port
     - TX, RX, CK
     - CTS, RTS (if present)

   * - ``Serial1``
     - 7, 8, 6
     -

   * - ``Serial2``
     - 1, 0, 10
     - 2, 3

   * - ``Serial3``
     - 29, 30, 31
     - 32, 33

Thus, if you use a particular serial port, you cannot also use its
communication pins for other purposes at the same time.

If you want to communicate with the Maple using the provided USB port,
use :ref:`SerialUSB <lang-serialusb>` instead.

To use them to communicate with an external TTL serial device, connect
the TX pin to your device's RX pin, the RX to your device's TX pin,
and the ground of your Maple to your device's ground.

.. warning:: Don't connect these pins directly to an RS232 serial
   port; they operate at +/- 12V and can damage your board.


Library Documentation
---------------------

All of the ``Serial[1,2,3]`` objects are instances of the
``HardwareSerial`` class, which is documented in this section.  (This
means that you can use any of these functions on any of ``Serial1``,
``Serial2``, and ``Serial3``).

.. cpp:class:: HardwareSerial

   Serial port class.  Predefined instances are ``Serial1``,
   ``Serial2``, and ``Serial3``.

.. cpp:function:: HardwareSerial::begin(unsigned int baud)

   Set up a ``HardwareSerial`` object for communications.  This method
   must be called before attempting to use the ``HardwareSerial``
   object (typically, you call this in your :ref:`setup()
   <lang-setup>` function).

.. cpp:function:: HardwareSerial::end()

   Disables the USART associated with this object, allowing any
   associated communication pins to be used for other purposes.

.. cpp:function:: unsigned int HardwareSerial::available()

   Returns the number of bytes available for reading.

.. cpp:function:: unsigned char HardwareSerial::read()

   Returns the next available, unread character.  If there are no
   available characters (you can check this with :cpp:func:`available
   <HardwareSerial::available>`), the call will block until one
   becomes available.

.. cpp:function:: HardwareSerial::flush()

   Removes the contents of the Serial's associated USART RX FIFO.
   That is, clears any buffered characters, so that the next character
   read is guaranteed to be new.

.. cpp:function:: HardwareSerial::print(unsigned char b)

   Print the given byte over the USART.

.. cpp:function:: HardwareSerial::print(char c)

   Print the given character over the USART.  7-bit clean characters
   are typically interpreted as ASCII text.

.. cpp:function:: HardwareSerial::print(const char *str)

   Print the given null-terminated string over the USART.

.. cpp:function:: HardwareSerial::print(int n)

   Print the argument's digits over the USART, in decimal format.
   Negative values will be prefixed with a ``'-'`` character.

.. cpp:function:: HardwareSerial::print(unsigned int n)

   Print the argument's digits over the USART, in decimal format.

.. cpp:function:: HardwareSerial::print(long long n)

   Print the argument's digits over the USART, in decimal format.
   Negative values will be prefixed with a ``'-'`` character.

.. cpp:function:: HardwareSerial::print(unsigned long long n)

   Print the argument's digits over the USART, in decimal format.

.. _lang-serial-print-n-base:

.. cpp:function:: HardwareSerial::print(int n, int base)

   Print the digits of ``n`` over the USART, in base ``base``.  The
   ``base`` value 2 corresponds to binary, 8 to octal, 10 to decimal,
   and 16 to hexadecimal (you can also use the symbolic constants
   ``BIN``, ``OCT``, ``DEC``, ``HEX``).  If ``base`` is 10, negative
   values will be prefixed with a ``'-'`` character (otherwise, ``n``
   will be interpreted as an unsigned quantity).

.. cpp:function:: HardwareSerial::print(long long n, int base)

   Same behavior as the above :ref:`print(int n, int base)
   <lang-serial-print-n-base>`, except with 64-bit values.

.. cpp:function:: HardwareSerial::print(double n)

   Print ``n``, accurate to 6 digits after the decimal point.

.. _lang-serial-println:

.. cpp:function:: HardwareSerial::println(char c)

   Like ``print(c)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(const char *c)

   Like ``print(c)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(unsigned char b)

   Like ``print(b)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(int n)

   Like ``print(n)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(unsigned int n)

   Like ``print(n)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(long long n)

   Like ``print(n)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(unsigned long long n)

   Like ``print(n)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(int n, int base)

   Like ``print(n, b)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(long long n, int base)

   Like ``print(n, b)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println(double n)

   Like ``print(n)``, followed by ``"\r\n"``.

.. cpp:function:: HardwareSerial::println()

   Prints ``"\r\n"`` over the USART.

.. cpp:function:: HardwareSerial::write(unsigned char ch)

   Sends one character over the USART.  This function is currently
   blocking, although nonblocking writes are a planned future
   extension.

   This is a low-level function.  One of the ``print()`` or
   ``println()`` functions is likely to be more useful when printing
   multiple characters, when formatting numbers for printing, etc.

.. cpp:function:: HardwareSerial::write(const char* str)

   Send the given null-terminated character string over the USART.

   This is a low-level function.  One of the ``print()`` or
   ``println()`` functions is likely to be more useful when printing
   multiple characters, when formatting numbers for printing, etc.

.. cpp:function:: HardwareSerial::write(void *buf, unsigned int size)

   Writes the first ``size`` bytes of ``buf`` over the USART.  Each
   byte is transmitted as an individual character.

   This is a low-level function.  One of the ``print()`` or
   ``println()`` functions is likely to be more useful when printing
   multiple characters, when formatting numbers for printing, etc.

Arduino Compatibility Note
--------------------------

Unlike the Arduino, none of the Maple's serial ports is connected to
the USB port on the Maple board (for that, use :ref:`SerialUSB
<lang-serialusb>`).  Thus, to use these pins to communicate with your
personal computer, you will need an additional USB-to-serial adaptor.

.. TODO LATER port these examples over

.. Examples
.. --------

.. -  `ASCII Table <http://arduino.cc/en/Tutorial/ASCIITable>`_
.. -  `Dimmer <http://arduino.cc/en/Tutorial/Dimmer>`_
.. -  `Graph <http://arduino.cc/en/Tutorial/Graph>`_
.. -  `Physical Pixel <http://arduino.cc/en/Tutorial/PhysicalPixel>`_
.. -  `Virtual Color Mixer <http://arduino.cc/en/Tutorial/VirtualColorMixer>`_
.. -  `Serial Call Response <http://arduino.cc/en/Tutorial/SerialCallResponse>`_
.. -  `Serial Call Response ASCII <http://arduino.cc/en/Tutorial/SerialCallResponseASCII>`_

.. include:: cc-attribution.txt