Main Site Documentation

Connecting a socket via Ethernet on the ChipworkX Development Board


#1

I am having a similar issue to several older posts in connecting to the Ethernet and using a socket. My code is based on the Server Real Web Pages tutorial on the NETMF Projects Site at http://www.microframeworkprojects.com/index.php?title=TCP_/_Web_Server_Tutorial. This site has code for the Cobra so I changed the pin definitions to the ChipworkX but changed nothing else. If I run this code with the DHCP code uncommented and the static code commented out, it obtains or renews the lease as appropriate. So I know that I am physically connected and talking at some level.

My problem is with the Sockets namespace and the socket class. The only library I have been able to find this namespace defined in is GHIElectronics.NETMF.W5100.

I have the following References

GHIElectronics.NETMF.Hardware.ChipworkX
GHIElectronics.NETMF.Net
GHIElectronics.NETMF.System
GHIElectronics.NETMF.W5100
Microsoft.SPOT.Hardware
Microsoft.SPOT.Native
Microsoft.SPOT.Net
mscorlib

When I run it, I get the following message:

An unhandled exception of type ‘System.NotSupportedException’ occurred in GHIElectronics.NETMF.W5100.dll

at the line where I try and create the socket:
Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

and the debug output gives me:

#### Exception System.NotSupportedException - CLR_E_NOT_SUPPORTED (1) ####
#### Message: 
#### GHIElectronics.NETMF.Net.W5100::RegisterWrite [IP: 0015] ####
#### GHIElectronics.NETMF.Net.SocketNative::close [IP: 0007] ####
#### GHIElectronics.NETMF.Net.SocketNative::socket [IP: 008e] ####
#### GHIElectronics.NETMF.Net.Sockets.Socket::.ctor [IP: 002a] ####
#### ChipworkX_TCP_test.Program::Main [IP: 003c] ####

A first chance exception of type ‘System.NotSupportedException’ occurred in GHIElectronics.NETMF.W5100.dll
An unhandled exception of type ‘System.NotSupportedException’ occurred in GHIElectronics.NETMF.W5100.dll

I assume W5100 reference is wrong, but I can’t find another reference that gives me the Sockets class definition.

What dumb thing am I doing wrong?

Code follows.


using System;
using System.Threading;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.Hardware;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.Sockets;

namespace ChipworkX_TCP_test
{
    public class Program
    {

        static InputPort upButton = new InputPort((Cpu.Pin) ChipworkX.Pin.PA23, true, Port.ResistorMode.PullUp);
        static InputPort selectButton = new InputPort((Cpu.Pin)ChipworkX.Pin.PA20, true, Port.ResistorMode.PullUp);
        static InputPort downButton = new InputPort((Cpu.Pin)ChipworkX.Pin.PA18, true, Port.ResistorMode.PullUp);

        public static void Main()
        {
            #region Check we have a valid NIC
            // First, make sure we actually have a network interface to work with!
            if (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces().Length < 1)
            {
                Debug.Print("No Active network interfaces. Bombing out.");
                Thread.CurrentThread.Abort();
            }
            #endregion

            // OK, retrieve the network interface
            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];

            #region DHCP Code
            // If DHCP is not enabled, then enable it and get an IP address, else renew the lease. Most of us have a DHCP server
            // on a network, even at home (in the form of an internet modem or wifi router). If you want to use a static IP
            // then comment out the following code in the "DHCP" region and uncomment the code in the "fixed IP" region.
            //if (NI.IsDhcpEnabled == false)
            //{
            //    Debug.Print("Enabling DHCP.");
            //    NI.EnableDhcp();
            //    Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            //}
            //else
            //{
            //    Debug.Print("Renewing DHCP lease.");
            //    NI.RenewDhcpLease();
            //    Debug.Print("DCHP - IP Address = " + NI.IPAddress + " ... Net Mask = " + NI.SubnetMask + " ... Gateway = " + NI.GatewayAddress);
            //}
            #endregion

            #region Static IP code
            // Uncomment the following line if you want to use a static IP address, and comment out the DHCP code region above
            NI.EnableStaticIP("192.168.1.99", "255.255.255.0", "192.168.1.1");
            #endregion

            #region Create and Bind the listening socket
            // Create the socket
            Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the listening socket to the port
            IPAddress hostIP = IPAddress.Parse(NI.IPAddress);
            IPEndPoint ep = new IPEndPoint(hostIP, 80);
            listenSocket.Bind(ep);
            #endregion

            // Start listening
            listenSocket.Listen(1);

            // Main thread loop
            while (true)
            {
                try
                {
                    Debug.Print("listening...");
                    Socket newSock = listenSocket.Accept();
                    Debug.Print("Accepted a connection from " + newSock.RemoteEndPoint.ToString());
                    byte[] messageBytes = Encoding.UTF8.GetBytes(ButtonPage());
                    newSock.Send(messageBytes);
                    newSock.Close();
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                }
            }

        }

        // Read the states of the Cobra buttons and build a web page showing their states
        static string ButtonPage()
        {
            // Determine the states of the three cobra buttons
            string ubs; if (upButton.Read() == false) ubs = "Pressed"; else ubs = "Released";
            string sbs; if (selectButton.Read() == false) sbs = "Pressed"; else sbs = "Released";
            string dbs; if (downButton.Read() == false) dbs = "Pressed"; else dbs = "Released";

            // Build the web page
            string s = "<html>\n";                                      // First the page type
            s += "<head><title>Fez Cobra Test Page</title></head>\n";   // now the page header
            s += "<body>\n";                                            // start the body        
            s += "<p>Up Button State = <i>" + ubs + "</i></p>";         // Up button, state in italics
            s += "<p>Select Button State = <i>" + sbs + "</i></p>";     // Select button, state in italics
            s += "<p>Down Button State = <i>" + dbs + "</i></p>";       // Down button, state in italics
            s += "</body>";                                             // close the body section
            s += "</html>";                                             // close the page type
            return s;
        }
    }
}


#2

Where are the posts? Maybe we should comment on them so they are not confusing anyone. There is no known issues with sockets at this point.

The documentation says http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation/html/f42a73a6-d8cd-3883-6189-c3409ca38fc2.htm

If you look in the free ebook, you will also learn that there is 2 versions of sockets, a standard one, which is ram hungry but is great for large devices like ChipworkX and there is a small version for USBizi that is based on W5100

There are many Microsoft examples for sockets that ship with the Microsoft NETMF 4.1 SDK. Look in your user folder. This is an example
C:\Users\Gus Issa\Documents\Microsoft .NET Micro Framework 4.1\Samples\SocketClient
Those examples should just run with no changes whatsoever.


#3

It is in System.dll.
Namespace - System.Net.Sockets


#4

I think it is not clear enough the difference between the original TCP/IP with NETMF. and the light weighted implementation with Wiznet W5100, specially for new users.
We promise to think of a more clear way to guide the user to the correct code and library.


#5

Thanks. I got it to work this morning. The guidance from FezMaster that I need system.dll was all I needed. Don’t know how I missed it, but in reading over all the suggested documentation and examples several times each I did not clue in that I needed to reference System. It was clear to me that I was missing a reference, but I could not figure out which one. Like so many things obvious in hindsight.

It is all a little hard to follow right now and the directions in the ebook seem to assume quite a bit of knowledge. I also have a WiFi card for my ChipworkX which I have not tried yet. Again, the documentation is very cryptic for a beginner. So far I have not found a simple example of how I enter my encryption keys and get on a private network.

In any case, love the product. Keep up the good work.

Duncan


#6

For the Wifi, There is an example code under
GHIElectronics.NETMF.NET.Wifi class documentation