Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Query NTP server. Byte operations not supported?


  • Please log in to reply
6 replies to this topic

#1 Moskus

Moskus

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationNorway

Posted 19 September 2011 - 12:25 PM

I tried to convert the NTP server query function found here to VB.NET in the .NETMF, but the byte operations fail.

Public Function GetNetworkTime() As DateTime
        Dim ep As New Net.IPEndPoint(Dns.GetHostEntry("time-a.nist.gov").AddressList(0), 123)

        Dim s As New Sockets.Socket(Sockets.AddressFamily.InterNetwork, Sockets.SocketType.Dgram, Sockets.ProtocolType.Udp)
        s.Connect(ep)

        Dim ntpData As Byte() = New Byte(47) {} ' RFC 2030'
        ntpData(0) = &H1B
        For i As Integer = 1 To 47
            ntpData(i) = 0
        Next

        s.Send(ntpData)
        s.Receive(ntpData)

        Dim offsetTransmitTime As Byte = 40
        Dim intpart As ULong = 0
        Dim fractpart As ULong = 0
        For i As Integer = 0 To 3
            intpart = CULng(256 * intpart + ntpData(offsetTransmitTime + i))
        Next

        For i As Integer = 4 To 7
            fractpart = CULng(256 * fractpart + ntpData(offsetTransmitTime + i))
        Next

        Dim milliseconds As ULong = CULng((intpart * 1000 + (fractpart * 1000) / &H100000000L))

        s.Close()

        Dim timeSpan__1 As TimeSpan = TimeSpan.FromTicks(CLng(milliseconds) * TimeSpan.TicksPerMillisecond)
        Dim dateTime As New DateTime(1900, 1, 1)
        dateTime += timeSpan__1

        Dim offsetAmount As TimeSpan = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime)
        Dim networkDateTime As DateTime = (dateTime + offsetAmount)

        Debug.Print(networkDateTime.ToString())

        Return networkDateTime
    End Function


These lines are throing errors:
intpart = 256 * intpart + ntpData(offsetTransmitTime + i)
fractpart = CULng(256 * fractpart + ntpData(offsetTransmitTime + i))
Dim milliseconds As ULong = CULng((intpart * 1000 + (fractpart * 1000) / &H100000000L))

I get these errors:
Requested operation is not available because the runtime library function 'System.Convert.ToUInt64' is not defined.
Requested operation is not available because the runtime library function 'System.Decimal.Add' is not defined.
Requested operation is not available because the runtime library function 'System.Decimal.Divide' is not defined.
Requested operation is not available because the runtime library function 'System.Decimal.Multiply' is not defined.



The exact same code works without problem in a regular .NET Framework (console) application.

#2 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 19 September 2011 - 06:59 PM

Look at the Daytime server. You don't have to screw with this arithmetic. The NIST Daytime servers return an accii message on tcp port 13. See http://forums.netdui...rotocol-client/ I changed to this protocol because of the the difficulties you are citing. wwv.nist.gov is very reliable. Baxter

#3 Moskus

Moskus

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationNorway

Posted 19 September 2011 - 07:11 PM

Great! I'm trying that. Thanks! :)

#4 Moskus

Moskus

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationNorway

Posted 20 September 2011 - 07:04 AM

... but I still would like simple bitoperations to work... ;)

#5 AdamS

AdamS

    New Member

  • Members
  • Pip
  • 8 posts

Posted 11 January 2012 - 04:22 PM

Moskus, Did you ever Get this working?

#6 Moskus

Moskus

    Advanced Member

  • Members
  • PipPipPip
  • 132 posts
  • LocationNorway

Posted 13 January 2012 - 08:50 PM

No, but I haven't had the chance to play with my Netduinos lately. I haven't installed the latest firmware either...

#7 AdamS

AdamS

    New Member

  • Members
  • Pip
  • 8 posts

Posted 14 January 2012 - 01:31 PM

No, but I haven't had the chance to play with my Netduinos lately. I haven't installed the latest firmware either...



thats ok i found a bit of a workaround for the time being by putting the ntp class into a separate C# class library attached to the solution :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.