说明
从指定缓冲区读取 UDP 数据。如果没有给出参数,它将返回缓冲区中的下一个字符。
该函数只有在UDP.parsePacket()之后才能成功调用。
用法
EthernetUDP.read();
EthernetUDP.read(packetBuffer, MaxSize);
参数
- packetBuffer:保存传入数据包的缓冲区(char)
- MaxSize:缓冲区的最大大小(int)
返回
- char:返回缓冲区中的字符
示例
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 177);
unsigned int localPort = 8888; // local port to listen on
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
}
void loop() {
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBuffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
}
}
相关用法
- Arduino Ethernet - EthernetUDP.parsePacket()用法及代码示例
- Arduino Ethernet - EthernetUDP.beginPacket()用法及代码示例
- Arduino Ethernet - EthernetUDP.available()用法及代码示例
- Arduino Ethernet - EthernetUDP.endPacket()用法及代码示例
- Arduino Ethernet - EthernetUDP.begin()用法及代码示例
- Arduino Ethernet - EthernetUDP.write()用法及代码示例
- Arduino Ethernet - Ethernet.setRetransmissionTimeout()用法及代码示例
- Arduino Ethernet - Ethernet.MACAddress()用法及代码示例
- Arduino Ethernet - Ethernet.hardwareStatus()用法及代码示例
- Arduino Ethernet - Ethernet.localIP()用法及代码示例
- Arduino Ethernet - Ethernet.setDnsServerIP()用法及代码示例
- Arduino Ethernet - Ethernet.init()用法及代码示例
- Arduino Ethernet - Ethernet.setMACAddress()用法及代码示例
- Arduino Ethernet - EthernetServer()用法及代码示例
- Arduino Ethernet - Ethernet.setSubnetMask()用法及代码示例
- Arduino Ethernet - Ethernet.dnsServerIP()用法及代码示例
- Arduino Ethernet - Ethernet.setLocalIP()用法及代码示例
- Arduino Ethernet - Ethernet.gatewayIP()用法及代码示例
- Arduino Ethernet - Ethernet.setGatewayIP()用法及代码示例
- Arduino Ethernet - EthernetClient()用法及代码示例
- Arduino Ethernet - Ethernet.setRetransmissionCount()用法及代码示例
- Arduino Ethernet - Ethernet.linkStatus()用法及代码示例
- Arduino Ethernet - Ethernet.begin()用法及代码示例
- Arduino Ethernet - Ethernet.subnetMask()用法及代码示例
- Arduino Ethernet - server.begin()用法及代码示例
注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 Ethernet - EthernetUDP.read()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。