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

How to reboot netduino + from code on a socket exception?


  • Please log in to reply
7 replies to this topic

#1 samjones

samjones

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts

Posted 29 January 2012 - 10:04 PM

So once in awhile, netduino can't get on the network,
Soooo, how can I, in code, re-init the enet on such an exception?

Ideally, I can do this in line in the code, so I can keep the debugger attached.

But if a reboot is needed, so be it... tell me how to do that from code....

thx!!

Example of exception below:


'System.Net.Sockets.Socket.Send'
    #### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
    #### Message: 
    #### Microsoft.SPOT.Net.SocketNative::send [IP: 0000] ####
    #### System.Net.Sockets.Socket::Send [IP: 0018] ####
    #### System.Net.Sockets.NetworkStream::Write [IP: 0051] ####
    #### System.Net.InputNetworkStreamWrapper::Write [IP: 000a] ####
    #### System.Net.HttpWebRequest::SubmitRequest [IP: 0077] ####
    #### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
    #### NetduinoPlusApplication1.Program::ProcessRequest [IP: 0016] ####
    #### NetduinoPlusApplication1.Program::GetBusStatus [IP: 0005] ####
    #### NetduinoPlusApplication1.Program::Main [IP: 004c] ####
    #### SocketException ErrorCode = 10054
    #### SocketException ErrorCode = 10054
A first chance exception of type 'System.Net.Sockets.SocketException' occurred in Microsoft.SPOT.Net.dll
    #### SocketException ErrorCode = 10054
    #### SocketException ErrorCode = 10054


#2 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

Posted 31 January 2012 - 03:23 AM

You could put a try/catch around your code and in the catch call PowerState.RebootDevice -Valkyrie-MT

#3 ColinR

ColinR

    Advanced Member

  • Members
  • PipPipPip
  • 142 posts
  • LocationCape Town, South Africa

Posted 31 January 2012 - 05:08 AM

The trouble you will face is that sometimes the exception is genuine, and sometimes it's due to the networking stack needing a reboot. The method I implemented in the end was an exception count, where after x exceptions, reboot.

#4 samjones

samjones

    Advanced Member

  • Members
  • PipPipPip
  • 105 posts

Posted 31 January 2012 - 06:17 AM

Colin, Is there a class out there to easily log exceptions to the sd card? I want to get a picture of what is going on when I am at work.... Thoughts? Thx!

#5 Arbiter

Arbiter

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationBrisbane, Australia

Posted 07 February 2012 - 12:33 AM

I wonder would it suffice to release and renew?

NetworkInterface.GetAllNetworkInterfaces()[0].ReleaseDhcpLease();
      NetworkInterface.GetAllNetworkInterfaces()[0].RenewDhcpLease();

One day, all this too shall parse.

#6 ColinR

ColinR

    Advanced Member

  • Members
  • PipPipPip
  • 142 posts
  • LocationCape Town, South Africa

Posted 07 February 2012 - 05:26 AM

Is there a class out there to easily log exceptions to the sd card?


Just wrap your code in try catch blocks and call WriteLog(exception.ToString());

private static void WriteLog(string log)
{
    try
    {
        var fs = new FileStream(@"\SD\log.txt", FileMode.Append, FileAccess.Write, FileShare.Read, 512);
        var sw = new StreamWriter(fs);
        sw.WriteLine(DateTime.Now.ToString(DateFormat) + ": " + log.Trim());
        sw.Flush();
        sw.Close();
    }
    catch { }
}


I wonder would it suffice to release and renew?


Unfortunately not according to my tests.

#7 Arbiter

Arbiter

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationBrisbane, Australia

Posted 07 February 2012 - 12:34 PM

I built an app that responds to the NetworkAddressChanged event by creating a TCP socket, connecting it to the echo service on port 7, writing "FUBAR" and reading it back into a buffer that it displays with Debug.Print(). This app totally failed to manifest the problem when I power cycle the wireless 3G router to which my N+ is connected.

On the app I have under development, this causes 10055 on the second pass.

Believing it to be a memory shortage, I constructed this
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Text;

namespace NetworkExercise
{
  public class Program
  {
    static bool isNetworkAvailable = false;
    static byte[] rampig = new byte[15360];

    public static void Main()
    {
      NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
      NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
      Thread.Sleep(Timeout.Infinite);
    }

    static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
    {
      isNetworkAvailable = e.IsAvailable;
      Debug.Print(DateTime.UtcNow.ToString("u") + " " + (isNetworkAvailable ? "ONLINE" : "OFFLINE"));
    }

    static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
    {
      if (isNetworkAvailable)
      {
        var na = NetworkInterface.GetAllNetworkInterfaces()[0];
        Debug.Print(na.IPAddress.ToString());
        Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        var ep = new IPEndPoint(Dns.GetHostEntry("whitehouse.podzone.net").AddressList[0], 7);
        socket.Connect(ep);
        socket.Send(Encoding.UTF8.GetBytes("FUBAR"));
        byte[] buf = new byte[5];
        Thread.Sleep(100);
        socket.Receive(buf);
        Debug.Print(new string(Encoding.UTF8.GetChars(buf)));
      }
    }
  }
}

Rather surprisingly. this runs without a hitch. Increase rampig by 1K and it doesn't even get off the starting block.
One day, all this too shall parse.

#8 mohammad

mohammad

    Advanced Member

  • Members
  • PipPipPip
  • 79 posts

Posted 22 May 2013 - 03:16 AM

This link might be useful to reboot the Netduino Plus by software http://forums.netdui...lus/#entry49735

 

Good Luck,

Mohammad






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.