用户工具

站点工具


reference:library:ethernetudpwrite

Ethernet:UDP CLASS

UDP.write()

描述

向远程连接发送UDP数据。必须调用在beginPacke()和endPacket()函数之间。beginPacket()初始化数据包,当遇到endPacket()函数时,才发送UDP数据。

语法

UDP.write(message);

参数设置

message:发送的信息 (char)

返回

字节:返回发送的字符数。(该值无需被读)

例子

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
 
// 为你的控制器输入MAC地址和IP地址。
// IP地址将依赖于你的本地网络
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888;      //本地监听端口
 
// EthernetUDP的实例,让我们通过UDP发送和接收数据包
EthernetUDP Udp;
 
void setup() {
  //启动以太网和UDP:
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
}
 
 
 
void loop() {
  Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  Udp.write("hello");
  Udp.endPacket();
}

返回主菜单

reference/library/ethernetudpwrite.txt · 最后更改: 2023/06/07 04:24 由 127.0.0.1