Ethernet ENC28, socket and supply power

Hi,

I would like to send data via Ethernet ENC28 from a Fez Raptor [NETMF 4.3] to PC [Classical Framework].
But, I don’t understand why my socket doesn’t work. Maybe I don’t use the good tools.

Here my code for Fez Raptor [NETMF 4.3] :


using System;
using System.Collections;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using System.Net.Sockets;
using System.Net;

using Microsoft.SPOT.Net.NetworkInformation;


namespace test_netmf43
{
    public partial class Program
    {
        #region Test Ethernet
        Socket socket; //System.Net.Sockets.Socket socket;
        bool EthernetIsConncted = false;

        void ProgramStarted()
        {
            ethernetENC28.UseThisNetworkInterface();
            ethernetENC28.UseStaticIP("192.168.100.2", "255.255.255.0", "");

            Debug.Print("Creating socket...");
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Debug.Print("[new socket] LocalEndPoint: " + socket.LocalEndPoint.ToString());


            ethernetENC28.NetworkUp += ethernetENC28_NetworkUp;
            ethernetENC28.NetworkDown += ethernetENC28_NetworkDown;

            RunServer();

            new Thread(RunServer).Start();

        }

        void ethernetENC28_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is down!");
            EthernetIsConnected = false;
        }

        void ethernetENC28_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Network is up!");
            Debug.Print("My IP is: " + ethernetENC28.NetworkSettings.IPAddress);
        }

        void RunServer()
        {

            // Wait for the network...
            while (ethernetENC28.IsNetworkUp == false)
            {
                Debug.Print("Waiting...");
                Thread.Sleep(1000);
            }
            
            #region Socket
            while (ethernetENC28.IsNetworkUp && !EthernetIsConnected)
            {
                try
                {
                    socket.Connect(new IPEndPoint(IPAddress.Parse("192.168.100.1"), 666));
                    EthernetIsConnected = true;
                    Debug.Print("[socket] RemoteEndPoint: " + socket.RemoteEndPoint.ToString());
                    Debug.Print("[socket] LocalEndPoint: " + socket.LocalEndPoint.ToString());
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                }
            }
            #endregion
        }
    }
}

Here my code for PC :

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace serverPC
{
    public class Program
    {
        static void Main(string[] args)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Parse("192.168.100.1"), 666));
            socket.Listen(1);

            var sock = socket.Accept();
        }
    }
}

Does anyone have an idea of what is going wrong ?
Note : I can ping my device, but socket never get 192.168.100.2:666 as an LocalEndPoint. It is set to a random IP address and Port : 0.


Moreover I read on GHI Electronics catalog at Ethernet ENC28 module :

I suspect the Ethernet problem could be due to the power supply, because I only use an USB Client DP module. I am not a specialist in electronics, so what type of external DC power or USB Hub can I use to continue to work (debug) my project ?

Thanks by advanced,

Laura

trying opening the network interface on the raptor

Thanks for your answers.

@ andre.m -

Yes I set my PC’s interface as 192.168.100.1.

[quote]you start your “server” twice, one time is enough
your “while” loop in your “server” does not make sense, 1. what do you think which value has !EthernetIsConnected? 2. you normally connect a socket one time…[/quote]

I start my server twice ? Or my client ? I am not sure to know what part of code you are talking about. Here ?

 
            ethernetENC28.UseThisNetworkInterface();
            ethernetENC28.UseStaticIP("192.168.100.2", "255.255.255.0", "");

  1. EthernetIsConnected turns to true if it is enter to the while loop and try to connect. Yes maybe, it is not necessary, but since I am making a try catch…I wanted to be sure.
  2. Yes, sure.

When I run it the first time, I enable the port on my “personal” PC, but I can’t do it at work… Maybe I should try an other solution… Can you suggest me something more convinient?

@ Mike - What do you mean by trying to open the interface? Doesn’t


ethernetENC28.UseThisNetworkInterface();

make it ?

Yes, that’s right !
Maybe I uncommented a wrong like before posting. Sorry. I don’t remember if I run my code like this. I will try back tomorrow.

