@@ -132,14 +132,24 @@ In this example, the serial monitor missed the first 4 lines of "Hello, World"
132
132
(0 to 3) because the program started to print those lines immediately after
133
133
flashing, but before the serial monitor was connected.
134
134
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:
138
+ ```
139
+ $ tinygo monitor -baudrate=9600
140
+ ```
141
+
142
+ See [ Alternative Serial Monitors] ( #alternative-serial-monitors ) for an
143
+ explanation of the "baud rate".
144
+
135
145
## Serial Input
136
146
137
147
Occasionally it is useful to send characters from the host computer to the
138
148
microcontroller. The following program reads a single byte from the
139
149
` machine.Serial ` object and prints the character back to the host computer.
140
150
141
151
The caveat is that the ` Serial.ReadByte() ` feature is * not* currently
142
- implemented on every microcontrollers supported by TinyGo. For example, the
152
+ implemented on every microcontroller supported by TinyGo. For example, the
143
153
following program does not work on the ESP32 or the ESP8266.
144
154
145
155
``` go
@@ -204,23 +214,26 @@ characters](https://en.wikipedia.org/wiki/C0_and_C1_control_codes), these are
204
214
echoed back as 2 characters: the caret character ` ^ ` and a letter representing
205
215
the control character. For example, typing Control-P prints ` ^P ` .
206
216
207
- A number of control characters are intercepted by the ` tinygo monitor ` itself
208
- instead of being sent to the microcontroller:
217
+ Of the 32 possible control characters, some of them are intercepted by the
218
+ ` tinygo monitor ` itself instead of being sent to the microcontroller:
209
219
210
220
* Control-C: terminates the ` tinygo monitor `
211
221
* Control-Z: suspends the ` tinygo monitor ` and drops back into shell
212
222
* Control-\\ : terminates the ` tinygo monitor ` with a stack trace
213
- * Control-S: flow- control, suspends output to the console
214
- * Control-Q: flow- control, resumes output to the console
223
+ * Control-S: flow control, suspends output to the console
224
+ * Control-Q: flow control, resumes output to the console
215
225
* Control-@: thrown away by ` tinygo monitor `
216
226
217
227
## Alternative Serial Monitors
218
228
219
229
There are many alternative serial monitor programs that can be used instead of
220
230
` tinygo monitor ` . The setup is slightly more complicated because you need to
221
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.
222
235
223
- On Linux machines, the microcontrollers will be assigned a serial port that has
236
+ On Linux machines, the microcontroller will be assigned a serial port that has
224
237
a ` USB ` prefix or an ` ACM ` prefix like this:
225
238
226
239
* ` /dev/ttyUSB0 `
@@ -231,19 +244,31 @@ On MacOS machines, the serial port will look like this:
231
244
* ` /dev/cu.usbserial-1420 `
232
245
* ` /dev/cu.usbmodem6D8733AC53571 `
233
246
234
- [ TODO: No idea on Windows.]
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.
235
260
236
261
### Arduino IDE
237
262
238
263
The [ Arduino IDE] ( https://www.arduino.cc/en/software ) contains its own serial
239
264
monitor. You may choose to use that instead. You need to set the serial port
240
265
(something like ` /dev/ttyUSB0 ` on Linux, or ` /dev/cu.usbserial-1420 ` on MacOS),
241
- and set the baud rate to 115200.
266
+ and set the baud rate to 115200 (or 9600 on AVR processors) .
242
267
243
268
### pyserial
244
269
245
270
The [ pyserial] ( https://pyserial.readthedocs.io/en/latest/pyserial.html ) is a
246
- Python library that comes with its own serial monitor. Setting up python3
271
+ Python library that comes with its own serial monitor. Setting up a python3
247
272
environment is a complex topic that is beyond the scope of this document. But if
248
273
you are able to install ` python3 ` and ` pip3 ` , you can install ` pyserial ` and use
249
274
its built-in ` miniterm ` tool roughly like this:
0 commit comments