0

我在 Windows 7 上使用 VS2012,它通过以太网连接到我的 Intel Galileo,它的草图是用 Arduino 1.5.3 上传的。我的最终目标是通过以太网电缆控制电机,但我无法在两个程序之间建立简单的连接。我没有使用以太网或 Udp 或任何网络的经验,所以请详细说明不合理的数量。

这是我在 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);

    }
}
}

这是我在 Arduino 上的代码,取自 arduino 网站:

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 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);
}

首先,我将草图上传到 Galileo,然后打开 Serial,然后运行 ​​c# 程序,但 Serial 中没有出现任何内容。

几天来,我一直在努力解决这个问题。我已经尝试过不同的 IP 地址组合和其他所有内容,现在我正在寻求您的帮助。

谢谢,马克

4

1 回答 1

1

我认为您在 Windows 系统和 Galileo 之间的 IP 地址解析存在问题。当我将 Galileo 直接插入我的 Windows 笔记本电脑时,我得到一个与未分配子网相关联的 IP 地址,例如 169.254.151.131。

因此,如果我为我的 Galileo 分配了一个 IP 地址,例如 192.168.1.177,我的 Windows 系统将无法与其通信。在我的 Galileo 上,当我在直接连接到笔记本电脑时询问以太网 DHCP 地址时,我会得到一个 IP 地址,例如 169.254.246.246。所以我的 Windows 系统可以与我的 Galileo 通信并向草图发送一个 UDP 数据包。

我的建议是检查您的 Windows 系统和 Galileo 的 IP 地址,以确保它们可以相互连接。

包括我用来与伽利略交谈的草图。

/*
  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: bjoern@cs.stanford.edu 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;

*/

下面是我的串口监视器的输出

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!

请注意,远程系统的 IP 地址显示为 255.255.255.255,端口 0,因此草图将无法发回 UDP 回复数据包。我还没有弄清楚这部分。检查https://communities.intel.com/community/makers/content以查看他们是否有解决方法。

另请注意,我使用 perl 与 Galileo 对话,而不是 C#。不过应该不是问题。我对 C# 不是很熟悉。

于 2014-07-28T23:19:53.843 回答