Do you think it could be the cause of socket’s random IP addresses?

@ andre.m - The socket I try to connect to 192.168.100.1:666 should have :
192.168.100.2:666 as a LocalEndPoint
And 192.168.100.1:666 as a RemoteEndPoint.
Right?

But it gets an other IP address (for example 16.22.105.38 ) and port 0 for both. And I have nothing created in this IP address on my computer.

@ Mike - What do you mean by trying to open the interface? Doesn’t
ethernetENC28.UseThisNetworkInterface();

My mistake. I checked the latest Gadgeteer source code and it does include the open.

usually x.x.x.1 is the network gateway/router. Are you sure you don’t have an address conflict?

@ andre.m - Sorry, I didn’t use proper name. The NETMF code is for the client. The other code on classical framework is for server and contains Bind().

@ Mike - So I should change it to x.x.x.3 for example?

you shouldn’t randomly change it to anything, you should change it to explicitly what your network addresses are. If you don’t have control of the addresses, find someone who does who can confirm what they should be.

So, for what it is worth, I have been able to reproduce your problem, for both static and dynamic (dhcp) configurtions and it does not seem related to your code. My G400D v1.3 on a development board is showing up with a mac address of GHI:00:00:01, which isn’t right (the sticker says GHI:80:5F:96). I am reflashing the board now with updated firmware (per the similar problem found at : [url]https://www.ghielectronics.com/community/forum/topic?id=21638[/url]). More news shortly - just wanted to let folks know that I have a repro on a fresh G400D.

First off Laura, I don’t think the problem is in your code. Maybe we both could be wrong here, but aside from a couple nitpicks, the code looks ok. I have tried correcting those nitpicks and it doesn’t help. The nitpicks were the lack of a gateway addr, and creating the socket before the network stack is up - fixing those didn’t seem to help.

My code is a little different than yours, because I am not using the Gadgeteer stuff, but the problem still repros.

Here’s what I did:
I tried a test program (included below), and could not get it to work for static or dynamic addresses.
So, I updated my G400D Loader and Firmware.
In the end, I can reliably get DHCP (dynamic) configurations to work (but only outside the debugger), and static configurations reliably do not work.

Observations:
[ul]The G400D had a MAC addr of GHI:00:00:01. I used FezConfig to set that to the value on the sticker on the back of the SOM, as described previously. This may have affected DHCP, and may not, but prior to the firmware update and setting the MAC addr, DHCP was not working and it was working afterward. I don’t put a lot of stock in the MAC change having affected anything, but fresh firmware is never a bad thing, so be sure to update your loader and firmware.[/ul]
[ul]If I run the sample code shown below, with static = false, in the debugger, it does not work and never detects a valid network (never gets an IP addr - just print “Waiting… IPAddr=0.0.0.0”)[/ul]
[ul]If I then hit reset to run the code with static = false, WITHOUT the debugger, it works fine and the server code hits the PRESS ENTER line. So this case works, just not in the debugger[/ul]
[ul]If I run the sample code with static = true in the debugger, the connect times out, with no exception, and with random local and remote endpoints as you are seeing.[/ul]
[ul]If I run the sample code with static = true WITHOUT the debugger, no connection is received by the server app (presumably, it is behaving like it did in the debugger).[/ul]
[ul]In all cases, the board can be pinged (so there is a valid network path)[/ul]

Other weirdness:
From wireshark, I see that with static=false, the DHCP packets fly by and then we get a TCP connection sequence.
With static=true, I never see the connection sequence, but I do see an IGMP Membership Report for group 224.0.0.1, which I certainly never expected. No idea why it is trying to do multicast stuff. I am probably not seeing all the traffic because I am running wireshark on my host and don’t have my switch set up for port replication, but the server is definitely never seeing a tcp connect request.
When the connect fails, it does not throw an exception - but the socket gets bound to random IP addr values :

[new socket] LocalEndPoint: 0.0.0.0:0
[socket] RemoteEndPoint: 48.192.107.38:0
[socket] LocalEndPoint: 200.193.107.38:0

The client code:

using System;
using System.Net.Sockets;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;

using GHI.Networking;
using System.Net;

namespace Client
{
    public class Program
    {
        private static BaseInterface _netif;
        private static Socket _socket;
        private static bool _networkIsUp;

        public static void Main()
        {
            _netif = new EthernetBuiltIn();
            _netif.Open();

            bool fStatic = true;

            if (fStatic)
            {
                _netif.EnableStaticIP("192.168.1.123", "255.255.255.0", "192.168.1.1");
            }
            else
            {
                if (!_netif.IsDhcpEnabled)
                    _netif.EnableDhcp();
            }

            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;

            new Thread(RunServer).Start();

            Thread.Sleep(Timeout.Infinite);
        }

        private static void RunServer()
        {
            while (!_networkIsUp)
            {
                Debug.Print("Waiting...  IPAddr=" + _netif.IPAddress);
                if (_netif.IPAddress != "0.0.0.0") // because the event doesn't fire if the addr didn't actually change
                    _networkIsUp = true;
                Thread.Sleep(1000);
            }

            Debug.Print("Creating socket...");
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            Debug.Print("[new socket] LocalEndPoint: " + _socket.LocalEndPoint.ToString());

            try
            {
                _socket.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.236"), 31079));
                Debug.Print("[socket] RemoteEndPoint: " + _socket.RemoteEndPoint.ToString());
                Debug.Print("[socket] LocalEndPoint: " + _socket.LocalEndPoint.ToString());
            }
            catch (Exception e)
            {
                Debug.Print(e.Message);
            }
        }

        private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            Debug.Print(" The network " + (e.IsAvailable ? "is " : "is not ") + "available");
        }

        private static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            var addr = _netif.IPAddress;
            Debug.Print("ip addr : " + addr);
            _networkIsUp = true;
        }
    }
}

