|
本帖最后由 topdog 于 2022-5-18 23:19 编辑
用软串口先read一下所有的数据,再去截取。b'[[10, 12], [23, 33], [43, 15], [36, 72]]' 以此为例:10就是截取第4,5个字节。
1,截取各段数字:
[pre]String inputString = "b'[[10,12],[23,33],[43,15],[36,72]]'";
void setup()
{
Serial.begin(9600);
int one = inputString.substring(4, 6).toInt();
int two = inputString.substring(7, 9).toInt();
int three = inputString.substring(12, 14).toInt();
int four = inputString.substring(15, 17).toInt();
int five = inputString.substring(20, 22).toInt();
int six = inputString.substring(23, 25).toInt();
int seven = inputString.substring(28, 30).toInt();
int eight = inputString.substring(31, 33).toInt();
Serial.print("one \t = \t ");
Serial.println(one);
Serial.print("two \t = \t ");
Serial.println(two);
Serial.print("three \t = \t ");
Serial.println(three);
Serial.print("four \t = \t ");
Serial.println(four);
Serial.print("five \t = \t ");
Serial.println(five);
Serial.print("six \t = \t ");
Serial.println(six);
Serial.print("seven \t = \t ");
Serial.println(seven);
Serial.print("eight \t = \t ");
Serial.println(eight);
}
void loop() {}[/pre]
2实现结果
3,软串口接python的程序:
[pre]#include <SoftwareSerial.h>;
char terminatorChar = "'";
SoftwareSerial PY_Serial(2, 3);
void setup()
{
Serial.begin(115200);
PY_Serial.begin(115200);
}
void loop() {
String inputString = "";
if (PY_Serial.available() > 0) {
inputString = PY_Serial.readStringUntil(terminatorChar);
Serial.println(inputString);
delay(100);
int one = inputString.substring(4, 6).toInt();
int two = inputString.substring(7, 9).toInt();
int three = inputString.substring(12, 14).toInt();
int four = inputString.substring(15, 17).toInt();
int five = inputString.substring(20, 22).toInt();
int six = inputString.substring(23, 25).toInt();
int seven = inputString.substring(28, 30).toInt();
int eight = inputString.substring(31, 33).toInt();
Serial.print("one \t = \t ");
Serial.println(one);
Serial.print("two \t = \t ");
Serial.println(two);
Serial.print("three \t = \t ");
Serial.println(three);
Serial.print("four \t = \t ");
Serial.println(four);
Serial.print("five \t = \t ");
Serial.println(five);
Serial.print("six \t = \t ");
Serial.println(six);
Serial.print("seven \t = \t ");
Serial.println(seven);
Serial.print("eight \t = \t ");
Serial.println(eight);
}
}[/pre]
3,硬体串口输入程序:
[pre]char terminatorChar = "'";
void setup() {
// 启动串口通讯
Serial.begin(115200);
Serial.println();
}
void loop() {
if (Serial.available()) {
Serial.println("Please enter sting:");
String inputString = Serial.readStringUntil(terminatorChar);
Serial.print("Received Serial sting: ");
Serial.println(inputString);
int one = inputString.substring(4, 6).toInt();
int two = inputString.substring(7, 9).toInt();
int three = inputString.substring(12, 14).toInt();
int four = inputString.substring(15, 17).toInt();
int five = inputString.substring(20, 22).toInt();
int six = inputString.substring(23, 25).toInt();
int seven = inputString.substring(28, 30).toInt();
int eight = inputString.substring(31, 33).toInt();
Serial.print("one \t = \t ");
Serial.println(one);
Serial.print("two \t = \t ");
Serial.println(two);
Serial.print("three \t = \t ");
Serial.println(three);
Serial.print("four \t = \t ");
Serial.println(four);
Serial.print("five \t = \t ");
Serial.println(five);
Serial.print("six \t = \t ");
Serial.println(six);
Serial.print("seven \t = \t ");
Serial.println(seven);
Serial.print("eight \t = \t ");
Serial.println(eight);
}
}[/pre]
4,实际测试结果:
可以参考:
https://www.arduino.cn/thread-104826-1-1.html
https://www.arduino.cn/thread-102084-1-1.html
https://www.arduino.cn/thread-102053-1-1.html
|
|