2017-05-17 30 views
0

UDPを介してセンサーデータを送信するためのスケッチを書いています。私は1パッケージあたり1バイトの単純な転送を取得しています。 どうすればdefine私のpackagelenghtbufferが必要ですか?UDPパケットサイズを定義する(Arduino)

ありがとうございました!以下

コード:

//Version 1.05 

//necessary libraries 
#include <SPI.h> 
#include <Ethernet2.h> 
#include <EthernetUdp2.h> 

//Pin settings 
#define CTD 19 

//Network Settings 
byte mac[] = { 0x90, 0xA2, 0xDA, 0x10, 0xEC, 0xAB }; //set MAC Address Ethernet Shield (Backside) 
byte ip[] = { XXX, XXX, X, X };      //set IP-Address 
byte gateway[] = { XXX, XXX, X, X };     //set Gateway 
byte subnet[] = { 255, 255, 255, 0 };    //set Subnetmask 


//local UDP port to listen on 
unsigned int localPort = 5568; 

//Recipient IP 
IPAddress RecipientIP(XXX, XXX, X, X); 

//Recipient UDP port 
unsigned int RecipientPort = 8888; 

//Buffer for sending data 
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; 

//EthernetUDP instance 
EthernetUDP Udp; 

//CTD data 
int incomingData = 0; 


void setup() 
{ 
    //Start Ethernet 
    Ethernet.begin(mac, ip); 

    //Start UDP 
    Udp.begin(localPort); 

    //for debug only 
    Serial.begin(9600); 

    //Serial baud rate for CTD 
    Serial1.begin(1200); 

    //Version 1.05 
Serial.print("Version 1.05"); 

    //CTD 
    pinMode(CTD, INPUT); 
} 

void loop() 
{ 

//If CTD is sending 
if (Serial1.available()) 
{ 
    //read incoming data 
    incomingData = Serial1.read(); 

    //for debug only 
    Serial.print("Data: "); 
    Serial.println(incomingData, BIN); 
} 
//Send UDP packets 

    //Debug only 
    Serial.print("Packet"); 

    // send to the IP address and port 
    Udp.beginPacket(RecipientIP, RecipientPort); 
    Udp.write(incomingData); 
    Udp.endPacket(); 
} 

答えて

0

次のコードは、パケットを送受信するためのものです:

この中
#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[] = {   
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
unsigned int localPort = 5683;      // 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[] = "Portland banjo shabby chic Vice dreamcatcher gluten-free. Fashion axe Godard bicycle rights before they sold out, try-hard selvage polaroid sriracha master cleanse biodiesel Schlitz Wes Anderson.";       // 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); 
  Udp.begin(localPort); 

  Serial.begin(115200); 
} 

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 
  Udp.beginPacket("192.168.3.147", 5683); 
  Udp.write(ReplyBuffer); 
  Udp.endPacket(); 
   
  delay(1000); 
} 

次の文は、サイズを取得:

int packetSize = Udp.parsePacket(); 
+0

私はあなたができると思いますあなたの参照のためにそれを使用してください – Billa

+0

私はそれを試みたが、私はバッファを使用しているときに私はすべてのデータを取得しません。 parsePacketまたはpacketSizeにデータを取得するにはどうすればよいですか? – TheTris

関連する問題