Trying to connect Intel Galileo to C# through ethernet cable

2.1k Views Asked by At

I'm using VS2012 on Windows 7 which is connected by ethernet to my Intel Galileo, whose sketches are uploaded with Arduino 1.5.3. My end goal is to control a motor through a ethernet cable, but I can't establish a simple connecting between the two programs. I have no experience with ethernets or Udp's or any networking for that matter, so please elaborate to irrational amounts.

Here is my code on c#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace TemperatureArduinoReader
{
static class Program
{

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]

    static void Main()
    {
        byte[] packetData = System.Text.ASCIIEncoding.ASCII.GetBytes("hello World");


        string IP = "192.168.1.177";
            //"127.0.0.1";
        int port = 8888;


        IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port);
        Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);


        client.SendTo(packetData, ep);

    }
}
}

Here is my code on Arduino, taken from the arduino webstite:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: [email protected] 12/30/2008


byte mac[] = { 0X98 , 0x4F, 0xEE, 0x01, 0x54, 0xB3};
IPAddress ip(192,168,1,177);
unsigned int localPort = 8888;      




char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;


void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);


Serial.begin(9600);
}


void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
  Serial.print(remote[i], DEC);
  if (i < 3)
  {
    Serial.print(".");
  }
}
Serial.print(", port ");
Serial.println(Udp.remotePort());


// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);


// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}

First, I upload the sketch to the Galileo, then open the Serial, then run the c# program, but nothing appears in the Serial.

I've been beating my head against a wall trying to figure this out for a couple days. I've tried different combinations of IP addresses and everything else, now I'm looking for your help.

Thanks, Mark

1

There are 1 best solutions below

0
On

I think you have a problem with IP address resolution between the Windows system and your Galileo. When I plugged a Galileo directly to my Windows laptop, I get an IP address associated with an unassigned subnet such as 169.254.151.131.

So, if I assigned an IP address such as 192.168.1.177 to my Galileo, my Windows system won't be able to talk to it. On my Galileo, when I asked for an Ethernet DHCP address while connected directly to my laptop, I'll get an IP address such as 169.254.246.246. So my Windows system can talk to my Galileo and send a UDP packet to the sketch.

My suggestion is to check the IP address of both your Windows system and your Galileo to make sure they can connect to each other.

Included is the sketch that I used to talk to the Galileo.

/*
  UDPSendReceive

 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender

 A Processing sketch is included at the end of file that can be used to send 
 and received messages for testing with a computer.

 created 21 Aug 2010
 by Michael Margolis

 This code is in the public domain.

 */


#include <SPI.h>          // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>  // UDP library from: [email protected] 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x98, 0x4f, 0xeE, 0x00, 0x23, 0xb9 };
unsigned int localPort = 8888;              // local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  Serial.begin(9600);
  delay(5000);
  Serial.println("Ready");
  // get an IP address from DHCP server, if there isn't a DHCP server on the network
  // an address such as 169.254.246.246 will be assigned
  if (Ethernet.begin(mac) == 1) {
    Serial.println("Ethernet.begin() succeeded!");
    Serial.print("IP:      "); 
    Serial.println(Ethernet.localIP());
    Serial.print("Subnet:  "); 
    Serial.println(Ethernet.subnetMask());
    Serial.print("Gateway: "); 
    Serial.println(Ethernet.gatewayIP());
    Serial.print("DNS:     "); 
    Serial.println(Ethernet.dnsServerIP());
  } else {
    Serial.println("Failed to initialize Ethernet");
    while(1);
  }
  Udp.begin(localPort);
  Serial.println("Listening to UDP port 8888");

}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  }
  delay(10);
}

/*
  A sample perl script to send a UDP message to above Galileo sketch
  #!/usr/bin/perl -w
  use strict;
  use IO::Socket::INET;
  # change PeerAddr to match what the Galileo sketch reports back
  my $sendSocket = new IO::Socket::INET(PeerAddr=>'169.254.246.246', 
                        PeerPort=>8888, 
                        Proto => 'udp', 
                        Timeout=>1) or die('Error creating UDP socket');
  my $data = "Hello Galileo!";
  print $sendSocket $data;

*/

And below is the output from my serial port monitor

Ready
Ethernet.begin() succeeded!
IP:      169.254.246.246
Subnet:  255.255.  0.  0
Gateway: 255.255.255.255
DNS:     255.255.255.255
Listening to UDP port 8888
Received packet of size 14
From 255.255.255.255, port 0
Contents:
Hello Galileo!

Note that the IP address of the remote system is shown as 255.255.255.255, port 0, so the sketch will not be able to send back a UDP reply packet. I haven't figured this part out yet. Check with https://communities.intel.com/community/makers/content to see if they have a workaround.

Also note that I used perl to talk to the Galileo, not C#. Should not be an issue though. I'm not all that familiar with C#.