EthernetUDP.parsePacket()

描述

檢查存在的 UDP 封包並且回傳封包大小。呼叫 Udp.read() 前必須先呼叫 parsePacket()

語法


Udp.parsePacket();

參數

無參數

回傳

int:UDP 封包的大小

範例

#include <Ethernet.h>
#include <EthernetUdp.h>

// 輸入本地端的 IP 位址
IPAddress ip(192, 168, 1, 177);

// 要監聽的本地端埠編號
unsigned int localPort = 8888;

// 創建一個 EthernetUDP 物件,我們可以藉由這個物件收發 UDP 的封包
EthernetUDP Udp;

void setup() {
  // 開始 Ethernet 和 EthernetUDP
  Ethernet.begin(NULL,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // 如果有接收到封包,讀取封包中的資料
  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
  }
  delay(10);
}

函式庫參考主頁面

The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.