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.
