Ok I attach photo and here is my code:
using System;
using System.IO.Ports;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Socket = System.Net.Sockets.Socket;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
namespace Auticko
{
public class Program
{
public static bool wifi_event = false;
public static bool wifi_last_status = false;
public static bool network_is_read = false;
public static ManualResetEvent NetworkAvailablityBlocking = null;
public static void Main()
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
if (!WiFi.IsEnabled)
{
WiFi.Enable(SPI.SPI_module.SPI2, (Cpu.Pin)FEZ_Pin.Digital.IO2, (Cpu.Pin)FEZ_Pin.Interrupt.IO26);
}
// WiFi settings
NetworkInterface[] netif = NetworkInterface.GetAllNetworkInterfaces();
Wireless80211 WiFiSettings = null;
for (int index = 0; index < netif.Length; ++index)
{
if (netif[index] is Wireless80211)
{
WiFiSettings = (Wireless80211)netif[index];
}
}
//WPAPSK v HK
//WiFiSettings.Ssid = "thomas";
//WiFiSettings.NetworkKey = new byte[] { 0x36, 0x3e, 0x65, 0x19, 0x9d, 0xa4, 0x29, 0x3d, 0x19, 0x87, 0xb1, 0xe5, 0x99, 0x29, 0x1e, 0x51, 0xc5, 0xc2, 0xfa, 0x93, 0x5b, 0x40, 0xbc, 0x4f, 0x61, 0x4f, 0x98, 0x24, 0x49, 0xf3, 0xf1, 0x28 };
//WiFiSettings.Encryption = Wireless80211.EncryptionType.WPAPSK;
//WiFiSettings.Authentication = Wireless80211.AuthenticationType.Shared;
WiFiSettings.Ssid = "Adoss";
WiFiSettings.Encryption = Wireless80211.EncryptionType.None;
WiFiSettings.Authentication = Wireless80211.AuthenticationType.Open;
Wireless80211.SaveConfiguration(new Wireless80211[] { WiFiSettings }, false);
NetworkAvailablityBlocking = new ManualResetEvent(false);
if (!WiFi.IsLinkConnected)
{
Debug.Print("Waiting for WiFi link!");
NetworkAvailablityBlocking.Reset();
while (!NetworkAvailablityBlocking.WaitOne(5000, false))
{
if (!WiFi.IsLinkConnected)
{
Debug.Print("WiFi link is not available yet! Wrong AP settings?Still waiting.");
Debug.Print("Still waiting.");
}
else
break;
}
}
try
{
if (!WiFiSettings.IsDhcpEnabled)
WiFiSettings.EnableDhcp();// This function is blocking
else
{
WiFiSettings.RenewDhcpLease();// This function is blocking
}
network_is_read = true;
Debug.Print("Network settings:");
Debug.Print("IP Address: " + WiFiSettings.IPAddress);
Debug.Print("Subnet Mask: " + WiFiSettings.SubnetMask);
Debug.Print("Default Getway: " + WiFiSettings.GatewayAddress);
Debug.Print("DNS Server: " + WiFiSettings.DnsAddresses[0]);
}
catch
{
Debug.Print("DHCP Failed");
}
Debug.Print("WiFi link is ready!");
Thread listener = new Thread(listen);
listener.Start();
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint remoteEndPoint = new IPEndPoint(new IPAddress(new byte[] { 192, 168, 1, 7 }), 2000);
//socket.Bind(remoteEndPoint);
//socket.Connect(remoteEndPoint);
socket.SendTo(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, remoteEndPoint);
//socket.Send(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 }, SocketFlags.None);
socket.Close();
Debug.Print("Ready");
}
static void listen()
{
SerialPort serialPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
serialPort.Open();
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 2000);
socket.Bind(remoteEndPoint);
while (true)
{
if (socket.Poll(-1, SelectMode.SelectRead))
{
byte[] inBuf = new byte[socket.Available];
int count = socket.ReceiveFrom(inBuf, ref remoteEndPoint);
//if (count == 0) continue;
string s = "Recieved ";
for (int i = 0; i < inBuf.Length; i++)
s += inBuf[i] + ";";
Debug.Print(s);
if (inBuf[0] == 255)
{
serialPort.Open();
serialPort.Write(inBuf, 0, inBuf.Length);
serialPort.Close();
}
Debug.Print("Servo " + inBuf[1] + "is at " + inBuf[2]);
}
}
}
static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
if (WiFi.IsLinkConnected)
{
if (wifi_last_status != true)
{
wifi_last_status = true;
NetworkAvailablityBlocking.Set();
}
}
}
else
{
if (!WiFi.IsLinkConnected)
{
if (wifi_last_status != false)
{
wifi_last_status = false;
network_is_read = false;
}
}
}
}
}
}
but if I’m debbuging, it freezes on third row in main method - Wifi.Enable()