{"id":1497,"date":"2007-05-04T22:23:03","date_gmt":"2007-05-05T01:23:03","guid":{"rendered":"http:\/\/www.hoogervorst.ca\/arthur\/?p=1497"},"modified":"2007-11-14T23:42:40","modified_gmt":"2007-11-15T02:42:40","slug":"serialport-and-c","status":"publish","type":"post","link":"http:\/\/www.hoogervorst.ca\/arthur\/?p=1497","title":{"rendered":"SerialPort and C#"},"content":{"rendered":"<p><span class=\"dropcap\">W<\/span>hat you need to know before you plan to work on Serial port communication:\n<\/p>\n<p>0. Create a good test environment. You may need com0com (<a href=\"http:\/\/www.hoogervorst.ca\/arthur\/?p=1317\">earlier<\/a>) which can simulate a loopback between 2 ports.\n<\/p>\n<p>1. Carefully choose between non-blocking and blocking. You save yourself a lot of pain when you thought using non-blocking was going to make work a lot easier. If you&#8217;re not familiar with the blocking vs. non-blocking approach, consider the table below the fold. (<a href=\"http:\/\/www.developerfusion.co.uk\/show\/28\/8\/\">More on blocking vs. non-blocking<\/a>). Holy wars have been fought between proponents of either form.\n<\/p>\n<p>2. While doing your serial port stuff, also note that ReadLine has a BLOCKING nature. This is not mentioned in the original FrameWork SDK help files, but yes, it is mentioned in the <a href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/system.io.ports.serialport.readline.aspx\">SDK online<\/a>. There&#8217;s no problem using ReadLine in your OnDataReceived event, but you <em>must<\/em> clear the port&#8217;s\/socket buffer first before you can close your port\/application (you&#8217;ll get specific errors).\n<\/p>\n<p>3. There is no 4.\n<\/p>\n<p><!--more--><\/p>\n<table style=\"width:499px;\">\n<tr valign=\"top\">\n<td width=\"30%\">&nbsp;<\/td>\n<td width=\"40%\"><strong>Non-Blocking<\/strong><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Description<\/strong><\/td>\n<td>Typically, you read the data whenever an event is fired, pass the data on to secondary functions\/methods that process and handle your variables.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Pro<\/strong><\/td>\n<td>In easy cases, you just read whatever is available and process everything right from within the &#8216;received event&#8217; of a port\/socket<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Con<\/strong><\/td>\n<td>Tracking variables, states and others can be extremely hard, particularly when data needs to be received, processed constantly.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Sample<\/strong><\/td>\n<td>\n<pre>private void fired_event\r\n{\r\n    string s = serialPort.ReadExisting();\r\n    ParseAndProcessData(s);\r\n}\r\nprivate ParseAndProcessData(string data)\r\n{\r\n    \/\/ For convenience sake...\r\n    this.fpdata = data.SubString(10, 10);\r\n    HandleCommandsState(fpdata);\r\n    SomeText.BeginInvoke(new handler, \r\n       etc, etc);\r\n}\r\n<\/pre>\n<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td width=\"20%\">&nbsp;<\/td>\n<td width=\"40%\"><strong>Blocking<\/strong><\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Description<\/strong><\/td>\n<td>Typically, you read the data at the moment you need it. If there&#8217;s no data available yet, the port (or socket) will wait until it has the data you requested for.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Pro<\/strong><\/td>\n<td>You can do calls to read data from a port or socket right from within your functions, procedures or methods.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Con<\/strong><\/td>\n<td>The moment you start reading\/sending data, the port\/socket will &#8216;block&#8217; until it finds the data you requested for. This may result in &#8216;GUI freezes&#8217; if not handled properly. Only use this when you&#8217;re threading skills are strong.<\/td>\n<\/tr>\n<tr valign=\"top\">\n<td><strong>Sample<\/strong><\/td>\n<td>\n<pre>private ParseAndProcessData()\r\n{\r\n   switch (fstate)\r\n   {\r\n      case TmyState.Nothing :\r\n           \/\/ Stay right there\r\n           break;\r\n      case TmyState.ReadingSocket :\r\n           string s = serialPort.ReadLine();  \r\n      case TmyState.WritingSocket :\r\n           serialPort.WriteLine(\r\n              \"ACKNOWLEDGED\r\n               this, 404, 405, e\");\r\n      case TmyState.Pending :\r\n           serialPort.DiscardInBuffer();\r\n           fstate = tmyState.Nothing\r\n   }\r\n}<\/pre>\n<\/td>\n<\/tr>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>What you need to know before you plan to work on Serial port communication: 0. Create a good test environment. You may need com0com (earlier) which can simulate a loopback between 2 ports. 1. Carefully choose between non-blocking and blocking. &hellip; <a href=\"http:\/\/www.hoogervorst.ca\/arthur\/?p=1497\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[56,88,152],"_links":{"self":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/1497"}],"collection":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1497"}],"version-history":[{"count":0,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=\/wp\/v2\/posts\/1497\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1497"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1497"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.hoogervorst.ca\/arthur\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1497"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}