Ethernet ENC28, socket and supply power

@ andre.m - Yes, I have…

The result is still the same.

I don’t change my MAC address, so I use the one provided by FEZ Config : 0-21-3-0-0-2.
I don’t know where it is written on the device…

The ENC28 does not come with a MAC address. You need to get a new address. I get mine at MAC Address Generator

Not sure if i am correct, but I use your code with a bit change and always work with me, no matter dynamic or static IP.

G400S:

using GHI.Networking;

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Security;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.IO.Ports;
using GHI.Pins;

namespace TestClientServerTCPENC28
{
    public class Program
    {
        static EthernetENC28J60 netif;
        const int PIN6_SOCKET1_S = (1 * 32) + 5; 
        const int PIN3_SOCKET1_S = (1 * 32) + 0; 
        const int PIN4_SOCKET1_S = (0 * 32) + 7; 
        static OutputPort led = new OutputPort((Cpu.Pin)(3 * 32 + 3), true); // led pd3
        static InputPort ldr1 = new InputPort((Cpu.Pin)(4), false, Port.ResistorMode.PullUp);
        static bool useDHCP = true; 
        static bool isNetworkReady = false;
        static Socket socket;
        public static void Main()
        {
            while (ldr1.Read() == true)
            {
                led.Write(!led.Read());
               
                Thread.Sleep(25);
            }
            while (ldr1.Read() == false ) ;
            netif = new EthernetENC28J60(SPI.SPI_module.SPI2, (Cpu.Pin)PIN6_SOCKET1_S, (Cpu.Pin)PIN3_SOCKET1_S, (Cpu.Pin)PIN4_SOCKET1_S); // socket 1 Fez Raptor
            NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
            NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(NetworkChange_NetworkAddressChanged);
            netif.Open();
            Debug.Print("Opened.");
            if (useDHCP == false)
            {

                netif.EnableStaticIP("192.168.1.122", "255.255.255.0", "192.168.1.1");
                netif.EnableStaticDns(new string[] { "75.75.75.75", "75.75.76.76" });

            }
            else
            {
                //// DHCP IP
                if (netif.IsDhcpEnabled == false)
                {
                    netif.EnableDhcp();
                    netif.EnableDynamicDns();
                }
            }
            RunServer();
         
        }
        static void RunServer()
        {
            int waitcnt = 0;
            while (isNetworkReady == false)
            {
                Debug.Print("Wait for network ready " + waitcnt++);
                Thread.Sleep(1000);
            }
            Debug.Print("Creating socket...");
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            bool EthernetIsConnected = false;
            while (!EthernetIsConnected)
            {
                try
                {
                    socket.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.90"), 666));
                    EthernetIsConnected = true;
                    Debug.Print("[socket] RemoteEndPoint: " + socket.RemoteEndPoint.ToString());
                    Debug.Print("[socket] LocalEndPoint: " + socket.LocalEndPoint.ToString());
                }
                catch (Exception e)
                {
                    Debug.Print(e.Message);
                }
            }
            Debug.Print("Socket Connected");
            Thread.Sleep(-1);
          
        }
        static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            Debug.Print("*****************************Network has changed!************************** ");
        }
        
        static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("New address for the Network Interface ");
            Debug.Print("Is DhCp enabled: " + netif.NetworkInterface.IsDhcpEnabled);
            Debug.Print("Is DynamicDnsEnabled enabled: " + netif.NetworkInterface.IsDynamicDnsEnabled);
            Debug.Print("NetworkInterfaceType " + netif.NetworkInterface.NetworkInterfaceType);
            Debug.Print("Network settings:");
            Debug.Print("IP Address: " + netif.NetworkInterface.IPAddress);
            Debug.Print("Subnet Mask: " + netif.NetworkInterface.SubnetMask);
            Debug.Print("Default Gateway: " + netif.NetworkInterface.GatewayAddress);
            string MACAddress = "";
            for (int i = 0; i < netif.NetworkInterface.PhysicalAddress.Length; i++)
            {
                MACAddress += " " +netif.NetworkInterface.PhysicalAddress[i].ToString();
               
            }
            Debug.Print("MAC Address: " + MACAddress);
            Debug.Print("Number of DNS servers:" + netif.NetworkInterface.DnsAddresses.Length);
            for (int i = 0; i < netif.NetworkInterface.DnsAddresses.Length; i++)
                Debug.Print("DNS Server " + i.ToString() + ":" + netif.NetworkInterface.DnsAddresses[i]);
            Debug.Print("------------------------------------------------------");
            if (netif.IPAddress != "0.0.0.0")
            {
                isNetworkReady = true;
            }
        }
    }
}

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.1.90"), 666));
            socket.Listen(1);

            var sock = socket.Accept();
            Console.WriteLine("Accepted client " + sock.RemoteEndPoint);
            Console.ReadLine();
        }
    }
}

@ Mike - Ok, that’s why I can’t read it on the device.
Thanks for the link, but I can’t have Internet and EthernetENC28 in the same time on my computer.
So I used and re-use FEZ Config to generate one… but it is still the same.

