{"id":6690,"date":"2013-02-14T21:00:40","date_gmt":"2013-02-15T02:00:40","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=6690"},"modified":"2013-02-14T21:13:21","modified_gmt":"2013-02-15T02:13:21","slug":"day-5-writing-to-an-sd-card","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/day-5-writing-to-an-sd-card\/","title":{"rendered":"Netduino Day 5 &#8211; Writing to an SD Card"},"content":{"rendered":"<p>Expandable memory is always a plus no matter whether it\u2019s a Phone, Camera or a Microcontroller. Even advantageous if we know how to use it. In this fifth day Netduino tutorial, we will learn a few writing operations about an SD card. We will learn how to write to a text file, shown as an example of writing a log. The <em>Logger<\/em> class is also capable of creating a text file at any given location then writing some text information to it.<\/p>\n<p><div id=\"attachment_6691\" style=\"width: 190px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6691\" rel=\"attachment wp-att-6691\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6691\" class=\"size-full wp-image-6691\" title=\"Day5_Insert_SD_Card\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/Day5_Insert_SD_Card.gif\" alt=\"\" width=\"180\" height=\"132\" \/><\/a><p id=\"caption-attachment-6691\" class=\"wp-caption-text\">SD Card in Netduino Plus<\/p><\/div><!--more--><\/p>\n<h2>Circuit Setup and Theory<\/h2>\n<p>Well, pop in the SD card to the slot and you are good to go! No messy wires\u2026<\/p>\n<p>When an SD card is inserted into the slot, Netduino Plus will automatically mount it as SD directory. So the root direct is SD. The logger file, by default creates a log at SD\\Report\\logger.txt location. If you need to write to a custom location then use the <em>LogCustom<\/em> method.<\/p>\n<p>After logged, typically at the end of your program, you <strong>MUST<\/strong> call the <em>Close<\/em> method otherwise information will not be saved unless you call <em>Flush<\/em> method from the <em>Logger<\/em> class.<\/p>\n<h2>C# .NET Program<\/h2>\n<p>This tutorial is mainly around programming as there is nothing to connect to our Netduino. Writing to a text file (or logging) is done via the <em>Logger<\/em>class. To make life little simpler, there are some static properties and methods which we can be called directly to write to an SD card. Let\u2019s look at the Class Diagram of the <em>Logger<\/em> class. The highlighted methods\/properties are static members.<\/p>\n<div id=\"attachment_6692\" style=\"width: 245px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6692\" rel=\"attachment wp-att-6692\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6692\" class=\"size-full wp-image-6692\" title=\"LoggerClassDiagram\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/LoggerClassDiagram.png\" alt=\"\" width=\"235\" height=\"390\" \/><\/a><p id=\"caption-attachment-6692\" class=\"wp-caption-text\">Logger Class Diagram<\/p><\/div>\n<p>Let\u2019s look at some of the key methods of this class such as Log, StreamWriter, etc. In the method, we simply put together all the arguments into one string and then it calls WriteLog.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"csharp\" style=\"font-family:monospace;\"><span style=\"color: #0600FF; font-weight: bold;\">public<\/span> <span style=\"color: #0600FF; font-weight: bold;\">static<\/span> <span style=\"color: #6666cc; font-weight: bold;\">void<\/span> Log<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #0600FF; font-weight: bold;\">params<\/span> <span style=\"color: #6666cc; font-weight: bold;\">object<\/span><span style=\"color: #008000;\">&#91;<\/span><span style=\"color: #008000;\">&#93;<\/span> strings<span style=\"color: #008000;\">&#41;<\/span>\n<span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> message <span style=\"color: #008000;\">=<\/span> <span style=\"color: #6666cc; font-weight: bold;\">string<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Empty<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">for<\/span> <span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">int<\/span> i <span style=\"color: #008000;\">=<\/span> <span style=\"color: #FF0000;\">0<\/span><span style=\"color: #008000;\">;<\/span> i <span style=\"color: #008000;\">&lt;<\/span> strings<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Length<\/span><span style=\"color: #008000;\">;<\/span> i<span style=\"color: #008000;\">++<\/span><span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        message <span style=\"color: #008000;\">=<\/span> message <span style=\"color: #008000;\">+<\/span> strings<span style=\"color: #008000;\">&#91;<\/span>i<span style=\"color: #008000;\">&#93;<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">ToString<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #008000;\">+<\/span> <span style=\"color: #666666;\">&quot; &quot;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    WriteLog<span style=\"color: #008000;\">&#40;<\/span>message, StreamWriter, PrefixDateTime, LogToFile <span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n<span style=\"color: #008000;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>One of the arguments used is StreamWriter which is a property and it creates a new instance of StreamWriter when called for the first time.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"csharp\" style=\"font-family:monospace;\"><span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #0600FF; font-weight: bold;\">static<\/span> StreamWriter StreamWriter\n<span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">get<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>_streamWriter <span style=\"color: #008000;\">==<\/span> <span style=\"color: #0600FF; font-weight: bold;\">null<\/span><span style=\"color: #008000;\">&#41;<\/span> _streamWriter <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> StreamWriter<span style=\"color: #008000;\">&#40;<\/span>LogFilePath,<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">bool<\/span><span style=\"color: #008000;\">&#41;<\/span>Append<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">return<\/span> _streamWriter<span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n<span style=\"color: #008000;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>The WriteLog method, it first adds the time stamp if required then calls the WriteLine method of a stream writer. Note that Netduino does not have internal clock so time logged by Netduino will not be same as clock time.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"csharp\" style=\"font-family:monospace;\"><span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #0600FF; font-weight: bold;\">static<\/span> <span style=\"color: #6666cc; font-weight: bold;\">void<\/span> WriteLog<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">string<\/span> message, StreamWriter streamWriter, <span style=\"color: #6666cc; font-weight: bold;\">bool<\/span> addDateTime, <span style=\"color: #6666cc; font-weight: bold;\">bool<\/span> logToFile<span style=\"color: #008000;\">&#41;<\/span>\n<span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>addDateTime<span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        DateTime current <span style=\"color: #008000;\">=<\/span> DateTime<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Now<\/span><span style=\"color: #008000;\">;<\/span>\n        message <span style=\"color: #008000;\">=<\/span> <span style=\"color: #666666;\">&quot;[&quot;<\/span> <span style=\"color: #008000;\">+<\/span> current <span style=\"color: #008000;\">+<\/span> <span style=\"color: #666666;\">&quot;:&quot;<\/span> <span style=\"color: #008000;\">+<\/span> current<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Millisecond<\/span> <span style=\"color: #008000;\">+<\/span> <span style=\"color: #666666;\">&quot;] &quot;<\/span> <span style=\"color: #008000;\">+<\/span> message<span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n    Debug<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Print<\/span><span style=\"color: #008000;\">&#40;<\/span>message<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>logToFile<span style=\"color: #008000;\">&#41;<\/span> streamWriter<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">WriteLine<\/span><span style=\"color: #008000;\">&#40;<\/span>message<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n<span style=\"color: #008000;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<h2>Output<\/h2>\n<p>When we directly call the <em>Logger<\/em> Class and start sending logs like below (code), the output may look like the image below.<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"csharp\" style=\"font-family:monospace;\"><span style=\"color: #008080; font-style: italic;\">\/\/ Directly start logging, no need to create any instance of Logger class<\/span>\nLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">LogToFile<\/span> <span style=\"color: #008000;\">=<\/span> <span style=\"color: #0600FF; font-weight: bold;\">true<\/span><span style=\"color: #008000;\">;<\/span>    <span style=\"color: #008080; font-style: italic;\">\/\/ if false it will only do Debug.Print()<\/span>\nLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Append<\/span> <span style=\"color: #008000;\">=<\/span> <span style=\"color: #0600FF; font-weight: bold;\">true<\/span><span style=\"color: #008000;\">;<\/span>       <span style=\"color: #008080; font-style: italic;\">\/\/ will append the information to existing if any<\/span>\nLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">PrefixDateTime<\/span> <span style=\"color: #008000;\">=<\/span> <span style=\"color: #0600FF; font-weight: bold;\">true<\/span><span style=\"color: #008000;\">;<\/span> <span style=\"color: #008080; font-style: italic;\">\/\/ add a time stamp on each Log call. Note: Netduino time is not same as clock time.<\/span>\n&nbsp;\n<span style=\"color: #008080; font-style: italic;\">\/\/ any number of arguments can be passed. They will appended by a white space<\/span>\nLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Log<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;All&quot;<\/span>, <span style=\"color: #666666;\">&quot;these&quot;<\/span>, <span style=\"color: #666666;\">&quot;will&quot;<\/span>, <span style=\"color: #666666;\">&quot;be&quot;<\/span>, <span style=\"color: #666666;\">&quot;combined&quot;<\/span>, <span style=\"color: #666666;\">&quot;in&quot;<\/span>, <span style=\"color: #666666;\">&quot;to&quot;<\/span>, <span style=\"color: #666666;\">&quot;one&quot;<\/span>, <span style=\"color: #666666;\">&quot;string&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\nLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Log<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;This should go into the second line.&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\nDebug<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Print<\/span><span style=\"color: #008000;\">&#40;<\/span>Logger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">LogFilePath<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<div id=\"attachment_6693\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6693\" rel=\"attachment wp-att-6693\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6693\" class=\"size-full wp-image-6693\" title=\"Wrte to SD Card_output\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/Wrte-to-SD-Card_output.png\" alt=\"\" width=\"550\" height=\"235\" \/><\/a><p id=\"caption-attachment-6693\" class=\"wp-caption-text\">Output of Logger.Log Method<\/p><\/div>\n<p>Here is an example of writing to a custom location:<\/p>\n\n<div class=\"wp_syntax\"><table><tr><td class=\"code\"><pre class=\"csharp\" style=\"font-family:monospace;\"><span style=\"color: #008080; font-style: italic;\">\/\/ Create an instance of Logger if you need to write to a custom location.<\/span>\nLogger customLogger <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> Logger<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">@&quot;One\\OneOne&quot;<\/span>, <span style=\"color: #666666;\">&quot;one.txt&quot;<\/span>, <span style=\"color: #0600FF; font-weight: bold;\">true<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\ncustomLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">CustomPrefixDateTime<\/span> <span style=\"color: #008000;\">=<\/span> <span style=\"color: #0600FF; font-weight: bold;\">false<\/span><span style=\"color: #008000;\">;<\/span>\ncustomLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">CustomLogToFile<\/span> <span style=\"color: #008000;\">=<\/span> <span style=\"color: #0600FF; font-weight: bold;\">true<\/span><span style=\"color: #008000;\">;<\/span>\ncustomLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">LogCustom<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;All&quot;<\/span>, <span style=\"color: #666666;\">&quot;these&quot;<\/span>, <span style=\"color: #666666;\">&quot;will&quot;<\/span>, <span style=\"color: #666666;\">&quot;be&quot;<\/span>, <span style=\"color: #666666;\">&quot;combined&quot;<\/span>, <span style=\"color: #666666;\">&quot;in&quot;<\/span>, <span style=\"color: #666666;\">&quot;to&quot;<\/span>, <span style=\"color: #666666;\">&quot;one&quot;<\/span>, <span style=\"color: #666666;\">&quot;string&quot;<\/span>, <span style=\"color: #666666;\">&quot;-CustomLogger1.&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\nDebug<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Print<\/span><span style=\"color: #008000;\">&#40;<\/span>customLogger<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">CustomFilePath<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<div id=\"attachment_6694\" style=\"width: 556px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6694\" rel=\"attachment wp-att-6694\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6694\" class=\"size-full wp-image-6694\" title=\"Wrte to SD Card_outputCustom\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/Wrte-to-SD-Card_outputCustom.png\" alt=\"\" width=\"546\" height=\"227\" \/><\/a><p id=\"caption-attachment-6694\" class=\"wp-caption-text\">Output of Logger.LogCustom Method<\/p><\/div>\n<h2>Downlaod<\/h2>\n<p>1)\u00a0\u00a0\u00a0\u00a0\u00a0 <a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2013\/02\/EmbeddedLab.NetduinoPlus.Day5_.zip\" target=\"_blank\">C# .NET Code (Solution File)<\/a><\/p>\n<h2>What Next<\/h2>\n<p>Right now, if you need to check what\u2019s written in the SD card, you have to physically take out the card and then insert it into the computer, which obviously, you don\u2019t want to do more frequently. So, we will learn about reading rather sending the information from the text file to computer via Serial Communication, no need to take the card out!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Expandable memory is always a plus no matter whether it\u2019s a Phone, Camera or a Microcontroller. Even advantageous if we know how to use it. In this fifth day Netduino tutorial, we will learn a few writing operations about an SD card. We will learn how to write to a text file, shown as an example of writing a log. The Logger class is also capable of creating a text file at any given location then writing some text information to it.<\/p>\n","protected":false},"author":4,"featured_media":6691,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,495],"tags":[540,36,1316,500,530,541],"class_list":["post-6690","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-lessons","category-netduino","tag-day-5","tag-logger","tag-netduino","tag-netduino-plus","tag-sd-card","tag-write-to-sd-card"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6690","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=6690"}],"version-history":[{"count":21,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6690\/revisions"}],"predecessor-version":[{"id":7130,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6690\/revisions\/7130"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/6691"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=6690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=6690"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=6690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}