Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Export TXT File from NetDuino Plus to PC


  • Please log in to reply
3 replies to this topic

#1 jgibb311

jgibb311

    New Member

  • Members
  • Pip
  • 3 posts

Posted 25 October 2011 - 06:36 PM

We are needing help transferring a TXT file from the NetDuino Plus micro SD card to a PC so that we can import that file into an Access database. We have successfully created the file on the micro SD card and wrote the data into it using FileStream and StreamWriter, however, now we are having trouble exporting that file to a PC. Also, if this makes things easier, we do not necessarily need the entire file copied over, only the actual contents of the file "file.txt" that is being created.

Here is the code we have so far:

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace CO2MonitoringUnit
{
    public class Program
    {
        public static void Main()
        {
            AnalogInput ap = new AnalogInput(Pins.GPIO_PIN_A0);
            const int MaximumValue = 1024;
            const float AnalogReference = 3.3f;

            while (true)
            {
                int digVal = ap.Read();
                float analogValue = (float)digVal / MaximumValue * AnalogReference;
                string thermValToFile = analogValue.ToString();
                //Thread.Sleep(5000);
                Debug.Print(thermValToFile);
                writeToFile(thermValToFile);
                Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].EnableDhcp();
                WebServer webServer = new WebServer();
                webServer.ListenForRequest();
            }
        }
        public static void writeToFile(string s)
        {
            string fileName = @"\SD\file.txt";
            FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None, 512);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(s);
            System.DateTime.Now.Year.Equals(2011);
            Debug.Print(System.DateTime.Now.Year.ToString());
            sw.WriteLine(System.DateTime.Now.Date.ToString());
            sw.WriteLine(System.DateTime.Now.ToLocalTime().ToString());
            sw.Close();
            fs.Close();
        }
        
    }
}


Here is the code that is running the web server:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.IO;

public class WebServer : IDisposable
{
    //private string readString;
    private Socket socket = null;
    //open connection to onbaord led so we can blink it with every request
    private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
    public WebServer()
    {
        //Initialize Socket class
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        //Request and bind to an IP from DHCP server
        socket.Bind(new IPEndPoint(IPAddress.Any, 80));
        //Debug print our IP address
        Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress);
        //Start listen for web requests
        socket.Listen(10);
        ListenForRequest();
    }

    public void ListenForRequest()
    {
        while (true)
        {
            using (Socket clientSocket = socket.Accept())
            {
                //Get clients IP
                IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                EndPoint clientEndPoint = clientSocket.RemoteEndPoint;
                //int byteCount = cSocket.Available;
                int bytesReceived = clientSocket.Available;
                if (bytesReceived > 0)
                {
                    //Get request
                    byte[] buffer = new byte[bytesReceived];
                    int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);
                    string request = new string(System.Text.Encoding.UTF8.GetChars(buffer));
                    Debug.Print(request);
                    //Compose a response
                    string response = fileHandler.readFromSD();
                    string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                    clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                    clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);
                }
            }
        }
    }

    #region IDisposable Members
    ~WebServer()
    {
        Dispose();
    }
    public void Dispose()
    {
        if (socket != null)
            socket.Close();
    }
    #endregion
}

Edited by Stefan, 25 October 2011 - 07:34 PM.
Added [code] tags


#2 erich

erich

    Member

  • Members
  • PipPip
  • 21 posts
  • LocationFrance

Posted 25 October 2011 - 08:18 PM

Without running your code I am not sure what your problem is. You have 2 pieces of code; data recording and data transfer which need to be threaded to run at the same time. My experience is this can memory issues (which I never resolved though I had other stuff going on to). My solution was to start the webserver in 1 thread then started data recording. Not sure whether you will need to close the file to upload data.

#3 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 25 October 2011 - 08:19 PM

We are needing help transferring a TXT file from the NetDuino Plus micro SD card to a PC so that we can import that file into an Access database. We have successfully created the file on the micro SD card and wrote the data into it using FileStream and StreamWriter, however, now we are having trouble exporting that file to a PC. Also, if this makes things easier, we do not necessarily need the entire file copied over, only the actual contents of the file "file.txt" that is being created.

Have you tried any of the existing web server applications on the forums to see if they meet your needs? The one I posted here could easily be modified to send a text file.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#4 jgibb311

jgibb311

    New Member

  • Members
  • Pip
  • 3 posts

Posted 26 October 2011 - 01:52 AM

Have you tried any of the existing web server applications on the forums to see if they meet your needs? The one I posted here could easily be modified to send a text file.

Regards,
Mark


Thanks, Mark. I'll check it out and let you know if I have any questions!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.