4545package main
4646
4747import (
48- " time"
4948 " fmt"
49+ " time"
5050)
5151
5252func main () {
@@ -116,6 +116,8 @@ serial monitor application on the host computer. There are many ways to do this,
116116but the easiest is probably the ` tinygo monitor ` subcommand which is built
117117directly into the ` tinygo ` program itself.
118118
119+ ### ` monitor ` subcommand
120+
119121After flashing the program above, run the ` tinygo monitor ` program to see the
120122output every second from the microcontroller:
121123
@@ -124,23 +126,141 @@ $ tinygo monitor
124126Connected to /dev/ttyACM0. Press Ctrl-C to exit.
1251274 : Hello, World
1261285 : Hello, World
127- 6 : Hello, World
128129[...]
129130```
130131
131132In this example, the serial monitor missed the first 4 lines of "Hello, World"
132133(0 to 3) because the program started to print those lines immediately after
133134flashing, but before the serial monitor was connected.
134135
135- On some AVR microcontrollers, the speed of the serial port is set to 9600
136- instead of the default 115200. So you need to override the baud rate of `tinygo
137- monitor` like this:
136+ ### ` -monitor ` flag
137+
138+ It is often useful to automatically start the monitor immediately after flashing
139+ your program to the microcontroller. The ` tinygo flash ` command takes an
140+ optional ` -monitor ` flag to accomplish this:
141+
142+ ```
143+ $ tinygo flash -target=xiao -monitor
144+ ```
145+
146+ On some microcontrollers, the ` -monitor ` flag fails with the following error
147+ message because the monitor starts too quickly:
148+
149+ ```
150+ $ tinygo flash -target=arduino-zero -monitor
151+ [...]
152+ Connected to /dev/ttyACM0. Press Ctrl-C to exit.
153+ error: read error: Port has been closed
154+ ```
155+
156+ If this happens, you can chain the ` flash ` and ` monitor ` subcommands manually,
157+ with a 1 or 2-second delay between the two commands. On Linux or MacOS, the
158+ command invocation looks like this:
159+
160+ ```
161+ $ tinygo flash -target=arduino-zero && sleep 1 && tinygo monitor
162+ ```
163+
164+ (The ` && ` separator runs the next command only if the previous command completed
165+ without errors. This is safer than using the semicolon ` ; ` separator because the
166+ semicolon continues to execute commands even if the previous command returned an
167+ error code.)
168+
169+ ### Baud Rate
170+
171+ The default [ baud rate] ( https://en.wikipedia.org/wiki/Serial_port#Speed ) of the
172+ serial port for almost all microcontrollers supported by TinyGo is 115200. The
173+ exceptions are boards using the AVR processors ([ Arduino Nano] ({{<ref
174+ "../reference/microcontrollers/arduino-nano">}}), [ Arduino Mega 1280] ({{<ref
175+ "../reference/microcontrollers/arduino-mega1280">}}), [ Arduino Mega 2560] ({{<ref
176+ "../reference/microcontrollers/arduino-mega2560">}})). On these, the serial port
177+ is set to 9600, so you need to override the baud rate of ` tinygo monitor ` like
178+ this:
179+
138180```
139181$ tinygo monitor -baudrate=9600
140182```
141183
142- See [ Alternative Serial Monitors] ( #alternative-serial-monitors ) for an
143- explanation of the "baud rate".
184+ You can combine the ` flash ` subcommand, the ` -monitor ` flag, and the ` -baudrate `
185+ flag into a single invocation like this:
186+
187+ ```
188+ $ tinygo flash -target arduino-nano -monitor -baudrate 9600
189+ ```
190+
191+ (Notice that the ` = ` after each flag has been replaced with a space. It's an
192+ alternative syntax that some people prefer because a space is easier to type
193+ than an equal sign ` = ` .)
194+
195+ ### Serial Port on Host
196+
197+ The microcontroller will be assigned a serial port on the host computer. If you
198+ have only a single microcontroller attached, you will normally not need to worry
199+ about what these serial ports are called. The ` tinygo monitor ` will
200+ automatically figure out which serial port to use.
201+
202+ On Linux machines, the serial port will have a ` USB ` prefix or an ` ACM ` prefix
203+ like this:
204+
205+ * ` /dev/ttyUSB0 `
206+ * ` /dev/ttyACM0 `
207+
208+ On MacOS machines, the serial port will look like this:
209+
210+ * ` /dev/cu.usbserial-1420 `
211+ * ` /dev/cu.usbmodem6D8733AC53571 `
212+
213+ On Windows machines, the serial port looks something like:
214+
215+ * ` COM1 `
216+ * ` COM31 `
217+
218+ ### Multiple Microcontrollers
219+
220+ If you have more than one microcontroller attached to the host computer, the
221+ ` tinygo flash ` and ` tinygo monitor ` subcommands can * sometimes* figure out which
222+ port it is using, but they will sometimes print out an error message, like this:
223+
224+ ```
225+ $ tinygo flash -target arduino-nano
226+ error: multiple serial ports available - use -port flag,
227+ available ports are /dev/ttyACM0, /dev/ttyUSB0
228+ ```
229+
230+ You then need to supply the ` -port ` flag to identify the microcontroller that
231+ you want to flash and monitor:
232+
233+ ```
234+ $ tinygo flash -target=arduino-nano -port=/dev/ttyUSB0
235+
236+ $ tinygo monitor -port=/dev/ttyUSB0 -baudrate=9600
237+ ```
238+
239+ Sometimes it is possible to combine the two commands into a single command even
240+ in the presence of multiple microcontrollers:
241+
242+ ```
243+ $ tinygo flash -target xiao -monitor
244+ ```
245+
246+ But sometimes, combining ` flash ` and ` monitor ` into a single command does not
247+ work. In that case, you can issue the ` flash ` and ` monitor ` commands separately.
248+ But it is often easier to just pull out the extra microcontroller(s) so that
249+ only a single board is connected to the host computer.
250+
251+ ```
252+ $ tinygo flash -target=arduino-nano -monitor
253+ error: multiple serial ports available - use -port flag,
254+ available ports are /dev/ttyACM0, /dev/ttyUSB0
255+
256+ $ tinygo flash -target=arduino-nano -monitor -port=/dev/ttyUSB0 -baudrate=9600
257+ [...]
258+ avrdude: 4238 bytes of flash verified
259+ avrdude done. Thank you.
260+ [...]
261+ error: multiple serial ports available - use -port flag,
262+ available ports are /dev/ttyACM0, /dev/ttyUSB0
263+ ```
144264
145265## Serial Input
146266
@@ -184,8 +304,8 @@ func main() {
184304 }
185305
186306 // This assumes that the input is coming from a keyboard
187- // so checking 120 per second is sufficient. But if the
188- // data comes from another processor, the port can
307+ // so checking 120 times per second is sufficient. But if
308+ // the data comes from another processor, the port can
189309 // theoretically receive as much as 11000 bytes/second
190310 // (115200 baud). This delay can be removed and the
191311 // Serial.Read() method can be used to retrieve
@@ -227,36 +347,10 @@ Of the 32 possible control characters, some of them are intercepted by the
227347## Alternative Serial Monitors
228348
229349There are many alternative serial monitor programs that can be used instead of
230- ` tinygo monitor ` . The setup is slightly more complicated because you need to
231- know the serial port on the host computer that the microcontroller is mapped to.
232- One way to discover the serial port is to use the ` -x ` flag on the ` flash `
233- command like this: ` tinygo flash -x -target=xxx ` . This will print diagnostic
234- messages which will contain the serial port of the microcontroller.
235-
236- On Linux machines, the microcontroller will be assigned a serial port that has
237- a ` USB ` prefix or an ` ACM ` prefix like this:
238-
239- * ` /dev/ttyUSB0 `
240- * ` /dev/ttyACM0 `
241-
242- On MacOS machines, the serial port will look like this:
243-
244- * ` /dev/cu.usbserial-1420 `
245- * ` /dev/cu.usbmodem6D8733AC53571 `
246-
247- On Windows machines, the serial port looks something like:
248-
249- * ` COM1 `
250- * ` COM31 `
251-
252- You also need to know the [ baud
253- rate] ( https://en.wikipedia.org/wiki/Serial_port#Speed ) of the serial port. The
254- default for almost all microcontrollers supported by TinyGo is 115200. The
255- current exceptions are boards using the AVR processors ([ Arduino Nano] ({{<ref
256- "../reference/microcontrollers/arduino-nano">}}), [ Arduino Mega 1280] ({{<ref
257- "../reference/microcontrollers/arduino-mega1280">}}), [ Arduino Mega 2560] ({{<ref
258- "../reference/microcontrollers/arduino-mega2560">}})). On these, the serial port
259- is set to 9600.
350+ ` tinygo monitor ` . The setup is slightly more complicated because you will need
351+ to supply the serial port and baud rate of the microcontroller as described in
352+ the [ Serial Port on Host] ( #serial-port-on-host ) and [ Baud Rate] ( #baud-rate )
353+ subsections above.
260354
261355### Arduino IDE
262356
@@ -284,7 +378,8 @@ Another useful feature of `pyserial` is the `list_ports` command:
284378$ python3 -m serial.tools.list_ports
285379/dev/ttyACM0
286380/dev/ttyS0
287- 2 ports found
381+ /dev/ttyUSB0
382+ 3 ports found
288383```
289384
290385This is useful when you plug in a random microcontroller to the USB port, and
0 commit comments