在 Arduino 中逐行阅读 IDE
Reading line by line in Arduino IDE
我正在尝试逐行读取 txt 文件(具有数值)。
我用过SPIFFS,我用过这个函数
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("− failed to open file for reading");
return;
}
int count = 0;
Serial.println(" read from file:");
while(file.available()){
if (count < 100)
Serial.write(file.read());
}
}
“file.read()”的替代函数是什么,比如“readline”,因为我需要从第一行读取文件到 100,从 101 到 200 等等。
文件是 Stream and has all of the methods that a Stream does, including readBytesUntil and readStringUntil.
从串行、网络客户端、文件或其他实现 Arduino 的对象读取行 you can use function readBytesUntil。
uint8_t lineBuffer[64];
while (file.available()) {
int length = file.readBytesUntil('\n', lineBuffer, sizeof(lineBuffer) - 1);
if (length > 0 && lineBuffer[length - 1] == '\r') {
length--; // to remove \r if it is there
}
lineBuffer[length] = 0; // terminate the string
Serial.println(lineBuffer);
}
您需要使用readStringUntil
。虽然这不是最有效的方法,但我会告诉你它是如何完成的。
#include <vector>
#include <SPIFFS.h>
std::vector<double> readLine(String path, uint16_t from, uint16_t to) {
std::vector<double> list;
if(from > to) return list;
File file = SPIFFS.open(path.c_str());
if(!file) return list;
uint16_t counter = 0;
while(file.available() && counter <= to) {
counter++;
if(counter < from) {
file.readStringUntil('\n');
} else {
String data = file.readStringUntil('\n');
list.push_back(data.toDouble());
}
}
return list;
}
void setup() {
Serial.begin(115200);
SPIFFS.begin(true);
std::vector<double> numbers = readLine("file.txt", 0, 100);
Serial.println("Data from 0 to 100:");
uint16_t counter = 0;
for (auto& n : numbers) {
counter++;
Serial.print(String(n) + "\t");
if(counter % 10 == 0) {
Serial.println();
}
}
numbers = readLine("file.txt", 101, 200);
Serial.println("\nData from 101 to 200:");
counter = 0;
for (auto& n : numbers) {
counter++;
Serial.print(String(n) + "\t");
if(counter % 10 == 0) {
Serial.println();
}
}
}
更新
假设你有 1050 个值,你想为每 100 个值解析它。
int i = 0;
while(i < 1050) {
int start = i + 1;
int end = (i + 100) > 1050 ? 1050 : i + 100;
std::vector<double> numbers = readLine("file.txt", start, end);
Serial.println("Data from " + String(start) + " to " + String(end) + ":");
uint16_t counter = 0;
for (auto& n : numbers) {
counter++;
Serial.print(String(n) + "\t");
if(counter % 10 == 0) {
Serial.println();
}
}
i += 100;
}
我正在尝试逐行读取 txt 文件(具有数值)。 我用过SPIFFS,我用过这个函数
void readFile(fs::FS &fs, const char * path){
Serial.printf("Reading file: %s\r\n", path);
File file = fs.open(path);
if(!file || file.isDirectory()){
Serial.println("− failed to open file for reading");
return;
}
int count = 0;
Serial.println(" read from file:");
while(file.available()){
if (count < 100)
Serial.write(file.read());
}
}
“file.read()”的替代函数是什么,比如“readline”,因为我需要从第一行读取文件到 100,从 101 到 200 等等。
文件是 Stream and has all of the methods that a Stream does, including readBytesUntil and readStringUntil.
从串行、网络客户端、文件或其他实现 Arduino 的对象读取行
uint8_t lineBuffer[64];
while (file.available()) {
int length = file.readBytesUntil('\n', lineBuffer, sizeof(lineBuffer) - 1);
if (length > 0 && lineBuffer[length - 1] == '\r') {
length--; // to remove \r if it is there
}
lineBuffer[length] = 0; // terminate the string
Serial.println(lineBuffer);
}
您需要使用readStringUntil
。虽然这不是最有效的方法,但我会告诉你它是如何完成的。
#include <vector>
#include <SPIFFS.h>
std::vector<double> readLine(String path, uint16_t from, uint16_t to) {
std::vector<double> list;
if(from > to) return list;
File file = SPIFFS.open(path.c_str());
if(!file) return list;
uint16_t counter = 0;
while(file.available() && counter <= to) {
counter++;
if(counter < from) {
file.readStringUntil('\n');
} else {
String data = file.readStringUntil('\n');
list.push_back(data.toDouble());
}
}
return list;
}
void setup() {
Serial.begin(115200);
SPIFFS.begin(true);
std::vector<double> numbers = readLine("file.txt", 0, 100);
Serial.println("Data from 0 to 100:");
uint16_t counter = 0;
for (auto& n : numbers) {
counter++;
Serial.print(String(n) + "\t");
if(counter % 10 == 0) {
Serial.println();
}
}
numbers = readLine("file.txt", 101, 200);
Serial.println("\nData from 101 to 200:");
counter = 0;
for (auto& n : numbers) {
counter++;
Serial.print(String(n) + "\t");
if(counter % 10 == 0) {
Serial.println();
}
}
}
更新
假设你有 1050 个值,你想为每 100 个值解析它。
int i = 0;
while(i < 1050) {
int start = i + 1;
int end = (i + 100) > 1050 ? 1050 : i + 100;
std::vector<double> numbers = readLine("file.txt", start, end);
Serial.println("Data from " + String(start) + " to " + String(end) + ":");
uint16_t counter = 0;
for (auto& n : numbers) {
counter++;
Serial.print(String(n) + "\t");
if(counter % 10 == 0) {
Serial.println();
}
}
i += 100;
}