Please нelp to find the bug - a file transfer from FTP

acronis
Posts: 143
Joined: Thu Mar 02, 2017 12:32 pm

Please нelp to find the bug - a file transfer from FTP

Post by acronis » Mon Jul 31, 2017 5:43 am

took the example from here: http://playground.arduino.cc/Code/FTP

File transfer to FTP server is excellent.


// #define FTPWRITE

But when I need to read from ftp server and write to the SD card , the file is created on the SD card with 0 length , and the program hangs or loops.
Can't find what the problem is.

The file is opened for reading via FTP and then FTP gives the error "Connection timed put"

Need help


understand that never goes out of this subroutine:

Code: Select all

.......
 dclient.stop();
 Serial.println(F("Data disconnected"));

 serial.print("here goes");
 if(!eRcv()) return 0;   //comes in here ever comes back

 serial.print("here misses");
......


I suspect that something is wrong with this routine

Code: Select all

.........
byte eRcv()
{
 byte respCode;
 byte thisByte;

 while(!client.available()) delay(1);
 

 respCode = client.peek();

 outCount = 0;

 while(client.available())
 { 
  thisByte = client.read();  
  Serial.write(thisByte);

  if(outCount < 127)
  {

   outBuf[outCount] = thisByte;
   outCount++;   
   outBuf[outCount] = 0;
   }
 }

 if(respCode >= '4')
 {
  efail();
  return 0; 
 }
 return 1;
}
.........

the program is fully

Code: Select all

/*
    FTP passive client for IDE v1.0.1 and w5100/w5200
    Posted October 2012 by SurferTim
    Modified 6 June 2015 by SurferTim
 */

#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
// comment out next line to write to SD from FTP server
// #define FTPWRITE

// this must be unique
 byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x67 };  

// change to your network settings
 IPAddress ip( 192, 168, 2, 2 );    
 IPAddress gateway( 192, 168, 2, 1 );
 IPAddress subnet( 255, 255, 255, 0 );

// change to your server
 IPAddress server( 1, 2, 3, 4 );

 EthernetClient client;
 EthernetClient dclient;

char outBuf[128];
char outCount;

// change fileName to your file (8.3 format!)
char fileName[13] = "test.txt";

void setup()
{
   Serial.begin(9600);

   digitalWrite(10,HIGH);

   if(SD.begin(4) == 0)
   {
     Serial.println(F("SD init fail"));          
   }

   Ethernet.begin(mac, ip, gateway, gateway, subnet); 
   digitalWrite(10,HIGH);
   delay(2000);
   Serial.println(F("Ready. Press f or r"));
}

void loop()
{
   byte inChar;

   inChar = Serial.read();

   if(inChar == 'f')
   {
     if(doFTP()) Serial.println(F("FTP OK"));
     else Serial.println(F("FTP FAIL"));
   }

   if(inChar == 'r')
   {
     readSD();    
   }

}

 File fh;

 byte doFTP()
{
#ifdef FTPWRITE
   fh = SD.open(fileName,FILE_READ);
#else
   SD.remove(fileName);
   fh = SD.open(fileName,FILE_WRITE);
#endif

   if(!fh)
   {
     Serial.println(F("SD open fail"));
     return 0;    
   }

#ifndef FTPWRITE  
   if(!fh.seek(0))
   {
     Serial.println(F("Rewind fail"));
     fh.close();
     return 0;    
   }
#endif

   Serial.println(F("SD opened"));

   if (client.connect(server,21)) {
     Serial.println(F("Command connected"));
   } 
   else {
     fh.close();
     Serial.println(F("Command connection failed"));
     return 0;
   }

   if(!eRcv()) return 0;

   client.println(F("USER myuser"));

   if(!eRcv()) return 0;

   client.println(F("PASS mypassword"));

   if(!eRcv()) return 0;

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

   if(!eRcv()) return 0;

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

   if(!eRcv()) return 0;

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

   if(!eRcv()) return 0;

   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(server,hiPort)) {
     Serial.println(F("Data connected"));
   } 
   else {
     Serial.println(F("Data connection failed"));
     client.stop();
     fh.close();
     return 0;
   }

