{"id":4960,"date":"2015-02-19T10:43:38","date_gmt":"2015-02-19T18:43:38","guid":{"rendered":"https:\/\/blog.digilentinc.com\/?p=4960"},"modified":"2021-06-17T08:18:33","modified_gmt":"2021-06-17T15:18:33","slug":"serial-monitor-communication","status":"publish","type":"post","link":"https:\/\/digilent.com\/blog\/serial-monitor-communication\/","title":{"rendered":"Serial Monitor Communication"},"content":{"rendered":"<p>Welcome back to the Digilent Blog!<\/p>\n<p>&nbsp;<\/p>\n<p>MPIDE comes with a nice serial monitor where you are able to print out values that your system board has measured onto your computer screen. But if you are able to print things onto your computer screen, wouldn&#8217;t it make sense if the chipKIT board also accepted and processed values that we typed into the serial monitor? It would make a lot of sense, which is probably why we can do just that. Let&#8217;s find out how.<\/p>\n<p>&nbsp;<\/p>\n<p>For those of you who might not know, the MPIDE&#8217;s serial monitor is a type of interface that monitors (no surprise there) and displays what is happening in the USB-UART terminal that is connecting your chipKIT to the computer. Whenever you call the &#8220;Serial.print&#8221; function, all of the information that is sent to this UART interface is displayed on your computer screen.<\/p>\n<figure id=\"attachment_4261\" aria-describedby=\"caption-attachment-4261\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4261\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37.jpg\" alt=\"Typecasting to turn a character 7 into a number 7 and add it to an existing number\" width=\"600\" height=\"406\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37.jpg 853w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37-600x406.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2014\/12\/adjusting-a-37-225x152.jpg 225w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4261\" class=\"wp-caption-text\">This <a title=\"Listening Calulator\" href=\"https:\/\/digilent.com\/blog\/index.php\/a-listening-calculator\/\" target=\"_blank\" rel=\"noopener\">screenshot<\/a>\u00a0with the serial monitor on the right-hand side shows what we have printed to UART terminal.<\/figcaption><\/figure>\n<p>What this picture does not show is that there is a text box and a &#8220;Send&#8221; button where you are able to type text into the Serial Monitor and send it off to the chipKIT board. Naturally, the chipKIT board will only respond to this information if it has been programmed to do so. Luckily, this is surprisingly easy to do with the Serial.available() and Serial.read() functions. The Serial.available() checks to see if any data is available in UART buffer. If there is, then it will return how many bytes are currently present inside the buffer.<\/p>\n<p>&nbsp;<\/p>\n<p>Serial.read() will read and remove one byte at a time from the UART buffer. The single removal makes it very convenient to combine this function with a while loop. That way, we can tell the chipKIT board:<\/p>\n<figure id=\"attachment_4961\" aria-describedby=\"caption-attachment-4961\" style=\"width: 603px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/while-serial-is-available-read.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4961\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/while-serial-is-available-read.jpg\" alt=\"While there is data available in the Serial Monitor, read and remove a byte.\" width=\"603\" height=\"135\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/while-serial-is-available-read.jpg 652w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/while-serial-is-available-read-600x134.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/while-serial-is-available-read-225x50.jpg 225w\" sizes=\"auto, (max-width: 603px) 100vw, 603px\" \/><\/a><figcaption id=\"caption-attachment-4961\" class=\"wp-caption-text\">While there is data available in the serial monitor, read and remove a byte.<\/figcaption><\/figure>\n<p>Once all of the characters have been collected, you can compare your set of characters (also known as a string) to a user defined set of options. If the set of characters matches one of the options, you can then have the chipKIT board send a response back over the serial monitor or run a routine such as flashing an LED multiple times.<\/p>\n<figure id=\"attachment_4965\" aria-describedby=\"caption-attachment-4965\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/uC32-conversation.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-4965\" src=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/uC32-conversation.jpg\" alt=\"MPIDE's Serial Monitor opens up a lot of options in your project design.\" width=\"600\" height=\"304\" srcset=\"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/uC32-conversation.jpg 1363w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/uC32-conversation-600x304.jpg 600w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/uC32-conversation-1024x519.jpg 1024w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/uC32-conversation-225x114.jpg 225w, https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/uC32-conversation-800x406.jpg 800w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/a><figcaption id=\"caption-attachment-4965\" class=\"wp-caption-text\">MPIDE&#8217;s serial monitor opens up a lot of options in your project design (click on the picture to enlarge).<\/figcaption><\/figure>\n<p>Check it out!<\/p>\n<div class='watch-action'><div class='watch-position align-left'><div class='action-like'><a class='lbg-style6 like-4960 jlk' data-task='like' data-post_id='4960' data-nonce='4d1b41d1d7' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Like' \/><span class='lc-4960 lc'>0<\/span><\/a><\/div><div class='action-unlike'><a class='unlbg-style6 unlike-4960 jlk' data-task='unlike' data-post_id='4960' data-nonce='4d1b41d1d7' rel='nofollow'><img src='https:\/\/digilent.com\/blog\/wp-content\/plugins\/wti-like-post-pro\/images\/pixel.gif' title='Unlike' \/><span class='unlc-4960 unlc'>0<\/span><\/a><\/div><\/div> <div class='status-4960 status align-left'>Be the 1st to vote.<\/div><\/div><div class='wti-clear'><\/div>","protected":false},"excerpt":{"rendered":"<p>MPIDE comes with a nice serial monitor where you are able to print out values that your system board has measured onto your computer screen. But if you are able to print things onto your computer screen, wouldn&#8217;t it make sense if the chipKIT board also accepted and processed values that we typed into the serial monitor? It would make a lot of sense, which is probably why we can do just that. Let&#8217;s find out how.<\/p>\n","protected":false},"author":17,"featured_media":4961,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4322,1563],"tags":[],"ppma_author":[4469],"class_list":["post-4960","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-accessories","category-guide"],"jetpack_featured_media_url":"https:\/\/digilent.com\/blog\/wp-content\/uploads\/2015\/01\/while-serial-is-available-read.jpg","jetpack_sharing_enabled":true,"authors":[{"term_id":4469,"user_id":17,"is_guest":0,"slug":"jamescolvin","display_name":"James Colvin","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/8aa85f7d11711acc7e571e1ed26c901b614a7064a2e15e522f54d9f26792ea9e?s=96&d=mm&r=g","1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":"","9":"","10":""}],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/4960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/comments?post=4960"}],"version-history":[{"count":0,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/posts\/4960\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media\/4961"}],"wp:attachment":[{"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/media?parent=4960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/categories?post=4960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/tags?post=4960"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/digilent.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=4960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}