2015-02-28 2 views
0

У меня есть проблема с доступом к полученной строке udp, я могу получить ее по серийному номеру. Мне просто нужно, чтобы мои входящие UdP данных в переменный с помощью ethercard библиотеки в цикле() функцию, так что я могу использовать их в своем program.Here в коде я работаю с:Как получить arduino ethercard udp получить данные?

#include <EtherCard.h> 
#include <IPAddress.h> 

#define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below) 

#if STATIC 
// ethernet interface ip address 
static byte myip[] = { 192,168,1,200 }; 
// gateway ip address 
static byte gwip[] = { 192,168,1,1 }; 
#endif 

// ethernet mac address - must be unique on your network 
static byte mymac[] = { 0x70,0x69,0x69,0x2D,0x30,0x31 }; 
byte Ethernet::buffer[500]; // tcp/ip send and receive buffer 
//callback that prints received packets to the serial port 
void udpSerialPrint(word port, byte ip[4], const char *data, word len) { 
    Serial.println(data); 
} 

void setup(){ 
    Serial.begin(57600); 
    Serial.println("\n[backSoon]"); 

    if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0) 
    Serial.println("Failed to access Ethernet controller"); 
#if STATIC 
    ether.staticSetup(myip, gwip); 
#else 
    if (!ether.dhcpSetup()) 
    Serial.println("DHCP failed"); 
#endif 

    ether.printIp("IP: ", ether.myip); 
    ether.printIp("GW: ", ether.gwip); 
    ether.printIp("DNS: ", ether.dnsip); 

    //register udpSerialPrint() to port 1337 
    ether.udpServerListenOnPort(&udpSerialPrint, 1337); 

    //register udpSerialPrint() to port 42. 
    ether.udpServerListenOnPort(&udpSerialPrint, 42); 
} 

void loop(){ 
    //this must be called for ethercard functions to work. 
    ether.packetLoop(ether.packetReceive()); 

    //? incoming = data; <--- this is my problem 
    //Serial.println(incoming); 

} 

Это просто немного модифицированная версия примера UDPListener, которая поставляется с библиотекой ethercard. Thank you

ответ

0

Я все еще на крутой кривой обучения, но мне удалось получить UDP-разговор между блоками, поэтому надеюсь, что после этого поможет. Подозреваем, что самым быстрым способом было бы создание глобальной переменной, такой как:

char gUDPdata [30] = "";

затем в вашей обычной процедуре udpSerialPrint добавьте следующее для быстрого и грязного результата. Это копирует «данные» в глобальную переменную, которую вы можете видеть в своем основном цикле.

Serial.println(data); 
data[0] = 0; 
strcpy(data, gUDPdata); 

затем в главном цикле следующей должны производить так же, как Serial.print в процедуре udpSerialPrint.

Serial.println(gUDPdata); 
Смежные вопросы