Main Site Documentation

Connecting a Hydra with a PC by Ethernet


#1

I need to have a Hydra communicate with a PC (probably running its own .NET program) through ethernet. I have the ENC28 ethernet module since it’s compatible with the Hydra, but I’m really getting overwhelmed with all the information everywhere.
Firstly, is it necessary to re-flash my new Hydra? I know a lot of people have for the sake of ethernet capabilities, but the contemporary product description indicates that it already has the firmware on the board.
Secondly, I’m having a hard time getting started with code. I copied the example code from here, (https://www.ghielectronics.com/community/codeshare/entry/606), and it gave me errors, mostly about syntax. I’m pretty inexperienced with networks, Gadgeteer, and Visual Studio, and having a hard time finding examples, I’d really appreciate any code samples, resources, or pointers you can offer my frustrated head.
Any words are appreciated.


#2

For the 4.2 SDK…

You will need to install the Ethernet version of the firmware. if you look on your PC, where the GHI SDK has been installed (C:\Program Files (x86)\GHI Electronics). Look in the OSHW folder for the Hydra firmware. You will find two versions.

I don’t work with OSHW stuff, so I am not the best one to help you initialize the network. But, if you are having syntax errors, telling us what they are might get you a faster answer.

BTW, if you install the Ethernet firmware, the ENC28 MUST be connected to the board, or the board will hang on boot.


#3

Thanks; I uploaded the new firmware, and all my old projects work when the enc28’s connected.
Right now[b] I’m just trying to get started with the code from the Networking Tutorial /b. I added a few libraries/references (can’t hurt, right?), but I’m getting two errors:
1) Cannot find a schema that defines target namespace ‘http://schemas.microsoft.com/dsltools/GadgeteerDSL’, schema validation skipped.

2) Missing partial modifier on declaration of type ‘GadgeteerApp1.Program’; another partial declaration of this type exists

I feel like these are linking syntax errors or something that should be easy to sort out, but I haven’t found any solution after a lot of searching. Any ideas?

Here’s the code:

using System;
using System.Collections;
using System.Threading;
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;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;

namespace GadgeteerApp1

{
   public class Program
   {
      //Set iface to the first available network interface
      static NetworkInterface iface = NetworkInterface.GetAllNetworkInterfaces()[0];
      static Socket server;

      public static void Main()
      {
         //Set iface to accept ip address automatically
         iface.EnableDhcp();

         //Wait for confirmation that ip address has been given
         while (iface.IPAddress == "0.0.0.0")
         {
            Debug.Print("Awaiting IP Address");
            Thread.Sleep(1000);
         }

         Debug.Print("IP Address Granted: " + iface.IPAddress);

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

         //Initialize a local endpoint to listen on the default http port 80
         IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 80);

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

         while (true)
         {
            // Wait for a client to connect.
            Socket clientSocket = server.Accept();

            // Process the client request.
            ProcessClientRequest(clientSocket);
         }
      }

      static void ProcessClientRequest(Socket m_clientSocket)
      {
         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</title></head>"
               + "<body><bold><a href=\"http://www.ghielectronics.com/\">Learn more about the .NET Micro Framework with FEZ by clicking here</a></bold></body></html>";
               byte[] buf = System.Text.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();
            }
         }
      }
   }
}

#4

If your project is a Gadgeteer application, which I assume it is, then your code is not organized correctly.

I suggest you read the getting started guide in “C:\Program Files (x86)\GHI Electronics\GHI .NET Gadgeteer SDK\Getting Started Guides”. It shows you how to create a Gadgeteer application.


#5

Thanks; I thought just copying the example would work. I’m still not at all great with Visual Studio, but I got this working once I learned that the main function’s already used (I stuffed everything in the Program Started method). It runs so far.
Being new to networking, I then connected the ENC28 to my laptop’s ethernet jack hoping something magical would happen (it didn’t; I just saw “Awaiting IP” in the debug window). I’m guessing I need to set up a network for the ENC28 to connect to, right? Meaning I need a router, right?
Thanks for your help.


#6

Thanks, but that sounds harder than my experience allows.
So to clarify, if I get a regular ol’ router at Radio Shack, I’ll be able to use that to send packets between the Hydra and a PC (with some coding of course?) Anyone know of any resources that I could use to make that happen?


#7

you’re on the internet, are you saying you don’t already have a network at home ?? You probably don’t need anything special here. Tell us what networking devices are at your place and we can help point out what you need.


#8

It’s for a project I’m working on at my internship; after talking it over, we’re using a switch with ethernet cables going to the hydra and the PC running a GUI application.
Since I’ve been having trouble with socket programming though, my adviser suggested I try to use a USB to talk between the Hydra and the PC’s GUI. According to this thread though (https://www.ghielectronics.com/community/forum/topic?id=6623), it seems like I’d need that usb-serial module Gus recommended.
Thoughts?
Thanks again!


#9

So depends on what you want to achieve. Serial is one method to communicate from PC to device. Ethernet is another. Serial is simple in comparison, but networking means you don’t need to be physically local to each other.

Personally, I love cheap stuff, and CP2102 based TTL USB devices are what I use for simple debug-level comms from Fez board to PC. I bought several on ebay for not much cash and just connect them up with the supplied jumper wires and G-Plugs from GHI. Perfect for “prototype” level connection, but if you want a polished solution you probably don’t want to only use them :slight_smile:

So if your switch isn’t connected to a router or network that allocates IP addresses (DHCP), then you need to manually set up IP addresses on your PC and Fez device that is on the same “network”. That’s usually pretty easy, but it’d be best if you told us everything about the situation (like is the PC also connected to the campus network on perhaps a different network, or is it dedicated to the switch and Fez connection). So possible, with some risks of the unknown. I’d say if serial is possible, go for it.


#10

Thanks! I was told serial wasn’t an option because we want to be able to connect it to any computer without having to configure/identify COM ports; it’ll just be plug&play.

We’re connecting the Hydra to the ENC28 to a network switch to a computer. The computer’s also connected to the campus network. I’m told we’re to use static IP’s on everything.

Still having a rough time with that tutorial (https://www.ghielectronics.com/docs/30/networking). For one thing I don’t know if I’m setting up the non-DHCP IP correctly, and then there’s the fact that I don’t get TCP/IP enough to understand what all the socket class methods are.
But does anyone know how to set up a static IP and maybe ping a switch to see if they’re even connected?
Thanks again!


#11

Thanks for all your help!
So now that the Hydra and computer connect to the same network (pinging works!), what would be the next step? Setting up, and reading/writing through sockets?

(something like " http://csharp.net-informations.com/communications/csharp-socket-programming.htm " )?


#12

The Hydra has to SEND the data of 2 analog inputs and 24 digital inputs. The Hydra has to RECEIVE 8 bits of data indicating which of the 8 digital outputs it should be setting high or low.


#13

Hm, not quite sure how that’ll work. We don’t want to have to connect to the internet for this or anything. It’d really be ideal to just be able to send and receive strings though the LAN.


#14

I thought that the http path would need a webserver; webserver implies internet, right?


#15

@ okinawanspud - A webserver is just an application that listens on a well known port (80) for request of a certain form (http). It can run locally and on a different port. You can run it on your Hydra and connect to it directly.