Main Site Documentation

[SOLVED] panda II & ethernet shield


#1

hi,

I’m trying to test arduino ethernet shield with panda II card. I just try the sample code given in the tutorial.

using System;
using System.Text;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Net;
using GHIElectronics.NETMF.Net.Sockets;
using GHIElectronics.NETMF.Net.NetworkInformation;

using Socket = GHIElectronics.NETMF.Net.Sockets.Socket;

/// <summary>
/// This is a simple web server. Given a request, it returns an HTML document. 
/// The same document is returned for all requests and no parsing of
/// the request is done.
/// </summary>
public static class MySocketServer
{
    public static void Main()
    {
        const Int32 c_port = 80;

        byte[] ip = { 172, 17, 210, 200 };
        byte[] subnet = { 255, 255, 0, 0 };
        byte[] gateway = { 172, 17, 0, 1 };
        byte[] mac = { 0x00, 0x26, 0x1C, 0x7B, 0x29,0xE8 };

        WIZnet_W5100.Enable(SPI.SPI_module.SPI1,
                            (Cpu.Pin)FEZ_Pin.Digital.Di10,
                            (Cpu.Pin)FEZ_Pin.Digital.Di9, true);

        try
        {
            NetworkInterface.EnableStaticIP(ip, subnet, gateway, mac);
            NetworkInterface.EnableStaticDns(new byte[] { 172, 17, 0, 1 });

            Socket server = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

//            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);
            IPAddress hostIPAddress = IPAddress.Parse("172.17.210.200");
            IPEndPoint localEndPoint = new IPEndPoint(hostIPAddress, c_port);

            server.Bind(localEndPoint);
            server.Listen(1);

            while (true)
            {
                // Wait for a client to connect.
         [b]       Socket clientSocket = server.Accept();// problem here[/b]
                // Process the client request. true means asynchronous.
                new ProcessClientRequest(clientSocket, true);
            }
        }
        catch (Exception e)
        {
            Debug.Print(e.ToString());
        }
    }
    /// <summary>
    /// Processes a client request.
    /// </summary>
    internal sealed class ProcessClientRequest
    {
        private Socket m_clientSocket;
        /// <summary>
        /// The constructor calls another method to handle the request, 
        /// but can optionally do so in a new thread.
        /// </summary>
        /// <param name="clientSocket"></param>
        /// <param name="asynchronously"></param>
        public ProcessClientRequest(Socket clientSocket,
                                    Boolean asynchronously)
        {
            m_clientSocket = clientSocket;
            if (asynchronously)
                // Spawn a new thread to handle the request.
                new Thread(ProcessRequest).Start();
            else ProcessRequest();
        }
        /// <summary>
        /// Processes the request.
        /// </summary>
        private void ProcessRequest()
        {
            const Int32 c_microsecondsPerSecond = 1000000;
            // 'using' ensures that the client's socket gets closed.
            using (m_clientSocket)
            {
                // Wait for the client request to start to arrive.
                Byte[] buffer = new Byte[1024];
                if (m_clientSocket.Poll(5 * c_microsecondsPerSecond,
                SelectMode.SelectRead))
                {
                    // If 0 bytes in buffer, 
                    // then the connection has been closed,
                    // reset, or terminated.
                    if (m_clientSocket.Available == 0)
                        return;
                    // Read the first chunk of the request 
                    // (we don't actually do anything with it).
                    Int32 bytesRead = m_clientSocket.Receive(buffer,
                    m_clientSocket.Available, SocketFlags.None);

                    // Return a static HTML document to the client.
                    String s =
                    "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\n\r\n<html><head><title>.NET Micro Framework Web Server on USBizi Chipset </title></head>"
                    + "<body><bold><a href=\"http://www.tinyclr.com/\">Learn more about the .NET Micro Framework with FEZ by clicking here</a></bold></body></html>";
                    byte[] buf = Encoding.UTF8.GetBytes(s);
                    int offset = 0;
                    int ret = 0;
                    int len = buf.Length;
                    while (len > 0)
                    {
                        ret = m_clientSocket.Send(buf, offset, len,
                                                  SocketFlags.None);
                        len -= ret;
                        offset += ret;
                    }
                    m_clientSocket.Close();
                }
            }
        }
    }
}

But the call to function Accept fail. I have inserted a try catch to intercept exception. So the message is:
#### Exception System.Exception - 0x00000000 (1) ####
#### Message: Fail[invalid ip,port]

I’ve replaced IPAddress.Any by the IP address of the card but without result.

What can I do?

Thank you

Joe: I fixed the MAC address


#2

You have commented out the following line of code.

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, c_port);

Why?

What happens if you use the original code in the sample?


#3

What shield exactly are you using ? If it is the Arduino Ethershield (v5) with the micro SD card reader embedded, you will most likely need to modify a little bit the shield for the SPI wires to be redirected from the small connector “under the board” to the SPI pins on the panda II (DI11, DI12, DI13), plus the CS and reset lines (DI9 and DI10). This is easy to do but requires soldering a few wires… :slight_smile:

Furthermore, Mike is right, if you are using a web server you should not listen to your own IP address ! :wink:


#4

Here is the WiKi page that show you how to modify your shield:

http://wiki.tinyclr.com/index.php?title=FEZ_and_Arduino_Ethernet_Shield_05


#5

Thank’s Sam, Nicolas and Mike. I’ve add the three wires and it’s clear now.

[quote]Furthermore, Mike is right, if you are using a web server you should not listen to your own IP address !
[/quote]does the panda have other IP address? It’s clear too if I bind with 172.17.210.200.

The IP address of the computer is 172.17.5.124


#6

[quote] It’s clear too if I bind with 172.17.210.200.

[/quote]

I did not know you had not wired the shield correctly.

It is not necessary to bind to you interface address. I guess it works, but it is good practice not to hardcode any address that is not necessary.


#7

Only one IP address is possible (unlike a PC for an example). So as far as I understood “binding” does not have exactly the same meaning here. IPAddress.Any worked for all my tests !


#8

thank for these advices

Very good forum !


#9

I recently found out a design bug for this particular shield, on the power side, and I modified the wiki accordingly to allow people to fix it !
http://wiki.tinyclr.com/index.php?title=FEZ_and_Arduino_Ethernet_Shield_05#Modify_the_Shield


#10

It’s what I’ve made

thanks