{"id":349,"date":"2011-12-19T19:13:16","date_gmt":"2011-12-19T18:13:16","guid":{"rendered":"http:\/\/stigern.net\/blog\/?p=349"},"modified":"2011-12-20T07:24:10","modified_gmt":"2011-12-20T06:24:10","slug":"tutorial-c-communicating-with-arduino-over-serial","status":"publish","type":"post","link":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/","title":{"rendered":"Tutorial: C# Communicating with Arduino over Serial"},"content":{"rendered":"<p>In this tutorial I&#8217;ll try to show you how to easily tell an arduino what to do from a C# program using serial communication. The only thing the C# program does is send a byte to the arduino, and the arduino program will then set pin 13 to <strong>HIGH<\/strong> or <strong>LOW<\/strong>.<\/p>\n<p>Lets start with the C# sourcecode, it&#8217;s written in C# Express 2010 which you can download for free <a href=\"http:\/\/www.microsoft.com\/visualstudio\/en-us\/products\/2010-editions\/visual-csharp-express\" target=\"_blank\">here<\/a>. Get the C# project files:<\/p>\n<p><a href=\"http:\/\/stigern.net\/forum\/download\/file.php?id=7\" target=\"_blank\">Download\u00a0CSharpEasySerialSource.zip<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"Easy Serial\" src=\"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg\" alt=\"\" width=\"296\" height=\"201\" \/><\/p>\n<p>I tried to comment the code so that it should explain most of the stuff going on. But it&#8217;s pretty basic, you select the current comport then hit Connect. If you selected the correct port that the arduino is using it should now be connected. The textbox only takes numbers, writing text there will only result in it being converted to bytes.<\/p>\n<p>When you click Send it will try to send the contents of the textbox to the arduino. In my example code I used &#8220;0&#8221; and &#8220;1&#8221; for turning off an on the LED. Lets look at the Ardunio example:<\/p>\n<div id=\"wpshdo_1\" class=\"wp-synhighlighter-outer\"><div id=\"wpshdt_1\" class=\"wp-synhighlighter-collapsed\"><table border=\"0\" width=\"100%\"><tr><td align=\"left\" width=\"80%\"><a name=\"#codesyntax_1\"><\/a><a id=\"wpshat_1\" class=\"wp-synhighlighter-title\" href=\"#codesyntax_1\"  onClick=\"javascript:wpsh_toggleBlock(1)\" title=\"Click to show\/hide code block\">Source code<\/a><\/td><td align=\"right\"><a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_code(1)\" title=\"Show code only\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/stigern.net\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/code.png\" \/><\/a>&nbsp;<a href=\"#codesyntax_1\" onClick=\"javascript:wpsh_print(1)\" title=\"Print code\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/stigern.net\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/printer.png\" \/><\/a>&nbsp;<a href=\"https:\/\/stigern.net\/blog\/wp-content\/plugins\/wp-synhighlight\/About.html\" target=\"_blank\" title=\"Show plugin information\"><img decoding=\"async\" border=\"0\" style=\"border: 0 none\" src=\"https:\/\/stigern.net\/blog\/wp-content\/plugins\/wp-synhighlight\/themes\/default\/images\/info.gif\" \/><\/a>&nbsp;<\/td><\/tr><\/table><\/div><div id=\"wpshdi_1\" class=\"wp-synhighlighter-inner\" style=\"display: none;\"><pre class=\"cpp\" style=\"font-family:monospace;\"><span class=\"kw4\">void<\/span> setup<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>\n<span class=\"br0\">&#123;<\/span>\n  Serial.<span class=\"me1\">begin<\/span><span class=\"br0\">&#40;<\/span>9600<span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span>\n  <span class=\"co1\">\/\/ Define the led pin as output<\/span>\n  pinMode<span class=\"br0\">&#40;<\/span>13, OUTPUT<span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n<span class=\"kw4\">void<\/span> loop<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>\n<span class=\"br0\">&#123;<\/span>\n  <span class=\"co1\">\/\/ Read data from serial communication<\/span>\n  <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>Serial.<span class=\"me1\">available<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy1\">&gt;<\/span> 0<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span>\n  <span class=\"kw4\">int<\/span> b <span class=\"sy1\">=<\/span> Serial.<span class=\"me1\">read<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span>\n  <span class=\"co1\">\/\/ Note, I made it so that it's more sensitive to 0-40% load.<\/span>\n  <span class=\"co1\">\/\/ Since thats where it's usually at when using the computer as normal.<\/span>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>b <span class=\"sy1\">==<\/span> 1<span class=\"br0\">&#41;<\/span>\n<span class=\"br0\">&#123;<\/span>\n  digitalWrite<span class=\"br0\">&#40;<\/span>13, HIGH<span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n<span class=\"kw1\">else<\/span> <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>b <span class=\"sy1\">==<\/span> 0<span class=\"br0\">&#41;<\/span>\n<span class=\"br0\">&#123;<\/span>\n  digitalWrite<span class=\"br0\">&#40;<\/span>13, LOW<span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\nSerial.<span class=\"me1\">flush<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy4\">;<\/span>\n<span class=\"br0\">&#125;<\/span>\n<span class=\"br0\">&#125;<\/span><\/pre><\/div><\/div>\n<p>What the program does it&#8217;s listening on the serial port. And when it\u00a0receives\u00a0a &#8220;1&#8221; pin 13 goes HIGH, and if it\u00a0receives\u00a0&#8220;0&#8221; it goes LOW. Pin 13 is in most cases a LED thats on the arduino board, so you only need a bare arduino to test it. And if you don&#8217;t know any C# and just want to use the program to control a arduino I&#8217;ve created a zip file containing only the .exe file for the program:<\/p>\n<p><a href=\"http:\/\/stigern.net\/forum\/download\/file.php?id=8\" target=\"_blank\">Download\u00a0CSharpEasySerial.zip<\/a><\/p>\n<p><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"http:\/\/www.youtube.com\/embed\/CEYNXhO11r4\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial I&#8217;ll try to show you how to easily tell an arduino what to do from a C# program using serial communication. The only thing the C# program does is send a byte to the arduino, and the arduino program will then set pin 13 to HIGH or LOW. Lets start with the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[14,4,13],"tags":[16,18,242,240,241,26,95,44,53,167,61],"class_list":["post-349","post","type-post","status-publish","format-standard","hentry","category-arduino-electronics","category-electronics","category-windows","tag-arduino","tag-c","tag-com","tag-communation","tag-control","tag-guide","tag-how","tag-port","tag-serial","tag-sharp","tag-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Tutorial: C# Communicating with Arduino over Serial - Stigern.Net<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Tutorial: C# Communicating with Arduino over Serial - Stigern.Net\" \/>\n<meta property=\"og:description\" content=\"In this tutorial I&#8217;ll try to show you how to easily tell an arduino what to do from a C# program using serial communication. The only thing the C# program does is send a byte to the arduino, and the arduino program will then set pin 13 to HIGH or LOW. Lets start with the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/\" \/>\n<meta property=\"og:site_name\" content=\"Stigern.Net\" \/>\n<meta property=\"article:published_time\" content=\"2011-12-19T18:13:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2011-12-20T06:24:10+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg\" \/>\n<meta name=\"author\" content=\"Stigern\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Stigern\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/\",\"url\":\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/\",\"name\":\"Tutorial: C# Communicating with Arduino over Serial - Stigern.Net\",\"isPartOf\":{\"@id\":\"https:\/\/stigern.net\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg\",\"datePublished\":\"2011-12-19T18:13:16+00:00\",\"dateModified\":\"2011-12-20T06:24:10+00:00\",\"author\":{\"@id\":\"https:\/\/stigern.net\/blog\/#\/schema\/person\/a2a77157fa1facae3c843032fbc1ad3c\"},\"breadcrumb\":{\"@id\":\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#primaryimage\",\"url\":\"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg\",\"contentUrl\":\"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/stigern.net\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorial: C# Communicating with Arduino over Serial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/stigern.net\/blog\/#website\",\"url\":\"https:\/\/stigern.net\/blog\/\",\"name\":\"Stigern.Net\",\"description\":\"my little blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/stigern.net\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/stigern.net\/blog\/#\/schema\/person\/a2a77157fa1facae3c843032fbc1ad3c\",\"name\":\"Stigern\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/stigern.net\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/305d159d64fe4a9ee434aed8ee639410?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/305d159d64fe4a9ee434aed8ee639410?s=96&d=mm&r=g\",\"caption\":\"Stigern\"},\"sameAs\":[\"http:\/\/www.stigern.net\"],\"url\":\"https:\/\/stigern.net\/blog\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Tutorial: C# Communicating with Arduino over Serial - Stigern.Net","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/","og_locale":"en_US","og_type":"article","og_title":"Tutorial: C# Communicating with Arduino over Serial - Stigern.Net","og_description":"In this tutorial I&#8217;ll try to show you how to easily tell an arduino what to do from a C# program using serial communication. The only thing the C# program does is send a byte to the arduino, and the arduino program will then set pin 13 to HIGH or LOW. Lets start with the [&hellip;]","og_url":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/","og_site_name":"Stigern.Net","article_published_time":"2011-12-19T18:13:16+00:00","article_modified_time":"2011-12-20T06:24:10+00:00","og_image":[{"url":"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg","type":"","width":"","height":""}],"author":"Stigern","twitter_misc":{"Written by":"Stigern","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/","url":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/","name":"Tutorial: C# Communicating with Arduino over Serial - Stigern.Net","isPartOf":{"@id":"https:\/\/stigern.net\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#primaryimage"},"image":{"@id":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#primaryimage"},"thumbnailUrl":"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg","datePublished":"2011-12-19T18:13:16+00:00","dateModified":"2011-12-20T06:24:10+00:00","author":{"@id":"https:\/\/stigern.net\/blog\/#\/schema\/person\/a2a77157fa1facae3c843032fbc1ad3c"},"breadcrumb":{"@id":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#primaryimage","url":"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg","contentUrl":"http:\/\/stigern.net\/images\/2011\/EasySerial.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/stigern.net\/blog\/tutorial-c-communicating-with-arduino-over-serial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/stigern.net\/blog\/"},{"@type":"ListItem","position":2,"name":"Tutorial: C# Communicating with Arduino over Serial"}]},{"@type":"WebSite","@id":"https:\/\/stigern.net\/blog\/#website","url":"https:\/\/stigern.net\/blog\/","name":"Stigern.Net","description":"my little blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/stigern.net\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/stigern.net\/blog\/#\/schema\/person\/a2a77157fa1facae3c843032fbc1ad3c","name":"Stigern","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/stigern.net\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/305d159d64fe4a9ee434aed8ee639410?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/305d159d64fe4a9ee434aed8ee639410?s=96&d=mm&r=g","caption":"Stigern"},"sameAs":["http:\/\/www.stigern.net"],"url":"https:\/\/stigern.net\/blog\/author\/admin\/"}]}},"_links":{"self":[{"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/posts\/349","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/comments?post=349"}],"version-history":[{"count":12,"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/posts\/349\/revisions"}],"predecessor-version":[{"id":363,"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/posts\/349\/revisions\/363"}],"wp:attachment":[{"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/media?parent=349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/categories?post=349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stigern.net\/blog\/wp-json\/wp\/v2\/tags?post=349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}