Arduino Forum

Using Arduino => Programming Questions => Topic started by: mikeztrainer on Dec 09, 2018, 09:42 am

Title: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 09, 2018, 09:42 am
hi,im using ftp code of surfertim with arduino mega2560 and w5100
https://playground.arduino.cc/Code/FTP

i can transfer text file successfully.but the text file transferred to the ftp server has an extra character in the beginning of the text.

heres the character always included in the text file = ÿ

help me what to do to avoid that character.thanks in advance
Title: Re: extra character ftp code of surfertim
Post by: J-M-L on Dec 09, 2018, 02:46 pm
post your code and explain where you file comes from
Title: Re: extra character ftp code of surfertim
Post by: MarkT on Dec 09, 2018, 03:28 pm
You set the wrong character encoding perhaps?
Title: Re: extra character ftp code of surfertim
Post by: Deva_Rishi on Dec 09, 2018, 03:31 pm
I always wonder what is the purpose of all the extra empty lines here (https://playground.arduino.cc/Code/FTP) Nothing to do with the issue of course, what is the ASCII value of the character ? do you just want to trim it off the text file before storage or send ? Check Mark's answer also, he's onto something.
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 09, 2018, 03:48 pm
heres the code,its almost the same with the code of surfer tim,i just change the ip,username and password.

#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, 88, 2 );   
IPAddress gateway( 192, 168, 88, 1 );
IPAddress subnet( 255, 255, 255, 0 );

// change to your server
IPAddress server(192, 168, 88, 1);

EthernetClient client;
EthernetClient dclient;

char outBuf[128];
char outCount;

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

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

}

byte doFTP()
{
#ifdef FTPWRITE
  fh = SD.open(fileName,FILE_READ);
  Serial.println(F("0"));
//#else
//  SD.remove(fileName);
//  fh = SD.open(fileName,FILE_WRITE);
#endif

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

#ifndef FTPWRITE 
  if(!fh.seek(0))
  {
    Serial.println(F("Rewind fail"));
    fh.close();
    return 0;   
  }
#endif
 Serial.println(F("2"));
  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 user"));

  if(!eRcv()) return 0;

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

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

    }
  }
 Serial.println(F("4"));
  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
 Serial.println(F("5"));
  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;
    }
  }
 Serial.println(F("6"));
  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"));
 Serial.println(F("7"));
  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;
 Serial.println(F("8"));
  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);
 Serial.println(F("9"));
  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;   
  }
 Serial.println(F("10"));
  while(fh.available())
  {
    Serial.write(fh.read());
  }

  fh.close();
}
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 09, 2018, 04:17 pm
i send file with text "hello"..but after it send to the ftp server,the text becomes "ÿhello"
Title: Re: extra character ftp code of surfertim
Post by: Deva_Rishi on Dec 09, 2018, 05:10 pm
i send file with text "hello"..but after it send to the ftp server,the text becomes "ÿhello"
If the text file is utf-16 then there is a bit order marker inserted at the start of the file, maybe that's it ... either way, you could just not send the first character. The character that is sent to the FTP server must be in the file, how did you create the file ? oh yeah please post code using code tags.  
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 09, 2018, 05:36 pm
i didnt change anything from the code of surfertim,except for the ips user ang password.
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 09, 2018, 06:04 pm
the character is in the file of test.txt,i dont why it always has that character in beginning of the file.
Title: Re: extra character ftp code of surfertim
Post by: J-M-L on Dec 09, 2018, 07:48 pm
the character is in the file of test.txt,i dont why it always has that character in beginning of the file.
where does this file come from?
Title: Re: extra character ftp code of surfertim
Post by: outsider on Dec 09, 2018, 08:40 pm
At the bottom of the reply page (NOT quick reply), you see Attachments and other options, click it and then click browse, find the "test.txt" file in your file system and attach it to your reply.
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 10, 2018, 01:25 am
where does this file come from?
from sdcard of w5100

At the bottom of the reply page (NOT quick reply), you see Attachments and other options, click it and then click browse, find the "test.txt" file in your file system and attach it to your reply.
its just a plain text file.i always check the content of the text.txt file,it never changed,and the length of it is 5 only for hello. maybe theres a problem with my laptop or has s virus.


Title: Re: extra character ftp code of surfertim
Post by: J-M-L on Dec 10, 2018, 01:30 am
Who wrote it there ? Does it has the weird byte(s) at the start in the first place ?
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 10, 2018, 01:54 am
Who wrote it there ? Does it has the weird byte(s) at the start in the first place ?
i make a txt file in my laptop,and paste it in the sdcard, then plug the sd card in w5100.
it doesnt have any wierd bytes ,its just a plain text.after the transfer,i checked the file in laptop,the date of the txt file didnt change.so that weird character comes during the transfer per byte.
Title: Re: extra character ftp code of surfertim
Post by: J-M-L on Dec 10, 2018, 01:56 am
Give us the file from your pc and the one from the se card (if you are not using plain ascii but uft8 or utf16 then depending on what your file's content is you can see weird bytes because you'll need multiple bytes for one character symbol)
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 10, 2018, 02:36 am
every time i run the sketch and press 'f',it first has an error "SD OPEN FAIL" first.
i need to press 'f' again to transfer the file.is it normal?
i tried changing the sdcard and format it,still got the same result.

i also tried in other laptop,download new arduino.i version use 1.6.5

i dont where that weird character came from.
Title: Re: extra character ftp code of surfertim
Post by: mikeztrainer on Dec 10, 2018, 03:12 am
Give us the file from your pc and the one from the se card (if you are not using plain ascii but uft8 or utf16 then depending on what your file's content is you can see weird bytes because you'll need multiple bytes for one character symbol)
this solves my problem.many thanks