Pages: [1]   Go Down
Author Topic: Recieve Serial and send out TCP IP  (Read 591 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Code:
#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);
   }
}
Logged

UK, Southwest
Offline Offline
Full Member
***
Karma: 5
Posts: 138
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why do you care about the network load?  TCP takes care of a packet being missed by resending it.

 If there is a load problem or performance problem you could send in packets of say, 10 bytes.  That would reduce your network load a lot and you could keep the same hardware.  You would have to declare a couple of buffers and switch between them, or track buffer usage.
And keep track of any part filled buffers at the end of the transmission and send them.

Also I see your code if tracking an 0x0c for the end of data.  If that is missed, you will cocatanate the next ticket.  Perhaps you should have a timeout in your code as well, in case the 0x0c is dropped.
     
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

First your note about putting in a timeout is a good idea. Thank You. 

I guess the only thing that I am really worried about is that if I receive a Print job while not connected. Will I just loose part of it?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I played with a little more and I one major flaw is that the print queue does no wait for it. I am able to send print jobs one after the other and they write over each other. Does arduino need to tell the windows it is busy and when it is ready? Does anyone have experience with this.

I believe if I was able to do this then there would be no need to buffer the print job.
Logged

Pages: [1]   Go Up
Jump to: