Arduino Due with Ethernet shield and sdfat problem (please help)

Hi forum,
I have the Arduino Due and the Ethernet Shield (W5100). My testing project is transmit a text file from LabVIEW throught Ethernet to the Due, then storage to the SD card. I tried with the SD standard library and they can work well, but the transmission and writting speed too slow (about 1kB/s). Then i tried the sdfat library (SdFatBeta20130621) hope they can work faster. I test individual Ethernet and write data to SD card both work well, but when i paired together, just some first characters from text file can write to SD file, all characters later are 每每每每每每每每每每每每每...
This is my code:

#include <SdFat.h>
#include <SPI.h>
#include <Ethernet.h>
#define USE_ARDUINO_SPI_LIBRARY 1

SdFat sd;
SdFile myFile;

const int chipSelect = 4;

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,5,177);
IPAddress server(192,168,5,20);

long int i=0;
char j=0;
char c;


EthernetClient client;

void setup() 
{
聽 pinMode(10, OUTPUT);
聽 digitalWrite(10, HIGH);
聽 //Serial Init------------------------------------------------------ 
聽 Serial.begin(9600);
聽 while (!Serial) {}
聽 Serial.println("Type any character to start");
聽 while (Serial.read() <= 0) {}
聽 delay(400);聽 
聽 
 //SD Card Init----------------------------------------------------- 
聽 Serial.print("\nInitializing SD card...");
聽 if (!sd.begin(chipSelect, SPI_HALF_SPEED)) sd.initErrorHalt();

聽 if (!myFile.open("test.txt", O_RDWR | O_CREAT)) {
聽 聽 sd.errorHalt("opening test.txt for write failed");}
聽 
//Ethernet-------------------------------------------------------- 
聽 digitalWrite(10, LOW);
聽 Ethernet.begin(mac, ip);
聽 delay(1000);
聽 Serial.println("connecting...");
聽 connect_again:
聽 if (client.connect(server, 8001)){
聽 聽 Serial.println("connected");} 
聽 else{
聽 聽 Serial.println("connection failed");
聽 聽 delay(2000);
聽 聽 goto connect_again;} 
}

void loop() 
{
聽 if (client.available()) 
聽 {
聽 聽 c = client.read();聽  
//聽 聽 Serial.print(c);
聽 聽 myFile.print(c);
聽 聽 i++;
聽 聽 if(i==1000) 
聽 聽  {
聽 聽 聽  myFile.close();
聽 聽 聽  Serial.println("Finished");
聽 聽  }
聽 }
聽 if (!client.connected()) 
聽 {
聽 聽 Serial.println();
聽 聽 Serial.println("disconnecting.");
聽 聽 client.stop();
聽 }
}

I am a newbie in arduino, any help please.
Thank you so much,

Where do you prevent corruption by closing the file?

The text file i want to write to SD card is: " aaaa...a " (1000 characters "a"), but result is " a每每每每...每 ", just the first charecter is true =(

聽 connect_again:
聽 if (client.connect(server, 8001)){
聽 聽 Serial.println("connected");} 
聽 else{
聽 聽 Serial.println("connection failed");
聽 聽 delay(2000);
聽 聽 goto connect_again;}

Forget everything you know about basic. Lose the label and the goto statement. Learn to use a while loop and the break statement.

You are not reading all the data from the client. You need a while loop that executes the body as long as client.connected() is true.

In the body of that while loop, you need to read any available data, and write to the SD card, if the count is less than 1000. Do NOT stop reading from the client if the response contains more than 1000 characters.

while(client.connected())
{
聽  while(client.available() > 0)
聽  {
聽 聽 聽 // Read and possibly store data
聽  }
}

The way your code is structured, you tell the client to go away as soon as you have read all data that has arrived, even though the client may still be sending data.

You can read data far faster than the client can send it, in the same way you can read this reply in far less time than it took me to hunt and peck it out.

I am really thanks with your help, Mr.PaulS.
I rewrited the code:

void loop() 
{
while(client.connected())
{
 聽 while(client.available() > 0)
 聽 {
 聽 聽 聽c = client.read();
 聽 聽 聽myFile.print(c);
 聽 聽 聽i++;
 聽 聽 聽if(i==1000) 
 聽 聽 聽 {
 聽 聽 聽 myFile.close();
 聽 聽 聽 Serial.println("Finished");
 聽 聽 聽 }
 聽 }
}
}

The result is " aaa...a每每每...每 " , with 512 first characters is true "a" , and other characters later still wrong "每..."

When i change the code:

void loop() 
{
while(client.connected())
{
 聽 while(client.available() > 0)
 聽 {
 聽 聽 聽c = client.read();
 聽 聽 聽if( c>0 && c<255 )
 聽 聽 聽{
 聽 聽 聽myFile.print(c);
 聽 聽 聽i++;
 聽 聽 聽if(i==1000) 
 聽 聽 聽 {
 聽 聽 聽 myFile.close();
 聽 聽 聽 Serial.println("Finished");
 聽 聽 聽 }
 聽 聽 聽}
 聽 }
}
}

Result is it never print the "Finished"
I guess that ethernet has been disconnected when transmitting data !?
I will try more...

I would try to determine if it is the ethernet or the SD card. It sounds like it may be the SD. Add the "Serial.print(c);" call again to see what you are getting from the w5100.

void loop() 
{
 聽while(client.connected())
 聽{
 聽 聽 while(client.available() > 0)
 聽 聽 {
 聽 聽 聽 聽c = client.read();
 聽 聽 聽 聽// add this
 聽 聽 聽 聽Serial.print(c);

 聽 聽 聽 聽if( c>0 && c<255 )
 聽 聽 聽 聽{
 聽 聽 聽 聽 聽myFile.print(c);
 聽 聽 聽 聽 聽i++;
 聽 聽 聽 聽 聽if(i==1000) 
 聽 聽 聽 聽 聽{
 聽 聽 聽 聽 聽 聽myFile.close();
 聽 聽 聽 聽 聽 聽Serial.println("Finished");
 聽 聽 聽 聽 聽}
 聽 聽 聽 }
 聽 聽 }
 聽}
}

This code will cause the sketch to lock up if the connection breaks or the server stalls. It will never exit the "while(client.connected())" loop. It needs a timeout to prevent that. This code has the timeout feature:
http://playground.arduino.cc/Code/WebClient

edit: And it does not close the connection. There should be a "client.stop()" call after the "while(client.connected())" loop exits.

Thank you very much sir,
I try with your suggest code and it print back to serial monitor " aaa...a每每每...每 " , with 526 first characters "a", later "每..."
Seem no problem with SD card, so i will try to check the timeout from your link. Thank you,

Try the code on the link I posted without the SD code, just the w5100 code and no SD card in the slot. See if it does the same thing. If it does, let me know. Maybe I can help you debug it.

There was an ethernet library problem in IDE versions prior to V1.0.1. It would not exit the "while(client.available())" loop.

Sorry about my feedback late, my health not good yesterday :roll_eyes:
I checked the code just only Ethernet code, it work very well, all characters read and print true.
My IDE version is 1.5.4 r2 (the latest version for Due).
So, i really do not know what happen when use Ehernet and SD together =(

You said the standard SD library worked, but very slow? Maybe you should go back to it. There are ways to improve performance.

Thank you sir, i will try more... :stuck_out_tongue: