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

Problem Detect the presence of SD in Netduino Plus


  • Please log in to reply
8 replies to this topic

#1 BrunoPescarolli

BrunoPescarolli

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationBrazil

Posted 19 July 2012 - 10:23 PM

Hello

I am setting up a code to detect the presence of the Micro Sd with Netduino Plus.

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

public class SD_CARD1

{

    static InterruptPort SDPORTA = new InterruptPort((Cpu.Pin)57, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
   public static void Main()
   {

       OutputPort SD = new OutputPort((Cpu.Pin)25, false);
       InputPort SD_DETECTADO = new InputPort((Cpu.Pin)57, false, Port.ResistorMode.PullUp);

       Debug.Print("SD CARD DETECTADO!" + (SD_DETECTADO.Read().ToString()));
       SD_DETECTADO.Dispose();

       SDPORTA.OnInterrupt += SDPORTA_OnInterrupt;
   }


   static void SDPORTA_OnInterrupt(uint data1, uint data2, DateTime data)
   {
       Debug.Print("SD CARD foi: " + ((!SDPORTA.Read()) ? "LOCALIZADO" : "REMOVIDO"));
   }
}


By compiling the code OK! but when you start the application returns an error writing the Following:
"An unhandled exception of type 'System.Exception' occurred in Microsoft.SPOT.Hardware.dll"

I do not know if there's anything wrong with the code!

but if something wrong can someone help me?

#2 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 20 July 2012 - 08:07 AM

Hi

Try adding a reference to Microsoft.SPOT.IO

Also you could use the events in the RemovableMedia object to detect insert and eject

i.e.
public static void Main(){
 RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
 RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
 Thread.Sleep(Timeout.Infinite);
}
void RemovableMedia_Eject(object sender, MediaEventArgs e) {
 Debug.Print("Ejected");
}

void RemovableMedia_Insert(object sender, MediaEventArgs e) {
 Debug.Print("Inserted");
}

Nak.

#3 ash

ash

    Advanced Member

  • Members
  • PipPipPip
  • 66 posts

Posted 20 July 2012 - 09:35 AM

i just did this:

            DirectoryInfo dirinfo = new DirectoryInfo(@"\SD\Bin");
            if (dirinfo.Exists)
                GotSDCard = true;

:lol:

#4 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 20 July 2012 - 11:41 AM

i just did this:

            DirectoryInfo dirinfo = new DirectoryInfo(@"\SD\Bin");
            if (dirinfo.Exists)
                GotSDCard = true;

:lol:


I would imagine you would get an exception if their is no SD card present, where as using the events is far more reliable (and effcient as no objects are created/garbage collected) way of setting a bool to indicate if a card is present, as the event is triggered by the microswitch on the socket, so even if you were to access the SD card in a binary manner (via SPI or something like that if you have a need for a custom FS/Format) the events would still be raised.

Nak.

#5 BrunoPescarolli

BrunoPescarolli

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationBrazil

Posted 22 July 2012 - 07:57 PM

Help me please! I am new to the area

#6 BrunoPescarolli

BrunoPescarolli

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationBrazil

Posted 22 July 2012 - 09:33 PM

I ran the code that nakchak posted

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

namespace SdCard
{
    public class Program
    {
        public static void Main()
        {
            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
            Thread.Sleep(Timeout.Infinite);
        }
        void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Ejected");
        }

        void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Inserted");
        }

    }
}


The code is perfect but returns errors:
"An object reference is required for the non-static field, method, or property 'SdCard.Program.RemovableMedia_Insert(object, Microsoft.SPOT.IO.MediaEventArgs)'"

"An object reference is required for the non-static field, method, or property 'SdCard.Program.RemovableMedia_Eject(object, Microsoft.SPOT.IO.MediaEventArgs)'"

Should be small thing wrong in the code.
How to solve?

#7 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 23 July 2012 - 06:13 AM

You should make both methods static in this case:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace SdCard
{
    public class Program
    {
        public static void Main()
        {
            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
            Thread.Sleep(Timeout.Infinite);
        }

        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("Ejected");
        }

        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("Inserted");
        }
    }
}

This is because it's in de main program, which is also set up to be static.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#8 BrunoPescarolli

BrunoPescarolli

    Member

  • Members
  • PipPip
  • 14 posts
  • LocationBrazil

Posted 23 July 2012 - 08:35 PM

Hi Stefan the code running OK now! no ERROR's! :D I detect the Sd Card OK? I now have access to it like a removable disk Whether have any link that talks about this?

#9 cSharper

cSharper

    Member

  • Members
  • PipPip
  • 13 posts
  • LocationOrlando, FL

Posted 18 August 2015 - 04:14 PM

Hi all,

 

I know this thread is old, but is there an updated way to perform this exact task?

Right now when I run the sample program given, I get this error:

Inserted
A first chance exception of type 'System.IO.IOException' occurred in Microsoft.SPOT.IO.dll
An unhandled exception of type 'System.IO.IOException' occurred in Microsoft.SPOT.IO.dll


 Uncaught exception 
Ejected
Inserted
Inserted





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.