如何在Arduino C ++中的一个会话中将多个文件上传到ftp [英] How to upload multiple files to ftp in one session in Arduino C++

查看:164
本文介绍了如何在Arduino C ++中的一个会话中将多个文件上传到ftp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

没有回应.

    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

推荐答案

我将PASV部分从connectFTP()移到了fileTransfer(),这有助于循环,但是ftp上最多只能存储6KB的文件.

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

}

这次收到的日志:

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,程序挂起

19080506.log is about 4KB and program hangs up


这篇关于如何在Arduino C ++中的一个会话中将多个文件上传到ftp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