Attention: open in a new window. PDFPrint

First steps into Networking on .NET micro

I will not yet show, how you could send data over your network connection on your .NET micro device in this article, but I will describe, what steps to perform to setup networking on the device and how to read out information about your network devices. At first you might want to know, that I own a Tahoe II Rev 1.0 board from devicesolutions.net, so this article is based on this device, but should work with any other device as well.

Now, lets start.

 

At first you have to establish the connection between your device and your computer to setup the network connection using the MFDeploy utility shipped with the .NET micro framework SDK provided by Microsoft. Most likely, you will find the utility in your program menu (Start -> Programs -> Microsoft .NET Micro Framework 3.0 -> Tools in my case). Start up the utility and select the right connection properties for your device. If you own a Meridian based device (as I do), you might select USB as connection and your device should appear in the second drop-down list.



Afterwards click on Target -> Configuration -> Network to enter your networking information. Enter some "valid" values and make sure that you use a mac address that is valid and unique in your environment (the best option is to use the address that should have been shipped with your device ;) ) and click update to save your settings. I do not use DHCP at this point, as it takes some time on system bootup which causes Visual Studio to stop Debugging before the application starts up. So if I enable DHCP on the device permanently, this will cause the impossibility of debugging.

As we are prepared to use our network device now, we should be able to read out the information that we have written to it.

Create a new Window Application for your .NET mf device using the project template and add the Microsoft.SPOT.Net reference.

Now, add some fields and methods to your application class:

        private Panel mainPane;
        char[] nums = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
        private String uintToStr(int value)
        {
            int l = 0;
            int v = value;
            while (v > 0)
            {
                l++;
                v /= 10;
            }
            l = l > 0 ? l : 1;
            v = value;
            char[] crc = new char[l];
            for (int i = (l - 1); i > -1; i--)
            {
                crc[i] = nums[v % 10];
                v /= 10;
            }
            return new String(crc);
        }
        private int lh = 10;
        private int t = 5;
        private int l = 5;
        private void addLine(String text)
        {
            Text txt = new Text();
            txt.TextContent = text;
            txt.Font = Resources.GetFont(Resources.FontResources.small);
            txt.SetMargin(l, t, 0, 0);
            mainPane.Children.Add(txt);
            t += lh;
        }
        private void ListInterfaces()
        {
            NetworkInterface[] ifaces = NetworkInterface.GetAllNetworkInterfaces();
            this.addLine("Number of Interfaces: " + uintToStr(ifaces.Length));
            foreach (NetworkInterface iface in ifaces)
            {
                this.addLine("IP:  " + iface.IPAddress + "/" + iface.SubnetMask);
                this.addLine("DHCP:" + iface.IsDhcpEnabled);
            }
        }


We use the mainPane (Panel) to write some text on it. the uintToStr converts uint values to Strings. Do not use negative values with this method, as this will fail. I have used this method here as ifaces.Length.ToString() just returns an empty string. The method addLine adds a line of text to our mainPane and the method ListInterfaces does all the magic. It reads a list of interfaces and writes the IP-address and the subnet-mask of the device to the panel.

Do not forget to add the following using-directive:

using Microsoft.SPOT.Net.NetworkInformation;

and change the CreateWindow method that it looks as follows:

        public Window CreateWindow()
        {
            // Create a window object and set its size to the
            // size of the display.
            mainWindow = new Window();
            mainWindow.Height = SystemMetrics.ScreenHeight;
            mainWindow.Width = SystemMetrics.ScreenWidth;
            mainPane = new Panel();
            mainPane.HorizontalAlignment = HorizontalAlignment.Stretch;
            mainPane.VerticalAlignment = VerticalAlignment.Stretch;
            mainWindow.Child = mainPane;
            ListInterfaces();
            mainWindow.Child = mainPane;
            // Connect the button handler to all of the buttons.
            mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);
            // Set the window visibility to visible.
            mainWindow.Visibility = Visibility.Visible;
            // Attach the button focus to the window.
            Buttons.Focus(mainWindow);
            return mainWindow;
        }

Deploy everthing to your device and you should see a list of your network devices on the target device.