Hi guys,
Just got a Cobra and I am trying to covert some of the Panda II code over for use in a new app. The time update method is my first problem. I need to update the time on the Cobra. Here’s the error:
DCHP - IP Address = 192.168.0.101 … Net Mask = 255.255.255.0 … Gateway = 192.168.0.1
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (1) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::getaddrinfo [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 0008] ####
#### FEZ_Cobra_Console_NTP_Time.Program+myTime::NTPTime [IP: 0008] ####
#### FEZ_Cobra_Console_NTP_Time.Program::Main [IP: 0060] ####
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = -1
#### SocketException ErrorCode = -1
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
#### Message:
#### FEZ_Cobra_Console_NTP_Time.Program+myTime::NTPTime [IP: 0119] ####
#### FEZ_Cobra_Console_NTP_Time.Program::Main [IP: 0060] ####
A first chance exception of type ‘System.NullReferenceException’ occurred in FEZ Cobra Console Application.exe
Updated Time: 01/01/2009 00:01:54
Would someone point out the error in the program.
Thanks,
K
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Time;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net.NetworkInformation;
using Socket = System.Net.Sockets.Socket;
namespace FEZ_Cobra_Console_NTP_Time
{
public class Program
{
private static bool timeUpdate = false;
public static void Main()
{
Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
if (NI.IsDhcpEnabled == false)
{
Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
}
while (true)
{
if (timeUpdate == false)
{
myTime Update = new myTime();
Update.NTPTime("time-a.nist.gov", -300);
timeUpdate = true;
Debug.Print("Updated Time: " + DateTime.Now.ToString()); //displays updated time in VS2010 output window
}
}
}
public class myTime
{
public bool NTPTime(string TimeServer, int GmtOffset = 0)
{
Socket s = null;
try
{
EndPoint rep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);
s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
byte[] ntpData = new byte[48];
Array.Clear(ntpData, 0, 48);
ntpData[0] = 0x1B; // Set protocol version
//s.SendTo(ntpData, rep); // Send Request
s.SendTo(ntpData, rep);
if (s.Poll(30 * 1000 * 1000, SelectMode.SelectRead)) // Waiting an answer for 30s, if nothing: timeout
{
s.ReceiveFrom(ntpData, ref rep); // Receive Time
byte offsetTransmitTime = 40;
ulong intpart = 0;
ulong fractpart = 0;
for (int i = 0; i <= 3; i++) intpart = (intpart << 8) | ntpData[offsetTransmitTime + i];
for (int i = 4; i <= 7; i++) fractpart = (fractpart << 8) | ntpData[offsetTransmitTime + i];
ulong milliseconds = (intpart * 1000 + (fractpart * 1000) / 0x100000000L);
s.Close();
TimeSpan timeSpan = TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
DateTime dateTime = new DateTime(1900, 1, 1);
dateTime += timeSpan;
Utility.SetLocalTime(dateTime.AddMinutes(GmtOffset));
RealTimeClock.SetTime(DateTime.Now);
return true;
}
s.Close();
}
catch
{
try { s.Close(); }
catch { }
}
return false;
}
}
}
}