说明
获取可用于从缓冲区读取的字节数(字符)。这是已经到达的数据。
该函数只有在UDP.parsePacket()之后才能成功调用。
available() 继承自 Stream 实用程序类。
用法
EthernetUDP.available()
参数
None
返回
- 可读取的字节数
示例
#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(Udp.available())
{
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.read()用法及代码示例
- 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.available()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。