如何从SD卡中的数字? [英] How to get numbers from sd card?
本文介绍了如何从SD卡中的数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不擅长编码。我有一个SD卡称为Test.cal文件包含6个号码。 20,21,22,23,24,25
但我不知道如何让这第六。有人能帮助我吗?下面是我自己对编码的想法。
的#include< SD.h>浮meterTemp [6];
串theLines [15];
myfile文件;无效设置()
{
//打开串口通信和等待端口打开:
Serial.begin(9600);
而(!串行){
; //等待串口连接。需要莱昂纳多只
}
Serial.print(初始化SD卡...);
//在以太网盾,CS引脚4,它设置为默认的输出。
//注意,即使它不是作为CS引脚,硬件SS引脚
//(10月最Arduino的板,53兆),必须保留作为输出
//或者SD库函数将无法正常工作。
pinMode(53,OUTPUT); 如果(!SD.begin(4)){
Serial.println(初始化失败!);
返回;
}
Serial.println(初始化完成。); //重新打开文件阅读:
MYFILE = SD.open(Test.cal);
如果(MYFILE){
//我希望把文件编号为阵,meterTemp [6],不inclduing逗号,所以当我拿到meterTemp [0] = - 。32,依此类推。
//从文件中读取,直到没有什么别的它:
而(myFile.available()){
Serial.write(myFile.read()); }
//关闭文件:
myFile.close();
}其他{
//如果文件没有打开,打印错误:
Serial.println(错误打开的test.txt);
}
}无效循环()
{
//设置后没有任何反应
}
解决方案
的#include< SD.h>浮meterTemp [6];
串theLines;
myfile文件;
字符串文件名=Test.cal;
INT行= 0;无效设置()
{
//打开串口通信和等待端口打开:
Serial.begin(9600);
而(!串行){
; //等待串口连接。需要莱昂纳多只
}
Serial.print(初始化SD卡...);
//在以太网盾,CS引脚4,它设置为默认的输出。
//注意,即使它不是作为CS引脚,硬件SS引脚
//(10月最Arduino的板,53兆),必须保留作为输出
//或者SD库函数将无法正常工作。
pinMode(53,OUTPUT); 如果(!SD.begin(4)){
Serial.println(初始化失败!);
返回;
}
Serial.println(初始化完成。); //重新打开文件阅读:
MYFILE = SD.open(Test.cal);
如果(MYFILE){
Serial.println(test.txt的:);
;
//从文件中读取,直到没有什么别的它:
而(myFile.available()){
Serial.write(myFile.read());
theLines = ReadFile的(文件名,行);
Serial.println(theLines);
}
//关闭文件:
myFile.close();
}其他{
//如果文件没有打开,打印错误:
Serial.println(错误打开的test.txt);
}
}无效循环()
{
//设置后没有任何反应
}字符串READFILE(字符串的文件名,INT线)
{
个char [20];
字符文件名[的sizeof(文件名)];
filename.toCharArray(文件名,sizeof的(文件名));myfile文件= SD.open(文件名,FILE_READ);
如果(MYFILE)
{
串recived =;
INT CUR = 0;
焦炭CH;
而(myFile.available())
{
CH = myFile.read();
如果(CH ==',')
{
CUR ++;
如果(CUR ==线)
{
myFile.close();
返回字符串(recived);
}
}
其他
{
recived + = CH;
}
}
}
返回;
}
I am bad at coding. I have a file called Test.cal in sd card contained 6 numbers. 20,21,22,23,24,25 But I do not know how to get this six number. Can someone help me ? Below is my own thought about the coding.
#include <SD.h>
float meterTemp[6];
String theLines[15];
File myFile;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("Test.cal");
if (myFile) {
//I want to put the file numbers into array, meterTemp[6], not inclduing comma, so when I get meterTemp[0]=-32, and so on.
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
解决方案
#include <SD.h>
float meterTemp[6];
String theLines;
File myFile;
String filename="Test.cal";
int line=0;
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// On the Ethernet Shield, CS is pin 4. It's set as an output by default.
// Note that even if it's not used as the CS pin, the hardware SS pin
// (10 on most Arduino boards, 53 on the Mega) must be left as an output
// or the SD library functions will not work.
pinMode(53, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
// re-open the file for reading:
myFile = SD.open("Test.cal");
if (myFile) {
Serial.println("test.txt:");
;
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
theLines=readFile(filename,line);
Serial.println(theLines);
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop()
{
// nothing happens after setup
}
String readFile(String filename, int line)
{
char s[20];
char FileName[sizeof(filename)];
filename.toCharArray(FileName, sizeof(FileName));
File myFile = SD.open(FileName,FILE_READ);
if(myFile)
{
String recived = "";
int cur = 0;
char ch;
while (myFile.available())
{
ch = myFile.read();
if(ch == ',')
{
cur++;
if(cur == line)
{
myFile.close();
return String(recived);
}
}
else
{
recived += ch;
}
}
}
return "";
}
这篇关于如何从SD卡中的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文