Go Down

Topic: Issue opening files for write (Read 1 time) previous topic - next topic

msavazzi

Team, I really need help as this is driving me nuts and do not know where is the issue.
I'm building an application with Arduino, I have 2 files
The first one is the configuration file I should read and update when config changes
The second one is a swap file to handle text I receive from a modem.

The program does this:
1) open config file and read
2) wait for modem to start
3) gather the list of messages and save them in the swap file
4) ....

Both files are opened and closed every time.
The swap file is deleted if exists before being opened

Now: 1) always work, file is read correctly
3) always fails!

I tried to change SD card, to change library from SD to SDFat, to change code and order, to use 2 different file handles to avoid conflicts...

but no way!
Once every 100000000 times 3 works :( and saves the file (thus it makes me think is not an issue on the code)

Can you help me?

Note to leave more power to Arduino Serial is at 4800 and AltSerial is at 2400 (so they should not impact all the rest)

Connection between SD shield and Arduino Pro Mini is on 10-11-12-13 as per design

msavazzi

#1
Dec 20, 2015, 04:32 pm Last Edit: Dec 20, 2015, 05:22 pm by msavazzi
Even with a super simple sketch that opens the first file and read and then opens the second one and writes I get a lot of errors:

Code: [Select]

Initializing SD card...initialization done.
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files DOES NOT exists
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 01234556789012
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354


here the sketch:
Code: [Select]

/*
  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 10

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

 This example code is in the public domain.

 */
#define SD_CS_PIN SS
#include <SPI.h>
#include <SdFat.h>
SdFat SD;
File aFileS;              // global File handle. Only one file at time can be opened on arduino
File aFileT;              // global File handle. Only one file at time can be opened on arduino
#define c_FileName "d.bin"        // filename of the data file
#define c_TempName "t.bin"        // filename of the data file
#define c_NumMsg  0
#define c_MsgAtt  3
#define c_NumCell 6
#define c_NUMCELL_SIZE 14        // Phone number size
int numMsg;                // total number of messages
int msgAtt;               // last read message - starting from 1. First 160 byte of file reserved for other variables
char numCell[c_NUMCELL_SIZE];     // send to cell number

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(4800);

  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(SD_CS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

}

void loop()
{
  aFileS = SD.open(c_FileName);

  // if the file is available, write to it:
  if (aFileS) {
    aFileS.seek(c_NumMsg);
    numMsg = word(aFileS.read(), aFileS.read());
    Serial.print(F("loop\tnumMsg : ")); Serial.println(numMsg);
    aFileS.seek(c_MsgAtt);
    msgAtt = word(aFileS.read(), aFileS.read());  // default, init value is 0.
    Serial.print(F("loop\tmsgAtt : ")); Serial.println(msgAtt);
    aFileS.seek(c_NumCell);
    memset(numCell, 0, c_NUMCELL_SIZE);
    for (int ai = 0; ai < c_NUMCELL_SIZE; ai++)
    {
      numCell[ai] = aFileS.read();
    }
    numCell[c_NUMCELL_SIZE] = 0;
    Serial.print(F("loop\tnumCell : ")); Serial.println(numCell);
  }
  else {
    Serial.println(F("loop File Error"));
  }
  aFileS.close();
  delay(1000);

  if (SD.exists(c_TempName)) {
    Serial.println(F("loop\tfiles exists"));
    if (SD.remove(c_TempName)){
      Serial.println(F("loop\tfiles removed"));
    }
    else {
      Serial.println(F("loop\tERR files not removed"));
    }
  }
  else{
    Serial.println(F("loop\tfiles DOES NOT exists"));
  }
  delay(10);

  aFileT = SD.open(c_TempName, FILE_WRITE);
  Serial.print(F("loop\taFileT\t")); Serial.println(aFileT);

  if (aFileT) {   //if data file exists
    Serial.println(F("loop\tready to write"));
    for (int ai = 0; ai < 50; ai++)
    {
              aFileT.println(ai);
    }
    aFileT.close();
    delay(10);
    Serial.println(F("loop\tfinished writing - Process File"));
  }
  else {
    Serial.println(F("loop\tERROR open file"));
  }
}

fat16lib

#2
Dec 20, 2015, 09:40 pm Last Edit: Dec 20, 2015, 09:41 pm by fat16lib
What are you using for an SD shield/module?

I created a dummy d.bin file and your program seems to run fine forever without error.

Here is the content of t.bin as text
Quote
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

msavazzi

#3
Dec 22, 2015, 10:52 pm Last Edit: Dec 22, 2015, 11:03 pm by msavazzi
I have a Catalex MicroSD Card Manager v1.0 11/01/2013
http://world.taobao.com/item/19306030373.htm?fromSite=main&spm=a1z10.1.w4004-8438023200.9.WRWan2

with a microSD:
Code: [Select]

init time: 690 ms

Card type: SD1

Manufacturer ID: 0X1A
OEM ID: PQ
Product: MS   
Version: 1.0
Serial number: 0X9B046118
Manufacturing date: 10/2007

cardSize: 513.28 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0X80FF8000

SD Partition Table
part,boot,type,start,length
1,0X0,0X6,233,1001719
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0

Volume is FAT16
blocksPerCluster: 32
clusterCount: 31295
freeClusters: 31290
freeSpace: 512.66 MB (MB = 1,000,000 bytes)
fatStartBlock: 234
fatCount: 2
blocksPerFat: 123
rootDirStart: 480
dataStartBlock: 512


still no luck

msavazzi

#4
Dec 22, 2015, 11:10 pm Last Edit: Dec 22, 2015, 11:17 pm by msavazzi
Now is even worst.

The file is CANCELLED but this is the log:
Code: [Select]

Initializing SD card...
initialization done.
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists


and the code is the same: loop

It deadlocks on the remove file!

Changed the SD card... even worst:
Code: [Select]

Initializing SD card...
initialization done.
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files DOES NOT exists
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files exists
loop files removed
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 1
loop ready to write
loop finished writing - Process File
loop numMsg : 354
loop msgAtt : 0
loop numCell : 00393346932755
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error
loop files DOES NOT exists
loop aFileT 0
loop ERROR open file
loop File Error

Go Up