0

我的代码可以很好地将循环中的第一个文件上传到 ftp。但是一旦要上传第二个文件,它就会挂起。

我首先使用 lib 读取 SD 卡根文件夹,我设置 ftp 连接connectFTP()用于建立数据传输端口。接下来我调用fileTransfer();While 循环函数运行良好,直到传输第一个文件。一旦第二个文件满足 If 条件 if (fileTemp != fileName && fileTemp[0] == '1' && fileTemp[1] == '9') 并发送

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

没有回应。

    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"));

}

收到的日志:

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
4

1 回答 1

0

我将PASV部件从connectFTP()to移到了fileTransfer(),它有助于循环,但在 ftp 中仍然只存储了最大 6KB 的文件。

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"));

}

这次收到的日志:

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大约4KB,程序挂了


于 2019-08-06T14:37:01.107 回答