How to upload multiple files to ftp in one session in Arduino C++

爱⌒轻易说出口 提交于 2019-12-23 04:21:29

问题


My code works well to upload first file in the loop to ftp. But it hangs once second file is going to be uploaded.

I'm reading the SD card root folder using lib first I set the ftp connection using connectFTP() to establish also data transfer port. Next I'm calling the fileTransfer(); While loop function works well, until first file is transfered. Once second file meet If criteria if (fileTemp != fileName && fileTemp[0] == '1' && fileTemp[1] == '9') and send the

      client.print(F("STOR "));
      client.println(fileTemp);

There is no responce.

    Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
 if (client.connect(config.server, config.ftpport)) {
    Serial.println("connected to server");
  }
  if(!eRcv()) Serial.println("fail");

  client.print(F("USER "));
  client.println(config.ftplogin);

  if(!eRcv()) Serial.println("fail USER");

  client.print("PASS ");
  client.println(config.ftppass);

  if(!eRcv()) Serial.println("fail PASS");

  client.println(F("SYST"));

  if(!eRcv()) Serial.println("fail SYST");;

  client.println(F("Type I"));

  if(!eRcv()) Serial.println("fail TYPE I");

  client.println(F("PASV"));

  if(!eRcv()) Serial.println("fail PASV");

  char *tStr = strtok(outBuf,"(,");
  int array_pasv[6];
  for ( int i = 0; i < 6; i++) {
    tStr = strtok(NULL,"(,");
    array_pasv[i] = atoi(tStr);
    if(tStr == NULL)
    {
      Serial.println(F("Bad PASV Answer"));    

    }
  }

  unsigned int hiPort,loPort;

  hiPort = array_pasv[4] << 8;
  loPort = array_pasv[5] & 255;

  Serial.print(F("Data port: "));
  hiPort = hiPort | loPort;
  Serial.println(hiPort);

  if (dclient.connect(config.server,hiPort)) {
    Serial.println(F("Data connected"));
  } 
  else {
    Serial.println(F("Data connection failed"));
    client.stop();
  } 
  client.println(F("CWD tpv.cba.pl"));
  if(!eRcv()) Serial.println("fail CWD");
  delay(500);
}

void fileTransfer() {
  if (!root.open("/")) {
      sd.errorHalt("open root failed");
  }
  char fileTemp[13];

  while (ftpfile.openNext(&root, O_RDONLY)) {
    ftpfile.getName(fileTemp,13);
    //Serial.println(fileTemp);
    if (fileTemp != fileName && fileTemp[0] == '1' && fileTemp[1] == '9') {
      Serial.println(fileTemp);
      client.print(F("STOR "));
      client.println(fileTemp);
      if(!eRcv()) Serial.println("fail STOR");
      size_t m;
      while((m = ftpfile.read(fileBuf, sizeof(fileBuf)))>0) {
        dclient.write(fileBuf, m);
      }
      if(!eRcv()) Serial.println("fail STOR");
    }
    ftpfile.close();
  }  

  dclient.stop();
  Serial.println(F("Data disconnected"));
    if(!eRcv()) Serial.println("fail Data disconnet");

  client.println(F("QUIT"));

  if(!eRcv()) Serial.println("fail QUIT");

  client.stop();
  Serial.println(F("Command disconnected"));

}

Logs received:

Starting connection to server...
connected to server
220---------- Welcome to Pure-FTPd [privsep] [TLS] ----------
220-You are user number 49 of 300 allowed.
220-Local time is now 14:11. Server port: 21.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this serve331 User xxxxx OK. Password required
230-Your bandwidth usage is restricted
230-OK. Current restricted directory is /
230 Max allowed filesize is 10485760 bytes
215 UNIX Type: L8
200 TYPE is now 8-bit binary
227 Entering Passive Mode (81,171,31,230,225,201)
Data port: 57801
Data connected
250 OK. Current directory is /xxx.cba.pl
19080400.log
150 Accepted data connection
226-File successfully transferred
226 0.094 seconds (measured here), 59.55 Kbytes per second
19080402.log