The server code:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Bind(new IPEndPoint(IPAddress.Any, 31079));
            Console.WriteLine("Listening");
            socket.Listen(1);

            Console.WriteLine("Accepting");
            var sock = socket.Accept();
            Console.WriteLine("Accepted");

            Console.WriteLine("Press ENTER");
            Console.ReadLine();
        }
    }
}

For what it’s worth, running the exact same code on a G120E development board works fine for both static and dynamic cases and in or out of the debugger. The static case does also send an IGMP broadcast packet, but then proceeds to function correctly.

The above failing cases were done on a G400D development board.

G120E info:
Loader (TinyBooter) version information:
4.3.7.7 on this computer.
4.3.7.7 on this device.

The Loader (TinyBooter) is up to date. <<<

Firmware (TinyCLR) version information:
4.3.7.10 on this computer.
4.3.7.10 on this device.

The Firmware (TinyCLR) is up to date. <<<
Please wait for the device to reboot… Done.

HalSystemInfo.halVersion :4.3.1.0
HalSystemInfo.halVendorInfo :Microsoft Copyright (C) Microsoft Corporation. All rig
ClrInfo.clrVersion :4.3.1.0
ClrInfo.clrVendorInfo :Microsoft Copyright (C) Microsoft Corporation. All rig
ClrInfo.targetFrameworkVersion :4.3.1.0
SolutionReleaseInfo.solutionVersion: 4.3.7.10
SolutionReleaseInfo.solutionVendorInfo: Copyright (C) GHI Electronics, LLC
SoftwareVersion.BuildDate: Jul 29 2015
SoftwareVersion.CompilerVersion: 410713
LCD.Width: 320
LCD.Height: 240
LCD.BitsPerPixel: 16

G400D info:
Loader (TinyBooter) version information:
4.3.7.7 on this computer.
4.3.7.10 on this device.

The Loader (TinyBooter) not up to date. <<<

Firmware (TinyCLR) version information:
4.3.7.10 on this computer.
4.3.7.10 on this device.

The Firmware (TinyCLR) is up to date. <<<
Please wait for the device to reboot… Done.

HalSystemInfo.halVersion :4.3.1.0
HalSystemInfo.halVendorInfo :Microsoft Copyright (C) Microsoft Corporation. All rig
ClrInfo.clrVersion :4.3.1.0
ClrInfo.clrVendorInfo :Microsoft Copyright (C) Microsoft Corporation. All rig
ClrInfo.targetFrameworkVersion :4.3.1.0
SolutionReleaseInfo.solutionVersion: 4.3.7.10
SolutionReleaseInfo.solutionVendorInfo: Copyright (C) GHI Electronics, LLC
SoftwareVersion.BuildDate: Jul 31 2015
SoftwareVersion.CompilerVersion: 410713
LCD.Width: 480
LCD.Height: 272
LCD.BitsPerPixel: 16

@ mcalsyn - Thanks for all your tests

My firmware was on date on both my computer and device but I updated it just in case.

MAC adress is written on the device, isn’t it?
Anyways, I assume the one provided by FEZ Config is good.

So I tried your code on my device in changing your IP adress by the one provided by FEZ Config (192.168.120.2 as server and 192.168.1.203 as client) and kept the same port.
I changed my PC Ethernet local network properties (TCP/IPv4) for 192.168.1.202 to be sure.

And I run it on debug mode : my IPAddr = 0.0.0.0 and I can’t ping my device anymore.

So maybe as it was question in the previous topic, it is due to the debug environment…
But I have never work out of the debug environment…

yes

Not necessarily

Well, the whole static setup never worked for me on my G400D - either in debug or out of the debugger. I just plain can’t get static IP to work. Dynamic addressing works fine and the same code (static and dynamic) works fine on G120E. We need to hear from the GHI folks.

@ andre.m - I’m pretty sure the problem was well stated. Which part didn’t you understand?

The problem manifests itself in socket.Connect(), which is not present in the code you posted. How is your example relevant to the problem?

Congratulations. We appear then to have a new issue.

That code isn’t dramatically different, and gives the same failed result for me (though I changed the code to use EthernetBuiltIn(). I don’t have an ENC, but Laura does and is seeing the same results. Maybe she can try your code with her ENC)

Seems pretty clear that there’s something wrong with Laura and my firmware setup. Whatever it is, we both got it.

NetworkAvailabilityChanged: True
eth0
IPv4 Address 172.16.0.47
IPv4 SubnetMask 255.255.255.0
IPv4 Gateway 172.16.0.1
IPv4 DNS Server 172.16.0.254
eth1
IPv4 Address 0.0.0.0
IPv4 SubnetMask 0.0.0.0
IPv4 Gateway 0.0.0.0
eth3
IPv4 Address 0.0.0.0
IPv4 SubnetMask 0.0.0.0
IPv4 Gateway 0.0.0.0
server running
32.193.107.38:0
240.195.107.38:0

@ andre.m - Thanks for the codes. But I use Fez Raptor so G400S, not G400D. Does it change something? Which port do I have to plug EthernetENC28 daisylink?

Where is printed MAC Address on FEZ Raptor board? I am still looking upside-down without finding it…

One more thing I notice when I “load built-in Ethernet Config” on FEZ Config:
DNS Primary Address: 0.0.0.0
DNS Second Address: 0.0.0.0

@ andre.m - Yes cable, not daisylink. Sorry. So I place the cable on socket #1.

Hum… nothing occurs. Neither network address nor network availability change.
Also I can’t ping my device.

Unfortunately, I got something quiet similar to mcalsyn

NetworkAvailabilityChanged: True
eth0
IPv4 Address 0.0.0.0
IPv4 SubnetMask 0.0.0.0
IPv4 Gateway 0.0.0.0
eth1
IPv4 Address 172.16.0.47
IPv4 SubnetMask 255.255.255.0
IPv4 Gateway 172.16.0.1
IPv4 DNS Server 172.16.0.254
eth3
IPv4 Address 0.0.0.0
IPv4 SubnetMask 0.0.0.0
IPv4 Gateway 0.0.0.0
server running
92.193.107.38:0
12.180.107.38:0