Monday 11 February 2013

Gadgeteer Ethernet J11D module not working in NETMF 4.2 projects

I had some problems getting an Ethernet module working with my Gadgeteer mainboard in a NetMF 4.2 project last night, so i thought i'd share how i got it working.

I'm using a Fez Spider mainboard and Ethernet J11D module and it seems that the Ethernet module works a bit differently under NetMF 4.2 that what i'm used to.





















Adding the Ethernet module to the Gadgeteer visual designer in the same way as other modules will cause a null reference exception.

We no longer need to add the Ethernet module to the designer. Instead we use the EthernetBuiltIn class and the NetworkInterfaceExtension.AssignNetworkingStackTo method.

You can find some sample code to get the Ethernet module working below.

You'll need to add a reference to GHI.Premium.Net for the EthernetBuiltIn class.

 static readonly EthernetBuiltIn Ethernet = new EthernetBuiltIn();  
   
     void ProgramStarted()  
     {  
       Debug.Print("Program Started");  
   
       Ethernet.Open();  
   
       NetworkInterfaceExtension.AssignNetworkingStackTo(Ethernet);  
   
       Ethernet.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Ethernet_NetworkAddressChanged);  
   
       Ethernet.NetworkInterface.EnableDhcp();  
     }  
   
     void Ethernet_NetworkAddressChanged(object sender, EventArgs e)  
     {  
       Debug.Print("Network Address Changed");  
       Debug.Print(Ethernet.NetworkInterface.IPAddress);  
     }  

This code should be enough to get you started with the Ethernet module under NETMF 4.2

Friday 1 February 2013

Gadgeteer - Setting system time from the internet


I came across a scenario recently where i needed to get set the system time from an NTP server on my gadgeteer mainboard.

















It's easy enough to set the system time using the Utility.SetLocalTime method, but we still need some way of getting the correct time from the network.

Turns out that we have some built in functionality to do this.


TimeService


The TimeService class provides some helper methods to allow syncing the local system time from an NTP server. We can also specify a time period, after which the TimeService will update from the NTP server again.

The code below gets the current time from time.windows.com syncs the system time and prints it out the the console.

First make sure you have added a reference to Microsoft.Spot.Time


    void SetTime()   
    {   
     Debug.Print("setting time");
   
     TimeService.SystemTimeChanged += new SystemTimeChangedEventHandler(TimeService_SystemTimeChanged);   
     TimeService.TimeSyncFailed += new TimeSyncFailedEventHandler(TimeService_TimeSyncFailed);  
 
     var settings = new TimeServiceSettings();   
     settings.ForceSyncAtWakeUp = true;
   
     // refresh time is in seconds   
     settings.RefreshTime = 1800;
    
     settings.PrimaryServer = GetTimeServerAddress();
   
     TimeService.Settings = settings;   
     TimeService.SetTimeZoneOffset(0);   
     TimeService.Start();   
    }
   
    byte[] GetTimeServerAddress()   
    {   
     IPAddress[] address = Dns.GetHostEntry("time.windows.com").AddressList;   
     if (address != null && address.Length > 0)   
     {   
      return address[0].GetAddressBytes();   
     }   
     throw new ApplicationException("Could not get time server address");   
    }
   
    void TimeService_TimeSyncFailed(object sender, TimeSyncFailedEventArgs e)   
    {   
     Debug.Print("error synchronizing time with NTP server: " + e.ErrorCode);   
    } 
  
    void TimeService_SystemTimeChanged(object sender, SystemTimeChangedEventArgs e)   
    {   
     Debug.Print("network time received. Current Date Time is " + DateTime.Now.ToString());   
    }   
 

Hopefully this will be useful for someone else out there.