连菜鸟都算不上 发表于 2015-3-4 17:36:56

程序求解!逐行读取SD卡中的TXT文件

本帖最后由 连菜鸟都算不上 于 2015-3-5 11:29 编辑

sd模块和Mege连线都没问题,就是如题的程序遇到问题了,求救。{:soso_e183:}

连菜鸟都算不上 发表于 2015-3-5 11:23:24

别沉,求解:'(

anonymouscc 发表于 2015-3-5 13:09:54

/*
SD card basic file example

This example shows how to create and destroy an SD card file        
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.
       
*/
#include <SD.h>

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(10, OUTPUT);

if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
}
Serial.println("initialization done.");

if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
}
else {
    Serial.println("example.txt doesn't exist.");
}

// open a new file and immediately close it:
Serial.println("Creating example.txt...");
myFile = SD.open("example.txt", FILE_WRITE);
myFile.close();

// Check to see if the file exists:
if (SD.exists("example.txt")) {
    Serial.println("example.txt exists.");
}
else {
    Serial.println("example.txt doesn't exist.");
}

// delete the file:
Serial.println("Removing example.txt...");
SD.remove("example.txt");

if (SD.exists("example.txt")){
    Serial.println("example.txt exists.");
}
else {
    Serial.println("example.txt doesn't exist.");
}
}

void loop()
{
// nothing happens after setup finishes.
}

anonymouscc 发表于 2015-3-5 13:13:42

/*
SD card read/write

This example shows how to read and write data to and from an SD card file        
The circuit:
* SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4

created   Nov 2010
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe

This example code is in the public domain.
       
*/

#include <SD.h>

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(10, OUTPUT);
   
if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
}
Serial.println("initialization done.");

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);

// if the file opened okay, write to it:
if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
        // close the file:
    myFile.close();
    Serial.println("done.");
} else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
}

// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
    Serial.println("test.txt:");
   
    // 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
}

yuanhaoliang 发表于 2015-3-6 11:34:47

哪里遇到问题嘛? 程序贴出来看下?

大概流程是这样子的:
1. 打开这个文件
2. seek(0); 文件定位到开头
3. read()读取,有一些库还会有readStringUntil()函数的,这个更方便
4. 遇到'\r'或'\n'或'\r\n'就是换行符,那前面读的就是一行的内容
5. 跳过换行符,继续读下一行.
6.直到文件结束.

连菜鸟都算不上 发表于 2015-3-7 00:51:48

yuanhaoliang 发表于 2015-3-6 11:34 static/image/common/back.gif
哪里遇到问题嘛? 程序贴出来看下?

大概流程是这样子的:


就是程序不知道怎么写,看了你的有些启发。还有疑惑如下::$
1.用什么程序能一个一个字节读取,读取的类型是字符还是数字或是其他类型?
2.读完一行后,这行数据会被调用,下次怎么继续读下一行,而不是从头开始?
谢谢你的帮助!

连菜鸟都算不上 发表于 2015-3-7 00:53:23

anonymouscc 发表于 2015-3-5 13:13 static/image/common/back.gif


这些demo我看过了,实际功能不是他那样的,我想逐行读取数据,不是一次性读完。
谢谢你的帮助:hug:

骷髅 发表于 2015-7-10 14:08:01

连菜鸟都算不上 发表于 2015-3-7 00:53 static/image/common/back.gif
这些demo我看过了,实际功能不是他那样的,我想逐行读取数据,不是一次性读完。
谢谢你的帮助

是否解决了呢,楼主,贴出来看看,遇到同样问题

pybok 发表于 2016-7-2 16:08:39

本帖最后由 pybok 于 2016-7-2 16:13 编辑

read()
读取文件的一个字节(byte)。

语法

file.read()

参数

file:File类的object(由SD.open()返回)

返回

下一个字节(或字符),如无下一字节(或字符)则返回 -1。

pybok 发表于 2016-7-2 16:09:55

file.seek(position) 将“光标”移到某一位置

pybok 发表于 2016-7-2 16:11:51

本帖最后由 pybok 于 2016-7-2 16:14 编辑

peek()
从文件读取一个字节(byte),但并不将索引位前进。也就是说,连续调用peek()将返回相同的值,再调用read()也将返回这一值。

语法

file.peek()

参数

file:File类的object(由SD.open()返回)

返回

下一个字节(或字符),如无下一个字节(或字符)则返回 -1。

---------------------------------------------------------------------------------------

先用seek()在文件中定位,然后用feek()读1个字节的数据,反复循环定位--读取--定位--读取。。。。。。。。。。。。。。
即可!!

linyballen 发表于 2016-7-22 15:53:43

if(BT.read()=='A')
{
myFile = SD.open("test.txt");
while (myFile.available())
    {
      BT.write(myFile.read());
    }
}
   else
   {} */
看一下這段是否用得上,讀取SD卡test.txt資料,逐字再靠藍芽傳出

a509111wu 发表于 2017-7-26 12:06:12

有沒有辦法,讓arduino板判斷txt裡的內容後,去執行相對應的動作?
例如:TXT讀去英文字母"A",執行A東動作;內文是"B"的話,執行B動作
页: [1]
查看完整版本: 程序求解!逐行读取SD卡中的TXT文件