说明
获取可供读取的字节数(字符)。这是已经到达并存储在接收缓冲区中的数据。 available() 继承自 Stream 实用程序类。
用法
modem.available()
参数
空
返回
可读取的字节数
示例
/*
Lora Send And Receive
This sketch demonstrates how to send and receive data with the MKR WAN 1300/1310 LoRa module.
This example code is in the public domain.
*/
#include <MKRWAN.h>
LoRaModem modem;
// Uncomment if using the Murata chip as a module
// LoRaModem modem(Serial1);
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
String appEui = SECRET_APP_EUI;
String appKey = SECRET_APP_KEY;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
while (!Serial);
// change this to your regional band (eg. US915, AS923, ...)
if (!modem.begin(EU868)) {
Serial.println("Failed to start module");
while (1) {}
};
Serial.print("Your module version is: ");
Serial.println(modem.version());
Serial.print("Your device EUI is: ");
Serial.println(modem.deviceEUI());
int connected = modem.joinOTAA(appEui, appKey);
if (!connected) {
Serial.println("Something went wrong; are you indoor? Move near a window and retry");
while (1) {}
}
// Set poll interval to 60 secs.
modem.minPollInterval(60);
// NOTE: independent of this setting, the modem will
// not allow sending more than one message every 2 minutes,
// this is enforced by firmware and can not be changed.
}
void loop() {
Serial.println();
Serial.println("Enter a message to send to network");
Serial.println("(make sure that end-of-line 'NL' is enabled)");
while (!Serial.available());
String msg = Serial.readStringUntil('\n');
Serial.println();
Serial.print("Sending: " + msg + " - ");
for (unsigned int i = 0; i < msg.length(); i++) {
Serial.print(msg[i] >> 4, HEX);
Serial.print(msg[i] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
int err;
modem.beginPacket();
modem.print(msg);
err = modem.endPacket(true);
if (err > 0) {
Serial.println("Message sent correctly!");
} else {
Serial.println("Error sending message :(");
Serial.println("(you may send a limited amount of messages per minute, depending on the signal strength");
Serial.println("it may vary from 1 message every couple of seconds to 1 message every minute)");
}
delay(1000);
if (!modem.available()) {
Serial.println("No downlink message received at this time.");
return;
}
char rcv[64];
int i = 0;
while (modem.available()) {
rcv[i++] = (char)modem.read();
}
Serial.print("Received: ");
for (unsigned int j = 0; j < i; j++) {
Serial.print(rcv[j] >> 4, HEX);
Serial.print(rcv[j] & 0xF, HEX);
Serial.print(" ");
}
Serial.println();
}
相关用法
- Arduino MKRWAN - version()用法及代码示例
- Arduino MKRWAN - deviceEUI()用法及代码示例
- Arduino MKRWAN - endPacket()用法及代码示例
- Arduino MKRWAN - print()用法及代码示例
- Arduino MKRWAN - begin()用法及代码示例
- Arduino MKRWAN - joinABP()用法及代码示例
- Arduino MKRWAN - restart()用法及代码示例
- Arduino MKRWAN - joinOTAA()用法及代码示例
- Arduino MKRWAN - read()用法及代码示例
- Arduino MKRWAN - beginPacket()用法及代码示例
- Arduino MKRGSM - gprs.attachGPRS()用法及代码示例
- Arduino MKRGSM - sms.read()用法及代码示例
- Arduino MKRNB - getCurrentCarrier()用法及代码示例
- Arduino MKRNB - getIMEI()用法及代码示例
- Arduino MKRGSM - sms.print()用法及代码示例
- Arduino MKRNB - endSMS()用法及代码示例
- Arduino MKRGSM - client.connected()用法及代码示例
- Arduino MKRGSM - voice.answerCall()用法及代码示例
- Arduino MKRGSM - voice.hangCall()用法及代码示例
- Arduino MKRGSM - sms.write()用法及代码示例
- Arduino MKRNB - connected()用法及代码示例
- Arduino MKRGSM - gsm.shutdown()用法及代码示例
- Arduino MKRGSM - modem.getIMEI()用法及代码示例
- Arduino MKRGSM - sms.available()用法及代码示例
- Arduino MKRNB - shutdown()用法及代码示例
注:本文由纯净天空筛选整理自arduino.cc大神的英文原创作品 MKRWAN - available()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。