Hello There,
I am using a Fez Spider and wifi rs21 module. I connect to my wifi module and am able to pull the time with this code.
DateTime NTPTime(string TimeServer)
{
// Find endpoint for timeserver
IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 50);
// Connect to timeserver
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
s.ReceiveTimeout = 1000;
s.SendTimeout = 1000;
if (s.Poll(1000, SelectMode.SelectWrite))
{
s.Connect(ep);
}
else
{
if (LogRequested != null)
{
LogRequested(null, new LogRequestArgs("Cannot connect to socket"));
}
return new DateTime();
}
// Make send/receive buffer
byte[] ntpData = new byte[48];
Array.Clear(ntpData, 0, 48);
// Set protocol version
ntpData[0] = 0x1B;
// Send Request
s.Send(ntpData);
// Receive Time
s.Receive(ntpData);
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;
TimeSpan offsetAmount = new TimeSpan(0, TimezoneOffset, 0, 0, 0);
DateTime networkDateTime = (dateTime + offsetAmount);
if (IsDST(networkDateTime))
{
networkDateTime.AddHours(1);
}
return networkDateTime;
}
The problem I am running into is when I disconnect from the wifi and reconnect this line
s.Send(ntpData);
hangs my application. I tried putting this to test it:
if (s.Poll(1000, SelectMode.SelectWrite))
{
s.Connect(ep);
}
else
{
if (LogRequested != null)
{
LogRequested(null, new LogRequestArgs("Cannot connect to socket"));
}
return new DateTime();
}
but it always failed. This behavior also happens with my webserver I start up on networkUp event and network down event although the debug output states that a websever was started on x ipaddress but I am never able to access it.
Network Up:
WebServer.StartLocalServer(_wifi.NetworkSettings.IPAddress, 80);
index = WebServer.SetupWebEvent("index", WebServer_RefreshRate);
applog = WebServer.SetupWebEvent("applog");
records = WebServer.SetupWebEvent("records");
applog.WebEventReceived += new WebEvent.ReceivedWebEventHandler(applog_WebEventReceived);
index.WebEventReceived += new WebEvent.ReceivedWebEventHandler(index_WebEventReceived);
records.WebEventReceived += new WebEvent.ReceivedWebEventHandler(records_WebEventReceived);
Network Down:
applog.WebEventReceived -= new WebEvent.ReceivedWebEventHandler(applog_WebEventReceived);
index.WebEventReceived -= new WebEvent.ReceivedWebEventHandler(index_WebEventReceived);
records.WebEventReceived -= new WebEvent.ReceivedWebEventHandler(records_WebEventReceived);
applog = null;
index = null;
records = null;
WebServer.StopLocalServer();
I have been kind of frustrated because I have tried every combination I can think off…
Thanks!