我正在将Ardunio uno与HM-10模块一起使用。我试图扫描我周围的所有信标,然后存储信标名称
#include <SPI.h>
#include <SoftwareSerial.h>
String inputTXT;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
//setup
mySerial.write("AT");
delay(100);
mySerial.write("AT+ROLE1"); // Master mode
delay(100);
mySerial.write("AT+IMME1"); //wait for a connection command before connecting
delay(100);
mySerial.write("AT+RESET");
delay(50);
}
void loop() {
delay(3000);
mySerial.write("AT+DISI?");
if (mySerial.available()) {
inputTXT = mySerial.readString();
Serial.println(inputTXT);
inputTXT = "";
}
}
我在每个循环中重复输出
OK+DISISOK+DISC:00000000:00000000000000000000000000000000:00000OK+DISISOK+DISCEOK+DISC:00000000:00000000000000000000000000000000:0000000000:38F9D379C9E5:-065OK+DISC:00000000:00000000000000000000000000000000:0000000000:4CAA0DE091B7:-066OK+DISC:00000000:00000000000000000000000000000000:0000000000:72363EC2C661:-084OK+DISC:4C000C0E:008D37DBECB6B76115D006C9B3FA1005:1B1C1E7B5B:76854777DBD7:-072OK+DISC:00000000:00000000000000000000000000000000:0000000000:38F55DEDE396:-079OK+DISC:00000000:00000000000000000000000000000000:0000000000:4D023B8ED54D:-083OK+DISC:00000000:00000000000000000000000000000000:0000000000:6B5DB3EB2
所以我现在想在每次迭代的最后一个冒号之前保存12位字符串,例如“ 4CAA0DE091B7”,有人可以告诉我或建议我如何去做吗?
答案 0 :(得分:0)
不幸的是,Arduino没有正则表达式,因此您需要手动执行。
#include <SPI.h>
#include <SoftwareSerial.h>
String inputTXT;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
//setup
Serial.begin(9600);
mySerial.begin(9600);
mySerial.write("AT");
delay(100);
mySerial.write("AT+ROLE1"); // Master mode
delay(100);
mySerial.write("AT+IMME1"); //wait for a connection command before connecting
delay(100);
mySerial.write("AT+RESET");
delay(50);
}
void loop()
{
delay(3000);
mySerial.write("AT+DISI?");
if (mySerial.available())
{
inputTXT = mySerial.readString();
int pos = 0;
String result = "";
const String regx = "00000000:00000000000000000000000000000000:0000000000:";
const int regx_len = regx.length();
while ((pos = inputTXT.indexOf(regx, pos)) != -1)
{
// substring from <starting point> to >starting point + 12>
result = inputTXT.substring(pos + regx_len, pos + regx_len + 12);
Serial.println(result);
// move starter point to end of last result
pos = pos + regx_len + 12;
}
}
}