#ifdef FTPWRITE 
   client.print(F("STOR "));
   client.println(fileName);
#else
   client.print(F("RETR "));
   client.println(fileName);
#endif

   if(!eRcv())
   {
     dclient.stop();
     return 0;
   }

#ifdef FTPWRITE
   Serial.println(F("Writing"));

   byte clientBuf[64];
   int clientCount = 0;

   while(fh.available())
   {
     clientBuf[clientCount] = fh.read();
     clientCount++;

     if(clientCount > 63)
     {
       dclient.write(clientBuf,64);
       clientCount = 0;
     }
   }

   if(clientCount > 0) dclient.write(clientBuf,clientCount);

#else
   while(dclient.connected())
   {
     while(dclient.available())
     {
       char c = dclient.read();
       fh.write(c);      
       Serial.write(c); 
     }
   }
#endif

   dclient.stop();
   Serial.println(F("Data disconnected"));

   if(!eRcv()) return 0;

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

   if(!eRcv()) return 0;

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

   fh.close();
   Serial.println(F("SD closed"));
   return 1;
}

 byte eRcv()
{
   byte respCode;
   byte thisByte;

   while(!client.available()) delay(1);

   respCode = client.peek();

   outCount = 0;

   while(client.available())
   {  
     thisByte = client.read();    
     Serial.write(thisByte);

     if(outCount < 127)
     {
       outBuf[outCount] = thisByte;
       outCount++;      
       outBuf[outCount] = 0;
     }
   }

   if(respCode >= '4')
   {
     efail();
     return 0;  
   }

   return 1;
}


void efail()
{
   byte thisByte = 0;

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

   while(!client.available()) delay(1);

   while(client.available())
   {  
     thisByte = client.read();    
     Serial.write(thisByte);
   }

   client.stop();
   Serial.println(F("Command disconnected"));
   fh.close();
   Serial.println(F("SD closed"));
}

void readSD()
{
   fh = SD.open(fileName,FILE_READ);

   if(!fh)
   {
     Serial.println(F("SD open fail"));
     return;    
   }

   while(fh.available())
   {
     Serial.write(fh.read());
   }

   fh.close();
}


"in code.if you enter the string " fh.close(); " in code

Code: Select all

 dclient.stop();
 Serial.println(F("Data disconnected"));

 fh.close();      //         <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<   added this line

 if(!eRcv()) return 0;


if you comment out the line // if(!eRcv()) return 0;

Code: Select all

.......
 dclient.stop();
 Serial.println(F("Data disconnected"));

 //if(!eRcv()) return 0;

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

// if(!eRcv()) return 0;

 client.stop();
 Serial.println(F("Command disconnected"));
 ............
the program works fine and the file is created on the SD card !!!

What's wrong with the line if(!eRcv()) return 0 ???
Why with this string, the program loops not working ?

Can't find the problem

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Please нelp to find the bug - a file transfer from FTP

Post by stevestrong » Mon Jul 31, 2017 8:30 am

Shouldn't you include "Ethernet_STM.h" instead of "Ethernet.h" ?

Do you connect both SD card and Ethernet module to the same SPI1 port?
Check the Ethernet chip select pin.
Alternatively use separate SPI ports.

acronis
Posts: 143
Joined: Thu Mar 02, 2017 12:32 pm

Re: Please нelp to find the bug - a file transfer from FTP

Post by acronis » Mon Jul 31, 2017 8:42 am

Hello stevestrong .

I use your latest version library https://github.com/stevstrong/Ethernet_STM32
+ https://github.com/stevstrong/Arduino_STM32
+ SDIO viewtopic.php?f=39&t=2215
+ https://github.com/stevstrong/Adafruit_ ... 6bit_STM32

