Dylan
June 25, 2012, 4:36pm
1
hello !
i have some problem with NTP …
i use this code
public static bool NTPTime(string TimeServer, int GmtOffset)
{
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
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();
DateTime dateTime = new DateTime(1900, 1, 1) + TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
Utility.SetLocalTime(dateTime.AddMinutes(GmtOffset));
RealTimeClock.SetTime(DateTime.Now);
return true;
}
s.Close();
}
catch
{
try { s.Close(); }
catch { }
}
return false;
}
and i call it like this
NTPTime("193.49.146.254", +2);
i try with the ip of my computer but it still dosen’t work …
the fez panda is directly connect to the internet.
i had this to try but it dosen’t fix it
WIZnet_W5100.Enable(SPI.SPI_module.SPI1, (Cpu.Pin)FEZ_Pin.Digital.Di10, (Cpu.Pin)FEZ_Pin.Digital.Di7, true);
NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
thank you !! ( and sorry for the mistake i’m french )
What exception message are you getting from the code? Are you able to compile the code?
Are you able to connect to other servers on the internet with your device?
I’m using very similar code with no problems.
For the server I used: time.nist.gov (NIST Internet Time Service )
Dylan
June 25, 2012, 5:06pm
3
i have no execption, and all my code compile …
it dosen’t work with your adress.
Maybe you can show me the code you use ?
thank for help !
Dylan:
i have no execption, and all my code compile …
it dosen’t work with your adress.
Maybe you can show me the code you use ?
thank for help !
Are you sure you’re connected to the internet ok?
But here’s part of the code I put into Codeshare. My module calls DoTimeUpdate() - That first tries the primary site, and then if it fails, it tries a differently one directly.
My code will probably not compile, due my Status property - which comes from the full code on Codeshare.
http://www.tinyclr.com/codeshare/entry/453
DateTime NTPTime(string TimeServer)
{
// borrowed (and slightly modified) from Nicolas3
// http://www.tinyclr.com/codeshare/entry/244
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
if (s.Poll(10 * 1000 * 1000, SelectMode.SelectRead)) // Waiting an answer for 10s, 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();
DateTime dateTime = new DateTime(1900, 1, 1) + TimeSpan.FromTicks((long)milliseconds * TimeSpan.TicksPerMillisecond);
return dateTime;
}
s.Close();
}
catch (Exception e)
{
try { s.Close(); }
catch { }
}
return DateTime.MinValue;
}
/// <summary>
/// Go and get the time from NIST
/// </summary>
public void DoTimeUpdate()
{
if (!timeUpdated)
{
Status = WiFiStatus.GettingTime;
DateTime dt;
try
{
// try the official site
dt = NTPTime("time.nist.gov");
timeUpdated = true;
}
catch (Exception ex)
{
string s = ex.Message;
dt = DateTime.MinValue;
}
try
{
// if the official site didn't work, then try a specific site
if (dt == DateTime.MinValue)
{
timeUpdated = true;
dt = NTPTime("nist1-ny.ustiming.org");
}
}
catch (Exception ex)
{
string s = ex.Message;
dt = DateTime.MinValue;
}
if (timeUpdated && dt != DateTime.MinValue)
{
// update the system clock with adjusted value
Utility.SetLocalTime(dt.AddHours(TimezoneOffset));
if (timeUpdated && TimeUpdated != null)
{
TimeUpdated(null, new TimeUpdatedArgs(dt));
}
}
Status = WiFiStatus.Connected;
}
}
}
Dylan
June 25, 2012, 5:31pm
5
thank a lot for your code !!!
i will try this now.
How can i check that my FEZ PANDA is connect to internet ?
thank again to help me ! ( in a way you same my life because it’s for school ^^)
Dylan:
thank a lot for your code !!!
i will try this now.
How can i check that my FEZ PANDA is connect to internet ?
thank again to help me ! ( in a way you same my life because it’s for school ^^)
I would try to request a web page - like “www.google.com ” – using the HttpWebRequest object.
BTW - I added a note about my code maybe not compiling for - the Status property in the DoTimeUpdate() method.
Dylan
June 25, 2012, 5:49pm
7
timeUpdated Status TimezoneOffset and WifiStatue dosen’t exit in my code, what i have miss ?
And i don’t know how use HttpWebRequest ( shame on me )
thanks !!
[edit] i try to ping the FEZ from my computer and it’s work ( the FEZ is connect by ethernet directly on my router and my pc use WIFI )
[edit 2] sorry i miss your codeshare, now i understand why they dosen’t exist ^^. but i think your program will impossible to add in my :s
Dylan:
timeUpdated Status TimezoneOffset and WifiStatue dosen’t exit in my code, what i have miss ?
And i don’t know how use HttpWebRequest ( shame on me )
thank
Those are properties in my class. The only one you need to recreate is the TimeZoneOffset (hour difference from UTC). Just create a local variable with that name and make it an int .
The other lines can be removed.
Something like this:
taken from (from http://www.tinyclr.com/codeshare/entry/126 )
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("www.google.com");
// Get a response from the server.
WebResponse resp = null;
try
{
resp = request.GetResponse();
}
catch (Exception e)
{
Debug.Print("Exception in HttpWebRequest.GetResponse(): " +
e.ToString());
throw e;
}
Dylan
June 25, 2012, 6:19pm
9
mhectorgato:
Dylan:
timeUpdated Status TimezoneOffset and WifiStatue dosen’t exit in my code, what i have miss ?
And i don’t know how use HttpWebRequest ( shame on me )
thank
Those are properties in my class. The only one you need to recreate is the TimeZoneOffset (hour difference from UTC). Just create a local variable with that name and make it an int .
The other lines can be removed.
Something like this:
taken from (from http://www.tinyclr.com/codeshare/entry/126 )
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("www.google.com");
// Get a response from the server.
WebResponse resp = null;
try
{
resp = request.GetResponse();
}
catch (Exception e)
{
Debug.Print("Exception in HttpWebRequest.GetResponse(): " +
e.ToString());
throw e;
}
i try your code but it don’t work. i have an execption on this line
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("www.google.com");
the exeption is =>
System.ArgumentException
in
GHIElectronics.NETMF.W5100.Http.dll
I’m not in front of my computer so it’s hard to help - perhaps its the missing http:// in the address??
Dylan
June 25, 2012, 6:37pm
11
ho it was just a problem with the URL of google ^^ ( just add http://)
but it’s dosen’t work i get this exception ( because it go to the catch) = >
Exception in HttpWebRequest.GetResponse(): GHIElectronics.NETMF.Net.WebException
Dylan:
ho it was just a problem with the URL of google ^^ ( just add http://)
but it’s dosen’t work i get this exception ( because it go to the catch) = >
Exception in HttpWebRequest.GetResponse(): GHIElectronics.NETMF.Net.WebException
Maybe put a breakpoint in the catch and look at the exception message itself??
Dylan
June 25, 2012, 6:51pm
14
#### Exception GHIElectronics.NETMF.Net.WebException - 0x00000000 (1) ####
#### Message:
#### GHIElectronics.NETMF.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
#### MFConsoleApplication1.Program::Main [IP: 003d] ####
i got this.
[edit] and before i get this =>
#### Exception System.Exception - 0x00000000 (1) ####
#### Message: DNS server IP address was not found.
#### GHIElectronics.NETMF.Net.Dns::GetHostEntry [IP: 0038] ####
#### GHIElectronics.NETMF.Net.HttpWebRequest::EstablishConnection [IP: 00e1] ####
#### GHIElectronics.NETMF.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### GHIElectronics.NETMF.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### MFConsoleApplication1.Program::Main [IP: 003d] ####
what the problem with DNS ??
Brett
June 25, 2012, 7:15pm
15
Your problem with DNS is that you probably don’t have it set correctly - one of two situations come to mind, first that you have a typo in the address and DNS fails to talk to the DNS host, or secondly that you’ve just not set up DNS. Please show us the code where you set your fixed IP address up?
Brett
June 25, 2012, 7:21pm
16
If you enable Static IP you also need this:
NetworkInterface.EnableStaticDns(dnsAddress);
Dylan
June 25, 2012, 7:44pm
17
thanks for help !!
.
now i have this problem …
#### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
#### Message:
#### GHIElectronics.NETMF.Net.HttpWebRequest::EstablishConnection [IP: 00f7] ####
#### GHIElectronics.NETMF.Net.HttpWebRequest::SubmitRequest [IP: 0013] ####
#### GHIElectronics.NETMF.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### MFConsoleApplication1.Program::Main [IP: 0061] ####
#### Exception GHIElectronics.NETMF.Net.WebException - 0x00000000 (1) ####
#### Message:
#### GHIElectronics.NETMF.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
#### MFConsoleApplication1.Program::Main [IP: 0061] ####
Don’t forget to set the DNS as well - 8.8.8.8 is a good ip to use for DNS. You’ll need to use a gateway as well.
Dylan are you using DHCP? Or Static IP?
Brett
June 25, 2012, 7:53pm
19
Personally, I suggest you just go back to doing NTP and ignoring HTTP at this stage. All you want is your network time code to sync the time, right? Get that working and then worry about other stuff. HTTP is more complex and isn’t adding anything to your current goal. I think we’ve established that your code didn’t do DNS right, so now we should figure out what is next.
Brett:
Personally, I suggest you just go back to doing NTP and ignoring HTTP at this stage. All you want is your network time code to sync the time, right? Get that working and then worry about other stuff. HTTP is more complex and isn’t adding anything to your current goal. I think we’ve established that your code didn’t do DNS right, so now we should figure out what is next.
I was just trying to establish that he had a connection to the internet.