2014-07-24 40 views
0

我在Windows 7上使用VS2012,它通過以太網連接到我的英特爾伽利略,其草圖使用Arduino 1.5.3上傳。我的最終目標是通過以太網電纜控制電機,但是我無法在兩個程序之間建立簡單的連接。 我沒有以太網或Udp或任何網絡的經驗,所以請詳細說明不合理的數額。試圖通過以太網電纜將英特爾伽利略連接到C#

這裏是我的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的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); 
} 

首先,我上傳的草圖到伽利略,然後打開串行,然後運行c#程序,但串行中沒有任何內容。

我一直在毆打我的頭靠在一堵牆上試圖弄清楚這幾天。我嘗試過不同的IP地址組合和其他一切,現在我正在尋找你的幫助。

謝謝,馬克

回答

1

我認爲你有Windows系統和你的伽利略之間的IP地址解析問題。 當我將伽利略直接插入Windows筆記本電腦時,我得到一個與未分配子網(如169.254.151.131)關聯的IP地址。

因此,如果我爲Galileo分配了一個IP地址,例如192.168.1.177,我的Windows系統將無法與其通話。在我的Galileo上,當我直接連接到筆記本電腦時詢問以太網DHCP地址時,我會得到一個IP地址,例如169.254.246.246。所以我的Windows系統可以和我的Galileo交談併發送一個UDP數據包給草圖。

我的建議是檢查你的Windows系統和你的伽利略的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: [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; 

*/ 

及以下是從我的串行端口輸出監控

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和伽利略說話,而不是C#。不應該是一個問題,但。我並不是很熟悉C#。

相關問題