I have a Sparkfun Pro Ethernet. I have a computer that prints reports for a manufacturing process, each report is about 2000 bytes of text. I have set up Ethernet pro usb as a generic text printer in windows, and the pc prints the report and arduino catches it and sends it over TCP/IP to a Server. Below is the code i am using.
I was thinking that it would be better to buffer the whole text and try to send it several TCP packets instead of a byte at a time like it is now. And this way I could hold on to it connect to server send and then disconnect. But will i need to get an arduino Mega to be able to do this. I was just trying to get someone elses thoughts on this.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,177 };
byte server[] = { 192,168,1,3 };
unsigned long lastconnected;
unsigned long lastdisconnected;
unsigned long currenttime;
int count = 0;
Client client(server, 7000);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
if (client.connect()) {
lastconnected = millis();
}
}
void loop()
{
if (Serial.available() > 0) { //Resend printed data from Serial out TCP
if(client.connected()){
char incomingByte = Serial.read();
if(incomingByte!= 0x0C){
client.print(incomingByte);
count++;
}
if (incomingByte == 0x0C){ // Sees Form Feed Stops and disconnects
client.println(incomingByte);
client.println("End of Ticket"); // Marks end of print job
client.println(count);
client.stop();
lastdisconnected = millis();
count=0;
}
}
}
if (millis() - lastconnected > 540000 && client.connected()){ // Disconnects after connected for nine minutes
client.stop();
lastdisconnected = millis();
delay(100);
}
if (!client.connected() && (millis() - lastdisconnected > 100)){ //If disconnected, reconnect
client.connect();
lastconnected = millis();
delay(1000);
}
}