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