{"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<p>[codesyntax lang=&#8221;cpp&#8221; lines=&#8221;no&#8221;\u00a0blockstate=&#8221;collapsed&#8221;]<\/p>\n<pre>void setup()\r\n{\r\n  Serial.begin(9600);\r\n  \/\/ Define the led pin as output\r\n  pinMode(13, OUTPUT);\r\n}\r\nvoid loop()\r\n{\r\n  \/\/ Read data from serial communication\r\n  if (Serial.available() &gt; 0) {\r\n  int b = Serial.read();\r\n  \/\/ Note, I made it so that it's more sensitive to 0-40% load.\r\n  \/\/ Since thats where it's usually at when using the computer as normal.\r\nif (b == 1)\r\n{\r\n  digitalWrite(13, HIGH);\r\n}\r\nelse if (b == 0)\r\n{\r\n  digitalWrite(13, LOW);\r\n}\r\nSerial.flush();\r\n}\r\n}<\/pre>\n<p>[\/codesyntax]<\/p>\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":{"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"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_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}]}}