WifiRS21 initialize

Hey All,

I have Spider 2 board and try to work with my WifiRS21 module
run the following code allays trough me Exception (InvalidSocketException)


 WiFiRS21 _wifi  = new WiFiRS21(9);

working with WiFiRS9110 class but with WiFiRS21 module works great

please advice

Thank’s Lior

Not sure what you are doing but the designer uses


Program.generated.cs uses:
   this.wifiRS21 = new GTM.GHIElectronics.WiFiRS21(6);
OR
    this.wifiRS21 = new GTM.GHIElectronics.WiFiRS21(9);

     // setup network events
     wifiRS21.NetworkDown += wifiRS21_NetworkDown;
     wifiRS21.NetworkUp += wifiRS21_NetworkUp;

        private void wifiRS21_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            if (useDebug) { Debug.Print("WiFi Network UP"); };
            if (useDebug) { Debug.Print("LinkConnected is: " + wifiRS21.NetworkInterface.LinkConnected.ToString()); };
            if (useDebug) { Debug.Print("NetworkAvailable is: " + wifiRS21.NetworkInterface.NetworkAvailable.ToString()); };
        }

        private void wifiRS21_NetworkDown(GTM.Module.NetworkModule   sender, GTM.Module.NetworkModule.NetworkState state)
        {
            if (useDebug) { Debug.Print("wifiRS21 Network Down. Always down before it is up.."); };
        }
        //

        // Called on a button press
        private void TryConnection()
        {
            if (useDebug) { Debug.Print("!! Socket thread!"); };

            if (!wifiRS21.NetworkInterface.Opened)
            {
                wifiRS21.NetworkInterface.Open();
                wifiRS21.UseDHCP();

                wifiRS21.NetworkInterface.Join(yourSSID, yourKEY);

                if (useDebug) { Debug.Print("Waiting for DHCP"); };

                while (wifiRS21.NetworkInterface.IPAddress == "0.0.0.0")
                {
                    Thread.Sleep(250);
                }

                Microsoft.SPOT.Net.NetworkInformation.NetworkInterface networkInterface =
                    Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];

                if (useDebug) { Debug.Print("IP address: " + networkInterface.IPAddress.ToString()); };
                if (useDebug) { Debug.Print("SubnetMask: " + networkInterface.SubnetMask.ToString()); };
                if (useDebug) { Debug.Print("GatewayAddress: " + wifiRS21.NetworkInterface.GatewayAddress.ToString()); };
                if (useDebug) { Debug.Print("NetworkInterfaceType: " + wifiRS21.NetworkInterface.NetworkInterfaceType.ToString()); }; //Wireless80211 = 71,
               
                using (Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    IPAddress ipAddress = IPAddress.Parse(wifiRS21.NetworkInterface.IPAddress);

                    IPEndPoint serverEndPoint = new IPEndPoint(ipAddress, PORT); //PORT is 8080

                    if (useDebug) { Debug.Print("Connecting to server at: " + serverEndPoint); };

                    clientSocket.Connect(serverEndPoint);

                    if (useDebug) { Debug.Print("Connected to server."); };

                    byte[] messageBytes = Encoding.UTF8.GetBytes("Hello World!");
                    clientSocket.Send(messageBytes);

                } // Socket closed
            }
        }
        //

This works on my SpiderII
Hope it is of some help

@ liorbennaim - Do you have anything else connected in the designer?