Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files are not created when called using separate fileOpen, fileWrite and fileClose functions #72

Open
jbteja opened this issue Aug 24, 2022 · 0 comments

Comments

@jbteja
Copy link

jbteja commented Aug 24, 2022

Files are not created when called using separate fileOpen, fileWrite and fileClose functions. Am I doing any wrong here?
Please Help.

Using:

  • Standard Esp32 Wroom Dev Board
  • Ch376s (getChipVer() returns 68)

My code In the Main loop:

chUsbHostOpenFile(fileName);
chUsbHostWriteFile(adat);
chUsbHostWriteFile(adat);
chUsbHostCloseFile();

Functions:

bool chUsbHostOpenFile(String fileName) {
  bool state = true;
  if(flashDrive.driveReady()) {
    flashDrive.setFileName(fileName.c_str());

    switch(flashDrive.openFile()){
      case ANSW_USB_INT_SUCCESS: // 0x14
      case ANSW_ERR_OPEN_DIR: // 0x41
        Serial.println(F("[USB] File opened successfully"));
        flashDrive.moveCursor(CURSOREND);
        break;

      case ANSW_ERR_MISS_FILE: // 0x42
        Serial.println(F("[USB] File doesn't exist, New file"));
        break;

      default:
        state = false;
        Serial.println(F("[USB] Unknown Error!"));
        break;
    }
  } else {
    state = false;
    Serial.println(F("[USB] OpenFile: Drive not found or busy"));
  }
  end:
  return state;
}


bool chUsbHostWriteFile(String sdata) {
  bool state = true;
  String timeStamp = String("\n" + timeClient.getFormattedDateTime() + " -> ");
  if(flashDrive.driveReady()) {
    if(flashDrive.getFreeSectors()){ //check the free space on the drive
      flashDrive.writeFile((char *) timeStamp.c_str(), timeStamp.length());
      flashDrive.writeFile((char *) sdata.c_str(), sdata.length()); //string, string length
      flashDrive.closeFile();
    } else {
      state = false;
      Serial.println(F("[USB] WriteFile: Disk full"));
    }
  } else {
    state = false;
    Serial.println(F("[USB] WriteFile: Drive not found or busy"));
  }
  return state;
}


bool chUsbHostCloseFile() {
  if(flashDrive.driveReady()) {
    flashDrive.closeFile();
    Serial.println(F("[USB] File closed successfully"));
  } else {
    Serial.println(F("[USB] CloseFile: Drive not found or busy"));
    return false;
  }
  return true;
}

But if Open, write and close the file in the same function the file gets created as expected.

bool chUsbHostWriteCloseFile(String fileName, String sdata) {
  bool state = true;
  String timeStamp = String("\n" + timeClient.getFormattedDateTime() + " -> ");
  if(flashDrive.driveReady()) {
    flashDrive.setFileName(fileName.c_str());
    if(flashDrive.openFile() == ANSW_USB_INT_SUCCESS){
    flashDrive.moveCursor(CURSOREND);
    }

    if(flashDrive.getFreeSectors()){ //check the free space on the drive
      flashDrive.writeFile((char *) timeStamp.c_str(), timeStamp.length());
      flashDrive.writeFile((char *) sdata.c_str(), sdata.length()); //string, string length
      flashDrive.closeFile();
    } else {
      state = false;
      Serial.println(F("[USB] WriteFile: Disk full"));
    }
  } else {
    state = false;
    Serial.println(F("[USB] WriteFile: Drive not found or busy"));
  }
  return state;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant