{"id":4916,"date":"2019-08-26T08:22:34","date_gmt":"2019-08-26T15:22:34","guid":{"rendered":"https:\/\/visualgdb.com\/w\/?p=4916"},"modified":"2019-08-26T08:22:34","modified_gmt":"2019-08-26T15:22:34","slug":"using-the-serial-port-on-teensy-boards-from-arduino-code","status":"publish","type":"post","link":"https:\/\/visualgdb.com\/tutorials\/arduino\/teensy\/serial\/","title":{"rendered":"Using the Serial Port on Teensy boards from Arduino code"},"content":{"rendered":"<p>This tutorial shows how to use the serial port on the Teensy boards to communicate with the Arduino code running on it. We will create a basic Teensy project with Visual Studio and modify it to input and output data via the USB serial port provided by the Teensy Arduino core.<\/p>\n<p>Before you begin, follow our <a href=\"https:\/\/visualgdb.com\/tutorials\/arduino\/teensy\/\">basic Teensy tutorial<\/a> to get started with Teensy and VisualGDB.<\/p>\n<ol>\n<li>Start Visual Studio and open the VisualGDB Arduino Project Wizard:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/01-newprj-8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4917\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/01-newprj-8.png\" alt=\"\" width=\"820\" height=\"613\" \/><\/a><\/li>\n<li>On the first page of the wizard select Create a new <strong>&#8220;Blinking LED&#8221;<\/strong> project: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/02-blink.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4918\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/02-blink.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>On the next page select your Teensy board. If the Teensy targets do not appear, follow our <a href=\"https:\/\/visualgdb.com\/tutorials\/arduino\/teensy\/\">basic Teensy tutorial<\/a> to import the Teensy files into VisualGDB:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/03-teensy.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4919\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/03-teensy.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>As the Teensy boards to not support JTAG\/SWD debugging, select &#8220;Debug Methods -&gt; Built-in GDB Simulator&#8221; on the Debug Method page. This will allow continuing the wizard, although the simulator won&#8217;t be able to emulate the Teensy board:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/04-simulator.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4920\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/04-simulator.png\" alt=\"\" width=\"856\" height=\"693\" \/><\/a><\/li>\n<li>Press &#8220;Finish&#8221; to create the project. Once it is created and loaded, open VisualGDB Project Properties and locate the &#8220;USB Type&#8221; setting:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/05-serial.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4921\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/05-serial.png\" alt=\"\" width=\"934\" height=\"696\" \/><\/a>The Teensy board is capable of emulating numerous different USB peripherals and their combinations. In this tutorial we will only use the virtual serial port, so ensure &#8220;Serial&#8221; is selected as the USB type.<\/li>\n<li>VisualGDB will automatically scan the code of the Teensy core and configure IntelliSense accordingly. E.g. it will suggest the available serial ports and their methods: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/07-write.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4923\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/07-write.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>We will now use the low-level <strong>write()<\/strong> method of the <strong>Serial<\/strong> object to output some text. Replace the loop() function with the code below:\n<pre class=\"\">void loop()\r\n{\r\n    Serial.write(\"On\\r\\n\", 4);\r\n    digitalWrite(LED_BUILTIN, HIGH);\r\n    delay(1000);\r\n    Serial.write(\"Off\\r\\n\", 5);\r\n    digitalWrite(LED_BUILTIN, LOW);\r\n    delay(1000);\r\n}<\/pre>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/08-ser.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4924\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/08-ser.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Build the code and program it into the Teensy board by pressing the programming button (see the <a href=\"https:\/\/visualgdb.com\/tutorials\/arduino\/teensy\/\">basic Teensy tutorial<\/a> for more details). Once the programming is complete, the board will get recognized as a USB-based COM port. Open VisualGDB Project Properties and select the port in the &#8220;COM port for uploading the sketch&#8221; field: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/09-port-1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4925\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/09-port-1.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Close VisualGDB Project Properties, right-click in Solution Explorer and select &#8220;Open Serial Terminal&#8221;:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/10-term.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4926\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/10-term.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>VisualGDB will open a terminal window showing the output from the <strong>loop()<\/strong> function:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/11-output.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4927\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/11-output.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Now we will demonstrate some more high-level serial port API:\n<ol style=\"list-style-type: lower-alpha;\">\n<li>The <strong>readStringUntil()<\/strong> method will be used to read the input line-by-line.<\/li>\n<li>The <strong>printf()<\/strong> method will be used to write formatted text output<\/li>\n<\/ol>\n<p><a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/12-until.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4928\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/12-until.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Replace the contents of the loop() function with the code below:\n<pre class=\"\">\u00a0       Serial.print(\"Write something:\\r\\n\");\r\n        auto line = Serial.readStringUntil('\\n');\r\n        Serial.printf(\"You wrote \\\"%s\\\"\\r\\n\", line.c_str());\r\n<\/pre>\n<p>Then program the board and reconnect the serial terminal:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/13-timed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4929\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/13-timed.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a>You will observe that the <strong>printf()<\/strong> method works as expected, however the <strong>readStringUntil()<\/strong> method times out.<\/li>\n<li>The lack of a hardware debugging interface makes it harder to understand what is going on, so we will rely on VisualGDB&#8217;s code jumps to quickly navigate the function call chains: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/chain.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4933\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/chain.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Go to the <strong>timedRead<\/strong>() function and note code that checks the <strong>_timeout<\/strong> variable: <a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/14-timeout.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4930\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/14-timeout.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Hover the mouse over the <strong>_timeout<\/strong> word and select &#8220;Code\/Data Relations&#8221;. VisualGDB will immediately show that the timeout is set by the<strong> setTimeout()<\/strong> method and read by 4 other methods:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/15-relations.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4931\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/15-relations.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<li>Add a call to <strong>setTimeout()<\/strong> before calling <strong>readStringUntil().\u00a0<\/strong>The example will now work as expected, waiting for you to enter the entire line of text:<a href=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/16-echo.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4932\" src=\"https:\/\/visualgdb.com\/w\/wp-content\/uploads\/2019\/06\/16-echo.png\" alt=\"\" width=\"1043\" height=\"775\" \/><\/a><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows how to use the serial port on the Teensy boards to communicate with the Arduino code running<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[170,53,183],"_links":{"self":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4916"}],"collection":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/comments?post=4916"}],"version-history":[{"count":1,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4916\/revisions"}],"predecessor-version":[{"id":4934,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/posts\/4916\/revisions\/4934"}],"wp:attachment":[{"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/media?parent=4916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/categories?post=4916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/visualgdb.com\/w\/wp-json\/wp\/v2\/tags?post=4916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}