Basic Networking for FEZ Domino

http://fieldeffect.info/wp/wp-content/uploads/2014/08/4582055932_d8067e2667.jpg

 

Left to right: FEZ Mini, FEZ Domino, Arduino

FEZ Domino is a small controller board that allows the user to program in physical hardware in C#. For more information on what FEZ is, please see here.

As of this writing (May 13, 2010), the FEZ Ethernet shield is not supported inside of the GHI Framework, but there is a beta Ethernet driver here.

In this tutorial I will show you how I created a simple listening server to allow the user to connect to a FEZ Domino attached to an Ethernet Shield. The Domino is plugged into an Ethernet hub, and the user connects through a telnet client. The user types, and hits “enter”, and the text is repeated back.

http://fieldeffect.info/wp/wp-content/uploads/2014/08/4602936582_8f6f20db9b_o.jpg

For a simple listening socket, I had to tweak the beta driver a little bit by adding a GetStatus function to the uSocket class, and also by making the SR_val enum public, instead of private. The purpose of GetStatus is to determine whether a “uSocket” is connected or not.

public SR_val GetStatus()
{
        byte s = _scoket_number;
        int status = W5100.RegisterRead(s, W5100.SocketRegisters.SR);
        return (SR_val)status;
}

Download modified FEZ_Shields_Ethernet.cs

A “uSocket” is the beta driver’s implementation of a socket. For uSockets, the Receive() function is non-blocking, but the Send() function can cause blocking to occur. That is why I needed to create a GetStatus function: because uSocket::Listen() was not blocking the thread before a connection occurred. If I recall correctly, UNIX sockets use a Listen() function that blocks the thread until a connection occurs.

Here are the basic steps you must take to create an Ethernet listening server on the FEZ Domino with Ethernet shield:

  1. Initialize the Ethernet board with a MAC address, an IP address, a netmask, and a gateway by calling the GHIElectronics.NETMF.FEZ.FEZ_Shields.Ethernet.Initialize() function.
  2. s = new GHIElectronics.NETMF.FEZ.FEZ_Shields.Ethernet.uSocket(FEZ_Shields.Ethernet.uSocket.Protocol.TCP, PORT_NO);
  3. Call s.Listen()
  4. Wait while s.GetStatus != SOCK_ESTABLISHED (socket connection is not yet established)

  5. Do whatever while s.GetStatus == SOCK_ESTABLISHED (socket connection is established)

    1. Call s.Receive, s.Send as desired
  6. When s.GetStatus != SOCK_ESTABLISHED, call s.Disconnect()

  7. Call s.Close()
  8. Repeat 2-7 as desired

Download the code I used for this simple server

To test this out, create a console project in C# and include the following assemblies:

GHIElectronics.NETMF.Hardware
GHIElectronics.NETMF.IO
GHIElectronics.NETMF.Net
GHIElectronics.NETMF.System
Microsoft.SPOT.Hardware
Microsoft.SPOT.Hardware.SerialPort
Microsoft.SPOT.IO
Microsoft.SPOT.Native
Microsoft.SPOT.Net
mscorlib
System
System.IO

Then, add the modified FEZ_Shields_Ethernet.cs and the TestEthernet.cs files, making sure the code from the TestEthernet.cs file is the only file with the Main() function in it.

Edit the following lines in the TestEthernet file to comply with your home network. Most home networks use 192.168.0.x for their IP range, but you should figure out what yours is and change the lines below accordingly.

ip_addr = System.Net.IPAddress.Parse("192.168.99.30");
ip_mask = System.Net.IPAddress.Parse("255.255.255.0");
ip_gateway = System.Net.IPAddress.Parse("192.168.99.4")

You may also wish to change the port which the device listens on (change the 32 below to whatever port you wish):

s = new GHIElectronics.NETMF.FEZ.FEZ_Shields.Ethernet.uSocket(FEZ_Shields.Ethernet.uSocket.Protocol.TCP, 32);

Compile and deploy this project to your FEZ Domino.

Now open a telnet client, such as PuTTY or Microsoft telnet. Connect to port 32 on the IP address you chose above for “ip_addr”. Now type some text and press “enter”. The text should then repeat on the line below what you just typed.

You can either close the connection or type $QUIT and <enter> to quit.

Have fun!

Files Needed for This Project