board STM32F407VET6 black + w5500 (SPI1) + TFT



#include <Adafruit_TFTLCD_16bit_STM32.h>
#include "SdioF4.h"
#include <Ethernet_STM32.h>
SPIClass mSpi(1);


byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x59, 0x99 };
IPAddress ip( 10, 22, 5, 199 );
IPAddress gateway( 10,22, 1, 1 );
IPAddress subnet( 255, 255, 0, 0 );
IPAddress server( 10,22, 5, 4 );
EthernetClient client;
EthernetClient dclient;




Ethernet.init(mSpi, PA4);
Ethernet.begin(mac, ip, gateway, gateway, subnet);
Last edited by acronis on Mon Jul 31, 2017 8:58 am, edited 5 times in total.

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Please нelp to find the bug - a file transfer from FTP

Post by stevestrong » Mon Jul 31, 2017 8:44 am

Then you should use:

Code: Select all

#include <SdioF4.h>
#include <Ethernet_STM32.h>
Pay attention:
viewtopic.php?f=39&t=2215

acronis
Posts: 143
Joined: Thu Mar 02, 2017 12:32 pm

Re: Please нelp to find the bug - a file transfer from FTP

Post by acronis » Mon Jul 31, 2017 8:47 am

I did everything as written in this thread viewtopic.php?f=39&t=2215

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Please нelp to find the bug - a file transfer from FTP

Post by stevestrong » Mon Jul 31, 2017 8:48 am

Did you test separately SDIO and Ethernet?

acronis
Posts: 143
Joined: Thu Mar 02, 2017 12:32 pm

Re: Please нelp to find the bug - a file transfer from FTP

Post by acronis » Mon Jul 31, 2017 8:53 am

No - not tested.
How do I test receiving a file from an FTP server without writing to SD card ?

acronis
Posts: 143
Joined: Thu Mar 02, 2017 12:32 pm

Re: Please нelp to find the bug - a file transfer from FTP

Post by acronis » Mon Jul 31, 2017 9:07 am

checked again - the program stops or loops it on the


dclient.stop();
if(!eRcv()) return 0; // <<<<<<<<< this routine

and

client.println(F("QUIT"));
if(!eRcv()) return 0; // <<<<<<<<<<<<< this routine

Code: Select all

byte eRcv()
{
 byte respCode;
 byte thisByte;

 while(!client.available()) delay(1);

 respCode = client.peek();

 outCount = 0;

 while(client.available())
 { 
  thisByte = client.read();  
  Serial.write(thisByte);

  if(outCount < 127)
  {
   outBuf[outCount] = thisByte;
   outCount++;   
   outBuf[outCount] = 0;
  }
 }

 if(respCode >= '4')
 {
  efail();
  return 0; 
 }

 return 1;
}



If you comment out so

dclient.stop();
//if(!eRcv()) return 0; // <<<<<<<<< this routine
client.println(F("QUIT"));
//if(!eRcv()) return 0; // <<<<<<<<<<<<< this routine

everything works fine, the file is normally recorded on the SD card with the FTP server

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Please нelp to find the bug - a file transfer from FTP

Post by stevestrong » Mon Jul 31, 2017 9:24 am

acronis wrote:
Mon Jul 31, 2017 8:53 am
No - not tested.
How do I test receiving a file from an FTP server without writing to SD card ?
You should test separately.
You could just print out to serial the received bytes.
I don't know what eRcv() does, but if it works without, then use it like that.
Check out the warning messages in Arduino IDE.

victor_pv
Posts: 1741
Joined: Mon Apr 27, 2015 12:12 pm

Re: Please нelp to find the bug - a file transfer from FTP

Post by victor_pv » Mon Jul 31, 2017 12:56 pm

Check this value on that routine:

Code: Select all

respCode = client.peek();
Seems like if it's >4 it would return 0 and cause the hang.
Not sure what's meant to do, but perhaps the problem is not with it, but with client.peek()

Post Reply