回答1:


I moved the PASV part from connectFTP() to fileTransfer() and it helps for loop, but still only max 6KB of file is stored at ftp.

void fileTransfer() {
  if (!root.open("/")) {
      sd.errorHalt("open root failed");
  }
  char fileTemp[13];

  while (ftpfile.openNext(&root, O_RDONLY)) {
    ftpfile.getName(fileTemp,13);
    //Serial.println(fileTemp);
    if (fileTemp != fileName && fileTemp[0] == '1' && fileTemp[1] == '9') {
      client.println(F("PASV"));

      if(!eRcv()) Serial.println("fail PASV");

      char *tStr = strtok(outBuf,"(,");
      int array_pasv[6];
      for ( int i = 0; i < 6; i++) {
        tStr = strtok(NULL,"(,");
        array_pasv[i] = atoi(tStr);
        if(tStr == NULL) Serial.println(F("Bad PASV Answer"));    
      }

      unsigned int hiPort,loPort;

      hiPort = array_pasv[4] << 8;
      loPort = array_pasv[5] & 255;

      Serial.print(F("Data port: "));
      hiPort = hiPort | loPort;
      Serial.println(hiPort);

      if (dclient.connect(config.server,hiPort)) {
        Serial.println(F("Data connected"));
      } else {
        Serial.println(F("Data connection failed"));
        client.stop();
      } 

      Serial.print(F("TEST: "));
      Serial.println(fileTemp);
      client.print(F("STOR "));
      client.println(fileTemp);
      delay(100);
      if(!eRcv()) Serial.println("fail STOR");

      for (uint32_t i=0; i< ftpfile.fileSize(); i += sizeof(fileBuf)){
        //Serial.println(i);
        ftpfile.read(fileBuf,sizeof(fileBuf));
        dclient.write(fileBuf, sizeof(fileBuf));
      }

      if(!eRcv()) Serial.println("fail STOR");
      dclient.stop();
      Serial.println(F("Data disconnected"));
      //if(!eRcv()) Serial.println("fail Data disconnet");  
    } 
    ftpfile.close();

  }  

  client.println(F("QUIT"));

  if(!eRcv()) Serial.println("fail QUIT");

  client.stop();
  Serial.println(F("Command disconnected"));

}

Logs received this time:

230-OK. Current restricted directory is /
230 Max allowed filesize is 10485760 bytes
215 UNIX Type: L8
200 TYPE is now 8-bit binary
227 Entering Passive Mode (81,171,31,230,202,243)
Data port: 51955
Data connected
TEST: 19080400.log
150 Accepted data connection
226-File successfully transferred
226 0.081 seconds (measured here), 68.95 Kbytes per second
Data disconnected
227 Entering Passive Mode (81,171,31,230,200,207)
Data port: 51407
Data connected
TEST: 19080402.log
150 Accepted data connection
226-File successfully transferred
226 0.085 seconds (measured here), 66.11 Kbytes per second
Data disconnected
227 Entering Passive Mode (81,171,31,230,203,91)
Data port: 52059
Data connected
TEST: 19080404.log
150 Accepted data connection
226-File successfully transferred
226 0.084 seconds (measured here), 67.00 Kbytes per second
Data disconnected
227 Entering Passive Mode (81,171,31,230,228,193)
Data port: 58561
Data connected
TEST: 19080504.log
150 Accepted data connection
226-File successfully transferred
226 0.079 seconds (measured here), 70.81 Kbytes per second
Data disconnected
227 Entering Passive Mode (81,171,31,230,214,248)
Data port: 55032
Data connected
TEST: 19080506.log
150 Accepted data connection 

19080506.log is about 4KB and program hangs up




来源:https://stackoverflow.com/questions/57376045/how-to-upload-multiple-files-to-ftp-in-one-session-in-arduino-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!