45
45
package main
46
46
47
47
import (
48
- " time"
49
48
" fmt"
49
+ " time"
50
50
)
51
51
52
52
func main () {
@@ -116,6 +116,8 @@ serial monitor application on the host computer. There are many ways to do this,
116
116
but the easiest is probably the ` tinygo monitor ` subcommand which is built
117
117
directly into the ` tinygo ` program itself.
118
118
119
+ ### ` monitor ` subcommand
120
+
119
121
After flashing the program above, run the ` tinygo monitor ` program to see the
120
122
output every second from the microcontroller:
121
123
@@ -124,23 +126,141 @@ $ tinygo monitor
124
126
Connected to /dev/ttyACM0. Press Ctrl-C to exit.
125
127
4 : Hello, World
126
128
5 : Hello, World
127
- 6 : Hello, World
128
129
[...]
129
130
```
130
131
131
132
In this example, the serial monitor missed the first 4 lines of "Hello, World"
132
133
(0 to 3) because the program started to print those lines immediately after
133
134
flashing, but before the serial monitor was connected.
134
135
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
+
138
180
```
139
181
$ tinygo monitor -baudrate=9600
140
182
```
141
183
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
+ ```
144
264
145
265
## Serial Input
146
266
@@ -184,8 +304,8 @@ func main() {
184
304
}
185
305
186
306
// 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
189
309
// theoretically receive as much as 11000 bytes/second
190
310
// (115200 baud). This delay can be removed and the
191
311
// 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
227
347
## Alternative Serial Monitors
228
348
229
349
There 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.
260
354
261
355
### Arduino IDE
262
356
@@ -284,7 +378,8 @@ Another useful feature of `pyserial` is the `list_ports` command:
284
378
$ python3 -m serial.tools.list_ports
285
379
/dev/ttyACM0
286
380
/dev/ttyS0
287
- 2 ports found
381
+ /dev/ttyUSB0
382
+ 3 ports found
288
383
```
289
384
290
385
This is useful when you plug in a random microcontroller to the USB port, and
0 commit comments