Main Site Documentation

Problem Networking application with EMX


#1

I am working with GHI’s EMX tool.
I want to communicate with time server to get the time. Can some one suggest the example. I have one example, it is working with emulator but not working with EMX tool.


#2

A search on the forum would have yielded this: http://www.tinyclr.com/forum/10/1294/

I have this running perfectly on my Cobra.
Have fun.


#3

Thanks for the quick reply.

I have checked the code with EMX. It is showing me error while assigning IPEndpoint at

IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);

the error message is

“An unhandled exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll”

my internet cable is connected in the ethernet port. Do i need to check another settings? How would i check that the EMX is getting internet connection or not?


#4

Post the code you have so far (don’t forget to use the code tags)


#5

Please find the code as under. The code is working fine with emulator but giving problem with EMX.


using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Net;
using GHIElectronics.NETMF.Net;
using System.Net;
using System.Net.Sockets;


namespace TimeServerConnection
{
    public class Program
    {
        public static void Main()
        {            

            Microsoft.SPOT.Hardware.Utility.SetLocalTime(NTPTime("time-a.nist.gov"));
        }


        public static DateTime NTPTime(String TimeServer)
        {
            // Find endpoint for timeserver
            IPEndPoint ep = new IPEndPoint(Dns.GetHostEntry(TimeServer).AddressList[0], 123);

            // Connect to timeserver
            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            s.Connect(ep);

            // 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 = TimeZone.CurrentTimeZone.GetUtcOffset(dateTime);
            DateTime networkDateTime = (dateTime + offsetAmount);

            return networkDateTime;
        }

    }
}




#6

Has the Cobra been properly configured for a static IP address?

Make sure DNS is properly configured properly.


#7

Thanks for the reply.

I want to know that how to configure the EMX with static IP address and DNS? Is there any code for configuration? any steps to follow?

How to check that it is configured properly?


#8

Fire up the MFDeploy tool, select USB/EMX, then go to Target/Configuration/Network and fill in the details.


#9

Thanks for the support. Its done. The DNS configuration was wrong and i don’t know how to configure them. Thanks all for support.


#10

So it’s working now?

You’re welcome.