本文整理汇总了C++中EthernetUDP::readPacket方法的典型用法代码示例。如果您正苦于以下问题:C++ EthernetUDP::readPacket方法的具体用法?C++ EthernetUDP::readPacket怎么用?C++ EthernetUDP::readPacket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类EthernetUDP
的用法示例。
在下文中一共展示了EthernetUDP::readPacket方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getNtpTime
unsigned long getNtpTime()
{
// We need to call the begin to reset the socket.
// Because localClient.connect() may occupy the same socket.
EthernetUDP theUDP;
theUDP.begin(localPort);
Serial.println("Sending time sync request to NTP server.");
sendNTPpacket(timeServer); // send an NTP packet to a time server
unsigned long startMillis = millis();
int tryCounter = 1;
while( millis() - startMillis < 1000) // wait up to one second for the response
{ // wait to see if a reply is available
if ( theUDP.available() )
{
theUDP.readPacket(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer
//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
// now convert NTP time into Arduino Time format:
// Time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
Serial.println("Time sync successfully.");
return epoch;
}
Serial.print("No date data available. Try counter: .");
Serial.println(tryCounter++);
delay(100);
}
Serial.println("Time sync failed.");
return 0; // return 0 if unable to get the time
}