Hello PauS, it's me again!
Could you help me again with this ?
Now, I'm facing a problem that when I start reading the data on my SD-Card, almost time I get the "perfect string" like this on my TCP Client: "18432050" , but some times I don't know why I get something like this: "184320451843204618432047184320481843204918432050"
I tried to increase the delay time on my while loop when read the data from SD, and I still facing this problem... if I decrease the delay It's happens more frequently...
Here is my while loop:
while (myFile.available()) {
if (!client.available()) {
break;
}
int charCnt = myFile.read(buf,9);
delay(100);
if (myFile.read() == '\n') {
client.print(buf);
}
buf[charCnt] = '\0';
}
Here is my entire code:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
File myFile;
char buf[10];
// Enter a MAC address, IP address and Portnumber for your Server below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//IPAddress serverIP(192,168,2,130);
IPAddress serverIP(10,190,24,80);
int serverPort=8082;
// Initialize the Ethernet server library
// with the IP address and port you want to use
EthernetServer server(serverPort);
void setup()
{
Serial.begin(9600);
// disable w5100 while setting up SD
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if(SD.begin(4) == 0) Serial.println("SD fail");
else Serial.println("SD ok");
Ethernet.begin(mac,serverIP);
// Ethernet.begin() returns with its SPI enabled, so you must disable it.
digitalWrite(10,HIGH);
Serial.println("Ready");
}
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
String clientMsg ="";
while (client.connected()) {
if (client.available()) {
char c = client.read();
//Serial.print(c);
clientMsg+=c;//store the recieved chracters in a string
//if the character is an "end of line" the whole message is recieved
if (c == 'r') {
// Open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
if (!client.available()) {
break;
}
int charCnt = myFile.read(buf,9);
delay(100);
if (myFile.read() == '\n') {
client.print(buf);
}
buf[charCnt] = '\0';
}
// close the file:
myFile.close();
client.println("End of File Transmission!");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}//End SD Reading
}
}
}
// give the Client time to receive the data
delay(5);
// close the connection:
client.stop();
}
}