reference:library:ethernetudpwrite
| |
— | reference:library:ethernetudpwrite [2023/06/07 04:24] (当前版本) – 创建 - 外部编辑 127.0.0.1 |
---|
| [[zh:reference:librariy:ethernet|Ethernet]]:UDP CLASS |
| ====== UDP.write() ====== |
| |
| ===== 描述 ===== |
| |
| 向远程连接发送UDP数据。必须调用在beginPacke()和endPacket()函数之间。beginPacket()初始化数据包,当遇到endPacket()函数时,才发送UDP数据。 |
| ===== 语法 ===== |
| |
| UDP.write(message); |
| ===== 参数设置 ===== |
| |
| message:发送的信息 (char) |
| ===== 返回 ===== |
| |
| 字节:返回发送的字符数。(该值无需被读) |
| ===== 例子 ===== |
| <code cpp> |
| #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(); |
| } |
| </code> |
| |
| [[zh:reference:librariy:main|返回主菜单]] |