{"id":6801,"date":"2013-04-01T20:51:21","date_gmt":"2013-04-02T00:51:21","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=6801"},"modified":"2013-04-01T23:10:07","modified_gmt":"2013-04-02T03:10:07","slug":"day-6-reading-sd-card-via-serial-port","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/day-6-reading-sd-card-via-serial-port\/","title":{"rendered":"Netduino Day 6 &#8211; Read SD card and send info to a Serial Port"},"content":{"rendered":"<p>In our earlier tutorial, <a href=\"http:\/\/embedded-lab.com\/blog\/?p=6690\" target=\"_blank\"><strong>Writing to an SD Card<\/strong><\/a>, we learned to write to an SD card. As you might have experienced, in order to see what\u2019s been written, we need to pop out the SD then connect it to a computer, which obviously is not very convenient all the time. So, in this tutorial we will read a text file (same file\/information that we wrote in <a href=\"http:\/\/embedded-lab.com\/blog\/?p=6690\" target=\"_blank\">previous tutorial<\/a>) and send that text to a computer. Our communication between Netduino SD card and Computer is established via Serial Communication using USB to UART-TTL device. So, the information will be transferred to a computer in a Component Object Model (COM) port.<\/p>\n<div id=\"attachment_6802\" style=\"width: 568px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6802\" rel=\"attachment wp-att-6802\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6802\" class=\"size-full wp-image-6802\" title=\"Read SD Card Output\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/Read-SD-Card-Output.png\" alt=\"\" width=\"558\" height=\"583\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/Read-SD-Card-Output.png 558w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/Read-SD-Card-Output-550x574.png 550w\" sizes=\"auto, (max-width: 558px) 100vw, 558px\" \/><\/a><p id=\"caption-attachment-6802\" class=\"wp-caption-text\">Text file content form SD card<\/p><\/div>\n<p>There are two major parts of this tutorial; first reading the data from SD Card and sending it to a computer, second an application listening to a COM port to retrieve the data. Besides these, in this tutorial we will touch on following major objects:<\/p>\n<ul>\n<li>Serial Communication<\/li>\n<li>InterruptPort<\/li>\n<li>EventHandler (Native and SerialDataReceived)<\/li>\n<li>StreamReader<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h2>Circuit Setup and Theory<\/h2>\n<p>Since the computer I am using doesn\u2019t have a serial port (most of the computer these days don\u2019t have one) so a USB UART (Universal Asynchronous Receiver-Transmitter) adapter (purchased <a href=\"http:\/\/cgi.ebay.com\/ws\/eBayISAPI.dll?ViewItem&amp;item=370532286388\">here<\/a>) is used which enable us to send serial data to a computer via a USB.<\/p>\n<div id=\"attachment_6804\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6804\" rel=\"attachment wp-att-6804\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6804\" class=\"size-full wp-image-6804\" title=\"USB UART Connecion with Netduino\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/USB-UART-Connecion-with-Netduino.png\" alt=\"\" width=\"550\" height=\"298\" \/><\/a><p id=\"caption-attachment-6804\" class=\"wp-caption-text\">USB UART Connecion with Netduino<\/p><\/div>\n<p>Netduino natively supports UART RX and TX on pins digital 0 and 1 pins. So, the pin 0 of Netduino goes to RXD and pin 1 is connected to TXD of the USB-UART device. When the information is sent from Netduino to USB UART device, we need a computer application to harness that information from the COM port. In order to receive such information I wrote a COM Talk tool.<\/p>\n<h2>COM Talk (Computer Application)<\/h2>\n<p>Let\u2019s create an application that can receive information from COM ports. I also added some functionality so that it can send information via selected COM port, however, we will not discuss on that part. I will not go into the details about how this tools works as it is more .NET stuff (than programming on microcontroller). This application can be used to receive information on any COM port so it&#8217;s not limited to this tutorial. This is how the UI of this tool looks like.<\/p>\n<div id=\"attachment_6805\" style=\"width: 555px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6805\" rel=\"attachment wp-att-6805\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6805\" class=\"size-full wp-image-6805\" title=\"COM Talk Application\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/COM-Talk-Application.png\" alt=\"\" width=\"545\" height=\"576\" \/><\/a><p id=\"caption-attachment-6805\" class=\"wp-caption-text\">StandAlone application listening to COM Ports<\/p><\/div>\n<p>The Input section of COM Talk will show the information received in selected COM port. Internally, bytes received are converted to string and displayed. The Output section allows sending information to COM port. This section is not used in the tutorial. We need to have this tool running whenever we want to receive data on a COM port.<br \/>\nHere\u2019s the entire code related to this COM Talk.<\/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;\">partial<\/span> <span style=\"color: #6666cc; font-weight: bold;\">class<\/span> COMInteractionForm <span style=\"color: #008000;\">:<\/span> Form\n<span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #008080;\">#region Constructor<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">public<\/span> COMInteractionForm<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        InitializeComponent<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        selectedSerialPort <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> SerialPort<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        InitializeEvents<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n&nbsp;\n    <span style=\"color: #008080;\">#region Private Methods<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">void<\/span> InitializeEvents<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Icon<\/span> <span style=\"color: #008000;\">=<\/span> Properties<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Resources<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Talk<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">buttonReloadCOMs<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Click<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> EventHandler<span style=\"color: #008000;\">&#40;<\/span>buttonReloadCOMs_Click<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">selectedSerialPort<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">DataReceived<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> SerialDataReceivedEventHandler<span style=\"color: #008000;\">&#40;<\/span>selectedSerialPort_DataReceived<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n&nbsp;\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">comboBoxCOMPorts<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">SelectedValueChanged<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> EventHandler<span style=\"color: #008000;\">&#40;<\/span>comboBoxCOMPorts_SelectedValueChanged<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        LoadAvailableCOMPorts<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">buttonSend<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Click<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> EventHandler<span style=\"color: #008000;\">&#40;<\/span>buttonSend_Click<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">richTextBoxOutput<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">TextChanged<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> EventHandler<span style=\"color: #008000;\">&#40;<\/span>richTextBoxOutput_TextChanged<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">FormClosing<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> FormClosingEventHandler<span style=\"color: #008000;\">&#40;<\/span>COMInteractionForm_FormClosing<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">labelEmbeddedLab<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Click<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> EventHandler<span style=\"color: #008000;\">&#40;<\/span>labelEmbeddedLab_Click<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">void<\/span> LoadAvailableCOMPorts<span style=\"color: #008000;\">&#40;<\/span><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><span style=\"color: #008000;\">!<\/span>COMPortsExists<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #0600FF; font-weight: bold;\">return<\/span><span style=\"color: #008000;\">;<\/span>\n&nbsp;\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">comboBoxCOMPorts<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Items<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Clear<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">foreach<\/span> <span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">string<\/span> portName <span style=\"color: #0600FF; font-weight: bold;\">in<\/span> SerialPort<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">GetPortNames<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n        <span style=\"color: #008000;\">&#123;<\/span>\n            <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">comboBoxCOMPorts<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Items<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0600FF; font-weight: bold;\">Add<\/span><span style=\"color: #008000;\">&#40;<\/span>portName<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n        <span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">comboBoxCOMPorts<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">SelectedItem<\/span> <span style=\"color: #008000;\">=<\/span> SerialPort<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">GetPortNames<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#91;<\/span><span style=\"color: #FF0000;\">0<\/span><span style=\"color: #008000;\">&#93;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">bool<\/span> COMPortsExists<span style=\"color: #008000;\">&#40;<\/span><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>SerialPort<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">GetPortNames<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Length<\/span>  <span style=\"color: #FF0000;\">0<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">void<\/span> buttonSend_Click<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">object<\/span> sender, EventArgs e<span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        selectedSerialPort<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">WriteLine<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #0600FF; font-weight: bold;\">this<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">richTextBoxOutput<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Text<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">void<\/span> COMInteractionForm_FormClosing<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">object<\/span> sender, FormClosingEventArgs e<span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        ClosePort<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #0600FF; font-weight: bold;\">true<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">void<\/span> labelEmbeddedLab_Click<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">object<\/span> sender, EventArgs e<span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        <span style=\"color: #000000;\">System.<span style=\"color: #0000FF;\">Diagnostics<\/span><\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Process<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Start<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;http:\/\/www.Embedded-Lab.com&quot;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n&nbsp;\n    <span style=\"color: #008080;\">#region Fields<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> SerialPort selectedSerialPort<span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> textReceived<span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n<span style=\"color: #008000;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<h2>C#.NET Program<\/h2>\n<p>Let\u2019s change the gear and learn how we can send information to a COM port from a Netduino. As mentioned earlier, Netduino has some dedicated Digital I\/O for serial communication and we are going to use digital port 0 and port 1 of Netduino. The code is written in such a way that it will only send the text file information when the on board button is pressed.<\/p>\n<p>To facilitate the information transmission via a serial port there are <em>Reader<\/em> and <em>SDCard<\/em> classes created.<\/p>\n<h3>Reader Class<\/h3>\n<p>The <em>Reader<\/em> class basically uses <em>StreamReader<\/em> and calls its <em>ReadToEnd<\/em> method.<\/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: #6666cc; font-weight: bold;\">class<\/span> Reader\n<span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #008080;\">#region Constructor<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">public<\/span> Reader<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">string<\/span> textFileFullPath<span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        TextFileFullPath <span style=\"color: #008000;\">=<\/span> textFileFullPath<span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n&nbsp;\n    <span style=\"color: #008080;\">#region Public Methods<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">public<\/span> <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> Read<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> text<span style=\"color: #008000;\">;<\/span>\n&nbsp;\n        <span style=\"color: #0600FF; font-weight: bold;\">try<\/span>\n        <span style=\"color: #008000;\">&#123;<\/span>\n            <span style=\"color: #0600FF; font-weight: bold;\">using<\/span> <span style=\"color: #008000;\">&#40;<\/span>StreamReader reader <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> StreamReader<span style=\"color: #008000;\">&#40;<\/span>TextFileFullPath<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n            <span style=\"color: #008000;\">&#123;<\/span>\n                text <span style=\"color: #008000;\">=<\/span> reader<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">ReadToEnd<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n            <span style=\"color: #008000;\">&#125;<\/span>\n        <span style=\"color: #008000;\">&#125;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">catch<\/span> <span style=\"color: #008000;\">&#40;<\/span>Exception e<span style=\"color: #008000;\">&#41;<\/span>\n        <span style=\"color: #008000;\">&#123;<\/span>\n            <span style=\"color: #0600FF; font-weight: bold;\">throw<\/span> <span style=\"color: #008000;\">new<\/span> Exception<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #666666;\">&quot;Failed to read &quot;<\/span> <span style=\"color: #008000;\">+<\/span> e<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Message<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #008000;\">&#125;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">return<\/span> text<span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n&nbsp;\n    <span style=\"color: #008080;\">#region Private Properties<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">private<\/span> <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> TextFileFullPath <span style=\"color: #008000;\">&#123;<\/span> <span style=\"color: #0600FF; font-weight: bold;\">get<\/span><span style=\"color: #008000;\">;<\/span> <span style=\"color: #0600FF; font-weight: bold;\">set<\/span><span style=\"color: #008000;\">;<\/span> <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n<span style=\"color: #008000;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<h3>SDCard Class<\/h3>\n<p>We don\u2019t actually need an <em>SDCard<\/em> class however I am making one as we can easily add more and more functionality to this class. This class basically comprises two public properties.<\/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: #6666cc; font-weight: bold;\">class<\/span> SDCard\n<span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #008080;\">#region Constructor<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">public<\/span> SDCard<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n&nbsp;\n    <span style=\"color: #008080;\">#region Public Properties<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">public<\/span> <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> RootPath <span style=\"color: #008000;\">&#123;<\/span> <span style=\"color: #0600FF; font-weight: bold;\">get<\/span> <span style=\"color: #008000;\">&#123;<\/span> <span style=\"color: #0600FF; font-weight: bold;\">return<\/span> <span style=\"color: #666666;\">&quot;SD&quot;<\/span><span style=\"color: #008000;\">;<\/span> <span style=\"color: #008000;\">&#125;<\/span> <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #0600FF; font-weight: bold;\">public<\/span> <span style=\"color: #6666cc; font-weight: bold;\">bool<\/span> Readable <span style=\"color: #008000;\">&#123;<\/span> <span style=\"color: #0600FF; font-weight: bold;\">get<\/span> <span style=\"color: #008000;\">&#123;<\/span> <span style=\"color: #0600FF; font-weight: bold;\">return<\/span> <span style=\"color: #000000;\">System.<span style=\"color: #0000FF;\">IO<\/span><\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Directory<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Exists<\/span><span style=\"color: #008000;\">&#40;<\/span>RootPath<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span> <span style=\"color: #008000;\">&#125;<\/span> <span style=\"color: #008000;\">&#125;<\/span>\n    <span style=\"color: #008080;\">#endregion<\/span>\n<span style=\"color: #008000;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>Now, we have these two helper classes defined; let\u2019s look at our main program. In the <em>Main<\/em> method, we have created some instances of <em>OutputPort<\/em>, <em>SerialPort<\/em>, <em>InterupPort<\/em>, and subscribed to <em>NativeEventHandler<\/em> for <em>OnInterrup<\/em> event of on board button, and also to <em>DataReceived<\/em> event of <em>SerialPort<\/em> class. We then open up the serial port and let the main thread to sleep forever.<\/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> Main<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span>\n<span style=\"color: #008000;\">&#123;<\/span>\n    onBoardLed <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> OutputPort<span style=\"color: #008000;\">&#40;<\/span>Pins<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">ONBOARD_LED<\/span>, <span style=\"color: #0600FF; font-weight: bold;\">false<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    port <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> SerialPort<span style=\"color: #008000;\">&#40;<\/span>SerialPorts<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">COM1<\/span>, <span style=\"color: #FF0000;\">9600<\/span>, Parity<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">None<\/span>, <span style=\"color: #FF0000;\">8<\/span>, StopBits<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">One<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    bufferSize <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> <span style=\"color: #6666cc; font-weight: bold;\">byte<\/span><span style=\"color: #008000;\">&#91;<\/span><span style=\"color: #FF0000;\">2048<\/span><span style=\"color: #008000;\">&#93;<\/span><span style=\"color: #008000;\">;<\/span>            \n&nbsp;\n    InterruptPort onBoardButton <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> InterruptPort<span style=\"color: #008000;\">&#40;<\/span>Pins<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">ONBOARD_SW1<\/span>, <span style=\"color: #0600FF; font-weight: bold;\">false<\/span>, Port<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">ResistorMode<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Disabled<\/span>, Port<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">InterruptMode<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">InterruptEdgeBoth<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    onBoardButton<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">OnInterrupt<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> NativeEventHandler<span style=\"color: #008000;\">&#40;<\/span>button_OnInterrupt<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    port<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">DataReceived<\/span> <span style=\"color: #008000;\">+=<\/span> <span style=\"color: #008000;\">new<\/span> SerialDataReceivedEventHandler<span style=\"color: #008000;\">&#40;<\/span>port_DataReceived<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n&nbsp;\n    <span style=\"color: #0600FF; font-weight: bold;\">if<\/span> <span style=\"color: #008000;\">&#40;<\/span>port<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">IsOpen<\/span><span style=\"color: #008000;\">&#41;<\/span> port<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Close<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    port<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Open<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n&nbsp;\n    Thread<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Sleep<\/span><span style=\"color: #008000;\">&#40;<\/span>Timeout<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Infinite<\/span><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>The moment, we press the on board button, an interrupt event will get fired. Since we have subscribed to that event, program will jump to the following event handler;<\/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> button_OnInterrupt<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">uint<\/span> data1, <span style=\"color: #6666cc; font-weight: bold;\">uint<\/span> data2, DateTime time<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>data2 <span style=\"color: #008000;\">==<\/span> <span style=\"color: #FF0000;\">0<\/span><span style=\"color: #008000;\">&#41;<\/span> <span style=\"color: #008080; font-style: italic;\">\/\/ button pressed?<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        onBoardLed<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Write<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #0600FF; font-weight: bold;\">true<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n&nbsp;\n        <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> filePath <span style=\"color: #008000;\">=<\/span> <span style=\"color: #666666;\">&quot;Report&quot;<\/span> <span style=\"color: #008000;\">+<\/span> Path<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">DirectorySeparatorChar<\/span> <span style=\"color: #008000;\">+<\/span> <span style=\"color: #666666;\">&quot;Log.txt&quot;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #6666cc; font-weight: bold;\">string<\/span> data <span style=\"color: #008000;\">=<\/span> ReadDataFromFile<span style=\"color: #008000;\">&#40;<\/span>filePath<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>data <span style=\"color: #008000;\">!=<\/span> <span style=\"color: #0600FF; font-weight: bold;\">null<\/span><span style=\"color: #008000;\">&#41;<\/span>\n            SendDataUsingCOMPort<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #000000;\">System.<span style=\"color: #0000FF;\">Text<\/span><\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Encoding<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">UTF8<\/span><span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">GetBytes<\/span><span style=\"color: #008000;\">&#40;<\/span>data<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n&nbsp;\n        onBoardLed<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Write<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #0600FF; font-weight: bold;\">false<\/span><span style=\"color: #008000;\">&#41;<\/span><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 second argument in above code, data2, contains the state of the button. The value of data2 becomes 0 when the button is pressed. So, at this state (data2 equals to 0) we need to read the text file and then send the information to a serial port. As it can be seen, the routine first calls <em>ReadDataFromFile<\/em> function by passing the path of the file to read to get the text file content.<\/p>\n<p>In the ReadDataFromFile function, it first creates an instance of an SDCard class then creates another instance of Reader class, only when SD card is readable. The Read method of reader class return the text of the given text file path.<\/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;\">string<\/span> ReadDataFromFile<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">string<\/span> filePath<span style=\"color: #008000;\">&#41;<\/span>\n<span style=\"color: #008000;\">&#123;<\/span>\n    SDCard sdCard <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> SDCard<span style=\"color: #008000;\">&#40;<\/span><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>sdCard<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Readable<\/span><span style=\"color: #008000;\">&#41;<\/span>\n    <span style=\"color: #008000;\">&#123;<\/span>\n        Reader reader <span style=\"color: #008000;\">=<\/span> <span style=\"color: #008000;\">new<\/span> Reader<span style=\"color: #008000;\">&#40;<\/span>sdCard<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">RootPath<\/span> <span style=\"color: #008000;\">+<\/span> Path<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">DirectorySeparatorChar<\/span> <span style=\"color: #008000;\">+<\/span> filePath<span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n        <span style=\"color: #0600FF; font-weight: bold;\">return<\/span> reader<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Read<\/span><span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #008000;\">&#41;<\/span><span style=\"color: #008000;\">;<\/span>\n    <span style=\"color: #008000;\">&#125;<\/span>\n&nbsp;\n    <span style=\"color: #0600FF; font-weight: bold;\">return<\/span> <span style=\"color: #0600FF; font-weight: bold;\">null<\/span><span style=\"color: #008000;\">;<\/span>\n<span style=\"color: #008000;\">&#125;<\/span><\/pre><\/td><\/tr><\/table><\/div>\n\n<p>Now, in the <em>button_OnInterrupt<\/em> method, we can see, it then calls <em>SendDataUsingCOMPort<\/em> and passes the corresponding bytes of the text that was read from SD card. In the <em>SendDataUsingCOMPort<\/em> we simply call the <em>Write<\/em> method of the serial port.<\/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> SendDataUsingCOMPort<span style=\"color: #008000;\">&#40;<\/span><span style=\"color: #6666cc; font-weight: bold;\">byte<\/span><span style=\"color: #008000;\">&#91;<\/span><span style=\"color: #008000;\">&#93;<\/span> data<span style=\"color: #008000;\">&#41;<\/span>\n<span style=\"color: #008000;\">&#123;<\/span>\n    port<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Write<\/span><span style=\"color: #008000;\">&#40;<\/span>data, <span style=\"color: #FF0000;\">0<\/span>, data<span style=\"color: #008000;\">.<\/span><span style=\"color: #0000FF;\">Length<\/span><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>Using the <em>SerialPort<\/em> class we were able to send the data to COM1 of Netduino by calling <em>Write<\/em> method. And the COM Talk standalone program was able to faithfully display the incoming data to a COM port.<\/p>\n<div id=\"attachment_6806\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=6806\" rel=\"attachment wp-att-6806\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6806\" class=\"size-full wp-image-6806\" title=\"USB UART Connecion with Netduino\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/USB-UART-Connecion-with-Netduino.jpg\" alt=\"\" width=\"550\" height=\"508\" \/><\/a><p id=\"caption-attachment-6806\" class=\"wp-caption-text\">USB UART Connecion with Netduino<\/p><\/div>\n<p>Here is a small video of sending data from Netduino to computer.<br \/>\n<iframe loading=\"lazy\" src=\"http:\/\/www.youtube.com\/embed\/20wVE5bVdu8\" frameborder=\"0\" width=\"580\" height=\"436\"><\/iframe><\/p>\n<h2>Download<\/h2>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2012\/12\/EmbeddedLab.NetduinoPlus.Day6_.ReadSDCard.zip\" target=\"_blank\">C# .NET Code (Solution File)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our earlier tutorial, Writing to an SD Card, we learned to write to an SD card. As you might have experienced, in order to see what\u2019s been written, we need to pop out the SD then connect it to a computer, which obviously is not very convenient all the time. So, in this tutorial we will read a text file (same file\/information that we wrote in previous tutorial) and send that text to a computer. Our communication between Netduino SD card and Computer is established via Serial Communication using USB to UART-TTL device. So, the information will be transferred<\/p>\n","protected":false},"author":4,"featured_media":6802,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29,495],"tags":[532,1316,500,554,555,556,530,143,531],"class_list":["post-6801","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-lessons","category-netduino","tag-interroptport","tag-netduino","tag-netduino-plus","tag-on-board-switch","tag-on-oboard-led","tag-read-sd-card","tag-sd-card","tag-serial-communication","tag-serialport"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6801","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=6801"}],"version-history":[{"count":41,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6801\/revisions"}],"predecessor-version":[{"id":7320,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/6801\/revisions\/7320"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/6802"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=6801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=6801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=6801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}