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.
