Cerberus test version (ie ENC28 module)
using System;
using System.Net;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Time;
namespace CerberusTimeService
{
public partial class Program
{
//NOTE with the Cerberus it does seem to matter which socket you use and my testing recommends socket 6 for connecting the ENC28 module
private const int localTimeZone = -7*60;
private int daylightSavingsShift;
private NetworkInterface _ni;
private void ProgramStarted()
{
Debug.Print("Program Started");
char_Display.PrintString("Running");
try
{
_ni = NetworkInterface.GetAllNetworkInterfaces()[0];
if (!_ni.IsDhcpEnabled)
_ni.EnableDhcp();
if (_ni.IPAddress != "0.0.0.0")
{
char_Display.SetCursor(0, 0);
char_Display.PrintString(_ni.IPAddress);
Debug.Print("IP Address:" + _ni.IPAddress);
UpdateTime();
DateTime now = DateTime.Now;
char_Display.Clear();
char_Display.PrintString(now.ToString("yyyy/MM/dd"));
char_Display.SetCursor(1, 0);
char_Display.PrintString(now.ToString("HH:mm:ss"));
}
else
{
char_Display.SetCursor(0, 0);
char_Display.PrintString(_ni.IPAddress);
}
}
catch (Exception exception)
{
Debug.Print("Main " + exception.Message);
}
}
private void UpdateTime()
{
try
{
var NTPTime = new TimeServiceSettings();
NTPTime.AutoDayLightSavings = true;
NTPTime.ForceSyncAtWakeUp = true;
NTPTime.RefreshTime = 3600;
//Thread.Sleep(1500);
NTPTime.PrimaryServer = Dns.GetHostEntry("2.ca.pool.ntp.org").AddressList[0].GetAddressBytes();
NTPTime.AlternateServer = Dns.GetHostEntry("time.nist.gov").AddressList[0].GetAddressBytes();
//Thread.Sleep(1500);
TimeService.Settings = NTPTime;
TimeService.SetTimeZoneOffset(localTimeZone); // MST Time zone : GMT-7
TimeService.SystemTimeChanged += onSystemTimeChanged;
TimeService.TimeSyncFailed += onTimeSyncFailed;
TimeService.Start();
//Thread.Sleep(500);
TimeService.UpdateNow(0);
//Thread.Sleep(9000);
Debug.Print("It is : " + DateTime.Now.ToString());
DateTime time = DateTime.Now;
Utility.SetLocalTime(time);
TimeService.Stop();
}
catch (Exception ex)
{
Debug.Print("Update Time " + ex.Message);
}
}
private void onSystemTimeChanged(Object sender, SystemTimeChangedEventArgs e)
// Called on successful NTP Synchronisation
{
DateTime now = DateTime.Now; // Used to manipulate dates and time
#region Check is we are in summer time and thus daylight savings apply
// Check if we are in Daylight savings. The following algorythm works pour Europe and associated countries
// In Europe, daylight savings (+60 min) starts the last sunday of march and ends the last sunday of october
var aprilFirst = new DateTime(now.Year, 4, 1);
var novemberFirst = new DateTime(now.Year, 11, 1);
var sundayshift = new[] {0, -1, -2, -3, -4, -5, -6};
int marchLastSunday = aprilFirst.DayOfYear + sundayshift[(int) aprilFirst.DayOfWeek];
int octoberLastSunday = novemberFirst.DayOfYear + sundayshift[(int) novemberFirst.DayOfWeek];
if ((now.DayOfYear >= marchLastSunday) && (now.DayOfYear < octoberLastSunday))
daylightSavingsShift = 60;
else
daylightSavingsShift = 0;
TimeService.SetTimeZoneOffset(localTimeZone + daylightSavingsShift);
#endregion
// Display the synchronized date on the Debug Console
now = DateTime.Now;
string date = now.ToString("dd/MM/yyyy");
string heure = now.ToString("HH:mm:ss");
Debug.Print(date + " // " + heure);
}
private void onTimeSyncFailed(Object sender, TimeSyncFailedEventArgs e)
// Called on unsuccessful NTP Synchronisation
{
Debug.Print("NTPService : Error synchronizing system time with NTP server");
}
}
}
Debug run
Found debugger!
Create TS.
Loading start at 806fedc, end 8098af8
Assembly: mscorlib (4.2.0.0) Assembly: Microsoft.SPOT.Native (4.2.0.0) Assembly: Microsoft.SPOT.Hardware (4.2.0.0)
Assembly: Microsoft.SPOT.Graphics (4.2.0.0) Assembly: Microsoft.SPOT.TinyCore (4.2.0.0)
Assembly: Microsoft.SPOT.Hardware.SerialPort (4.2.0.0) Assembly: Microsoft.SPOT.IO (4.2.0.0)
Assembly: System.IO (4.2.0.0) Assembly: Microsoft.SPOT.Hardware.OneWire (4.2.0.0)
Assembly: Microsoft.SPOT.Hardware.Usb (4.2.0.0) Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1)
Assembly: Microsoft.SPOT.Net (4.2.0.0) Assembly: System (4.2.0.0) Loading Deployment Assemblies.
Attaching deployed file.
Assembly: GTM.GHIElectronics.Display_HD44780 (1.1.2.0) Attaching deployed file.
Assembly: GHI.OSHW.Hardware (4.2.3.1) Attaching deployed file.
Assembly: Gadgeteer (2.42.0.0) Attaching deployed file.
Assembly: GHIElectronics.Gadgeteer.FEZCerberus (1.1.2.0) Attaching deployed file.
Assembly: Microsoft.SPOT.Time (4.2.0.0) Attaching deployed file.
Assembly: CerberusTimeService (1.0.0.0) Resolving.
The debugging target runtime is loading the application assemblies and starting execution.
Ready.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\mscorlib.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Native.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Graphics.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.TinyCore.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.SerialPort.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.IO.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.OneWire.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.Usb.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Hardware.PWM.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Net.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\System.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Gadgeteer\Core\Assemblies\.NET Micro Framework 4.2\le\Gadgeteer.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\GHI Electronics\GHI .NET Gadgeteer SDK\Modules\Display_HD44780\NETMF 4.2\le\GTM.GHIElectronics.Display_HD44780.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\GHI Electronics\GHI OSHW NETMF v4.2 SDK\Assemblies\le\GHI.OSHW.Hardware.dll'
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\GHI Electronics\GHI .NET Gadgeteer SDK\Mainboards\FEZCerberus\NETMF 4.2\le\GHIElectronics.Gadgeteer.FEZCerberus.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Program Files (x86)\Microsoft .NET Micro Framework\v4.2\Assemblies\le\Microsoft.SPOT.Time.dll', Symbols loaded.
'Microsoft.SPOT.Debugger.CorDebug.dll' (Managed): Loaded 'C:\Users\Blake\Documents\Visual Studio 2010\Projects\CerberusTimeService\CerberusTimeService\bin\Debug\le\CerberusTimeService.exe', Symbols loaded.
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZCerberus version 1.1
Program Started
IP Address:10.0.0.13
A first chance exception of type 'System.NotSupportedException' occurred in CerberusTimeService.exe
Update Time Exception was thrown: System.NotSupportedException
The thread '<No Name>' (0x3) has exited with code 0 (0x0).
NOTE that ‘Update Time’ is my text indicating that the expection occured in the UpdateTime routine.