极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 24820|回复: 12

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

[复制链接]
发表于 2015-3-4 17:36:56 | 显示全部楼层 |阅读模式
本帖最后由 连菜鸟都算不上 于 2015-3-5 11:29 编辑

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

使用道具 举报

 楼主| 发表于 2015-3-5 11:23:24 | 显示全部楼层
别沉,求解
回复 支持 反对

使用道具 举报

发表于 2015-3-5 13:09:54 | 显示全部楼层
  1. /*
  2.   SD card basic file example

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

  10. created   Nov 2010
  11. by David A. Mellis
  12. modified 9 Apr 2012
  13. by Tom Igoe

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

  18. File myFile;

  19. void setup()
  20. {
  21. // Open serial communications and wait for port to open:
  22.   Serial.begin(9600);
  23.    while (!Serial) {
  24.     ; // wait for serial port to connect. Needed for Leonardo only
  25.   }


  26.   Serial.print("Initializing SD card...");
  27.   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  28.   // Note that even if it's not used as the CS pin, the hardware SS pin
  29.   // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  30.   // or the SD library functions will not work.
  31.   pinMode(10, OUTPUT);

  32.   if (!SD.begin(4)) {
  33.     Serial.println("initialization failed!");
  34.     return;
  35.   }
  36.   Serial.println("initialization done.");

  37.   if (SD.exists("example.txt")) {
  38.     Serial.println("example.txt exists.");
  39.   }
  40.   else {
  41.     Serial.println("example.txt doesn't exist.");
  42.   }

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

  47.   // Check to see if the file exists:
  48.   if (SD.exists("example.txt")) {
  49.     Serial.println("example.txt exists.");
  50.   }
  51.   else {
  52.     Serial.println("example.txt doesn't exist.");  
  53.   }

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

  57.   if (SD.exists("example.txt")){
  58.     Serial.println("example.txt exists.");
  59.   }
  60.   else {
  61.     Serial.println("example.txt doesn't exist.");  
  62.   }
  63. }

  64. void loop()
  65. {
  66.   // nothing happens after setup finishes.
  67. }
复制代码
回复 支持 反对

使用道具 举报

发表于 2015-3-5 13:13:42 | 显示全部楼层
  1. /*
  2.   SD card read/write

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

  10. created   Nov 2010
  11. by David A. Mellis
  12. modified 9 Apr 2012
  13. by Tom Igoe

  14. This example code is in the public domain.
  15.          
  16. */

  17. #include <SD.h>

  18. File myFile;

  19. void setup()
  20. {
  21. // Open serial communications and wait for port to open:
  22.   Serial.begin(9600);
  23.    while (!Serial) {
  24.     ; // wait for serial port to connect. Needed for Leonardo only
  25.   }


  26.   Serial.print("Initializing SD card...");
  27.   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  28.   // Note that even if it's not used as the CS pin, the hardware SS pin
  29.   // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  30.   // or the SD library functions will not work.
  31.    pinMode(10, OUTPUT);
  32.    
  33.   if (!SD.begin(4)) {
  34.     Serial.println("initialization failed!");
  35.     return;
  36.   }
  37.   Serial.println("initialization done.");
  38.   
  39.   // open the file. note that only one file can be open at a time,
  40.   // so you have to close this one before opening another.
  41.   myFile = SD.open("test.txt", FILE_WRITE);
  42.   
  43.   // if the file opened okay, write to it:
  44.   if (myFile) {
  45.     Serial.print("Writing to test.txt...");
  46.     myFile.println("testing 1, 2, 3.");
  47.         // close the file:
  48.     myFile.close();
  49.     Serial.println("done.");
  50.   } else {
  51.     // if the file didn't open, print an error:
  52.     Serial.println("error opening test.txt");
  53.   }
  54.   
  55.   // re-open the file for reading:
  56.   myFile = SD.open("test.txt");
  57.   if (myFile) {
  58.     Serial.println("test.txt:");
  59.    
  60.     // read from the file until there's nothing else in it:
  61.     while (myFile.available()) {
  62.             Serial.write(myFile.read());
  63.     }
  64.     // close the file:
  65.     myFile.close();
  66.   } else {
  67.           // if the file didn't open, print an error:
  68.     Serial.println("error opening test.txt");
  69.   }
  70. }

  71. void loop()
  72. {
  73.         // nothing happens after setup
  74. }
复制代码
回复 支持 反对

使用道具 举报

发表于 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
哪里遇到问题嘛? 程序贴出来看下?

大概流程是这样子的:

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

使用道具 举报

 楼主| 发表于 2015-3-7 00:53:23 | 显示全部楼层
anonymouscc 发表于 2015-3-5 13:13

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

使用道具 举报

发表于 2015-7-10 14:08:01 | 显示全部楼层
连菜鸟都算不上 发表于 2015-3-7 00:53
这些demo我看过了,实际功能不是他那样的,我想逐行读取数据,不是一次性读完。
谢谢你的帮助

是否解决了呢,楼主,贴出来看看,遇到同样问题
回复 支持 反对

使用道具 举报

发表于 2016-7-2 16:08:39 | 显示全部楼层
本帖最后由 pybok 于 2016-7-2 16:13 编辑

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

语法

file.read()

参数

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

返回

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

回复 支持 反对

使用道具 举报

发表于 2016-7-2 16:09:55 | 显示全部楼层
file.seek(position) 将“光标”移到某一位置
回复 支持 反对

使用道具 举报

发表于 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个字节的数据,反复循环定位--读取--定位--读取。。。。。。。。。。。。。。
即可!!
回复 支持 反对

使用道具 举报

发表于 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資料,逐字再靠藍芽傳出
回复 支持 反对

使用道具 举报

发表于 2017-7-26 12:06:12 | 显示全部楼层
有沒有辦法,讓arduino板判斷txt裡的內容後,去執行相對應的動作?
例如:TXT讀去英文字母"A",執行A東動作;內文是"B"的話,執行B動作
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-20 09:20 , Processed in 0.044809 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表