EthernetUDP.write()
描述
把资料写进要透过 UDP 传送的封包。write() 必须在 beginPacket() 和 endPacket() 之间呼叫,beginPacket() 会初始化 UDP 封包,初始化后我们可以透过 write() 把资料写入封包中,要传送的资料都写入封包后呼叫 endPacket() 把封包传送出去。使用 86Duino 时封包最大可达 512 位元组,如果使用的是 arduino 封包最大可达 24 位元组。
语法
Udp.write(message);
Udp.write(buffer, size);
参数
message:要写入封包的资料,型态是 char 或 string
buffer:存有要写入封包资讯的阵列,型态是 byte 或 char
size:buffer 的大小
回传
byte:回传写入封包的位元组数量
范例
#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);
}
void loop() {
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write("hello");
Udp.endPacket();
}
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.
