Send raw data through Ethernet

Hi,

I have an etherned shield (http://www.bricogeek.com/shop/17-arduino-ethernet-shield.html) and i want to send an INT, one BYTE, ONE INT, a server python will be waiting for that data. So this is my arduino code:

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 112 };
byte server[] = { 192, 168, 1, 101}; 
byte gateway[] = {192, 168, 1, 1};

Client client(server, 5400);

int app = 15;
byte code = 3;
int length = 200;

void setup()
{
  Ethernet.begin(mac, ip, gateway);  
  delay(1000);
 
  if (client.connect()) {
    client.write(app);
    client.write(code);
    client.write(length); 
    client.stop();    
    delay(1000);
   } 
}

In the server python I receive only one byte. i don't know what's wrong. Can someone help me?

Do you tried to capture the packets with Wireshark (http://www.wireshark.org/) ? It can be intersting to know what is exactly received by the server

Yes,
I always sends bytes, for instance
const int APPLICATION = 69;
client.write(APPLICATION);

and

const byte APPLICATION = 69;
client.write(APPLICATION);

It sends the same : 1 byte : 45 (in hex)

How many times is your Python program reading? It may be that each byte is sent in a separate packet. You could try having the Python program do 3 reads.

Regards,

-Mike

I hope this helps;

Try changing to this: when you call stop, the connection is closing, perhaps before the data gets through the buffers on the target. (Although the TCPIP is sent synchronously and the data is sent out in packets before the stop registers, the stop may force your receiver's connection to close, destroying the buffer with the data you just sent after only the first read.)

If you have a lot of processing on the receiver, increase delay.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 112 };
byte server[] = { 192, 168, 1, 101};
byte gateway[] = {192, 168, 1, 1};

Client client(server, 5400);

int app = 15;
byte code = 3;
int length = 200;

void setup()
{
  Ethernet.begin(mac, ip, gateway);
  delay(1000);

  if (client.connect()) {
    client.write(app);
    client.write(code);
    client.write(length);
    delay(5);
    client.stop();
   }
}

I think the Problem is that .write can only write byte or char.

An int is larger than a byte (actually it's 2 bytes), so only the LSB is sent.

You have to split up your int into two seperate bytes.

I found this in thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1260110802/3#3

int val;
..
..
byte MSB = val >> 8;
byte LSB = val & 0xFF;
// now write bytes 

//e.g
server.write(MSB);
server.write(LSB);
....
// then read them back
val = (MSB << 8) | LSB;

That works.

admiraldr