I think we have a few examples of working code, and I have tried them all locally, and none work for me (on my G400D - they do work on my G120E) and so far none have worked for Laura. I continue to maintain that we have some sort of firmware or firmware config problem. That’s re-enforced by the fact that both Laura and I are seeing the same results with code that is working for other folks, so it is environmental. Either something is mis-matched or mis-installed.

@ Dat - Thanks for your code. It also doesn’t work. My device doesn’t get out off the while loop


            while (ldr1.Read() == true)
            {
                led.Write(!led.Read());
               
                Thread.Sleep(25);
            }

I don’t know what it is for… But the LED blinks for sure.

So then I commented this part of code and I was stuck in this while loop :


            while (isNetworkReady == false)
            {
                Debug.Print("Wait for network ready " + waitcnt++);
                Thread.Sleep(1000);
            }

Ok, it is not a really important stuff.

To sum up, with DHCP, it doesn’t work.

And with static, it is still the same :

Opened.
Wait for network ready 0
Wait for network ready 1
Wait for network ready 2
Wait for network ready 3
***Network has changed!
New address for the Network Interface
Is DhCp enabled: False
Is DynamicDnsEnabled enabled: False
NetworkInterfaceType 6
Network settings:
IP Address: 192.168.1.122
Subnet Mask: 255.255.255.0
Default Gateway: 192.168.1.1
MAC Address: 158 176 121 8 230 199
Number of DNS servers:2
DNS Server 0:75.75.75.75
DNS Server 1:75.75.76.76

Creating socket…
[socket] RemoteEndPoint: 180.183.107.38:0
[socket] LocalEndPoint: 204.29.108.38:0
Socket Connected
The program ‘[9] Micro Framework application: Managed’ has exited with code 0 (0x0).

With… I tried both.

OK, so I don’t really understand your comment here or what it implies about your network. Why can’t you connect to the internet when you are testing this? Are you physically connecting the network cable from the ENC28 into your PC? When you’re testing with DHCP, do you actually have a DHCP server somewhere on your network that is going to respond? If you don’t, then the DHCP test is going to fail because of that so the results may not be relevant

@ Brett - Yes, I have a Network cable between Ethernet ENC28 and my PC.
The aim of this topic is to send data from Fez Raptor to PC via Ethernet… But if you have a better idea to send data I will try.

Connect them both to a network switch.

If the PC and Raptor are directly connected with an Ethernet cable, then the cable must be a “crossover” cable.

although most modern PC Ethernet ports will auto-sense this, so possibly no longer an issue. But certainly with a direct cable like that there will never be a DHCP response so it’s a totally invalid test!

And fwiw, with a proper network setup, including switch, router, etc., with every one of the code samples, it also fails for me in exactly the same way.

I still see all the originally reported symptoms on G400D dev board and proper operation on G120 dev board with the exact same code.

1 Like

Right I didn’t use a crossover cable. I will see if I can find one, but not sure.
I have never used a switch, I will see how it works.

People who archieve to make its code runs what did you use?

[Edit] Wait… crossover cable is for two same devices, patch cable for two different devices, isn’t it? But right here it is two different devices so I should use a patch cable (as I have already done).

@ andre.m - It doesn’t work too. The result is still the same as previous.
Using a switch :
DHCP doesn’t work.
Static : socket doesn’t connect, but I can ping my device… So maybe mcalsyn is right. Something goes wrong with Gadgeteer debug environment.

My new questions now are:

  • how can I send data out of this environment via Ethernet ENC28?
  • how can I use directly USB Client DP to receive my data on PC and make run a C# code then?

Can you please tell us about your network? Is this a work or a home network? Do you know if there’s a DHCP server or not? Some of these things are things you absolutely need to understand before you can move too far past where you are now

And if I take a further step back, can you tell us about what you actually want to achieve? I assume since you’re now talking about USB that all you really need is a way to get data from the Gadgeteer device over to the PC, using whatever means is appropriate. How much data, how fast samples, and for how long?

I have a rock solid local network with 1gbps fiber modem, high-speed router, managed switch, and full network infrastructure including DHCP.

Using all three test examples (which aren’t actually different in any meaningful way):

G400D dev board:
Statically-assigned addresses, in a known-good subnet and known-available address do not work.
DHCP-assigned address work, but only outside of the debugger.
.connect() times out, but does not throw an exception. The local and remote endpoint values on the socket get random values.

G120 dev board:
Everything works as expected.

The test programs are simple and correct. There is either some weird firmware issue or corruption on my board. I have not eliminated power shortages (both during flash and during the test). That’s next, but not for some hours as I am booked up with other tasks. So all this talk of cables and such makes no sense to me, because we both see the same issues and there is no network issue here.

Work network.

Whatever means is appropriate. I would like to send data from Gadgeteer Fez Raptor to PC.
Just some processed measures from sensors to PC every 500 ms for an unknown time (maybe up to 1-2 hours).

[quote=“andre.m”]

But if I use this device, can I debug in the same time? If I understand Gadgeteer modules well, only with USB Client allow debug environment. I am using USB Client DP. So can I use it in the same time to debug?
https://www.ghielectronics.com/docs/101/powering-the-board