Udp.write(highByte()) issue

I seem to have an issue with highByte() not working properly.
Or, is it something to to with Udp.write(), or the combination?

See below for my sample data from this code.
Hardware is Arduino Mega 2560 and Seeed Ethernet board.

#include <SPI.h>         
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address for this device
IPAddress ip(192, 168, 1, 170);      // IP for this device
unsigned int localPort = 8880;      // local port to listen on

IPAddress remoteIP(192, 168, 1, 240);	// the remote IP address
unsigned int remotePort = 7770;		// the destination port

EthernetUDP Udp;

unsigned int MyArray1[1502];
unsigned int MyIndex = 1;
unsigned int x = 0;



void setup() {
    // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  Serial.println("Here we go.");
  int StartTime;
}


void loop() {
  if(MyIndex < 1500){
    MyArray1[MyIndex] = MyIndex;
    MyIndex = MyIndex + 1;
  }

    
  if(MyIndex == 1500){
    Udp.beginPacket(remoteIP, remotePort);
    for(x = 1; x < MyIndex; x++){    
    Udp.write(highByte(MyArray1[x]));
    Udp.write(lowByte(MyArray1[x]));
    Udp.write("**");
    }
    Udp.endPacket();
    
    delay(5000);
    MyIndex = 1;
  }
  
}

Why does the highByte value start with 04?
The lowByte always seems to work ok.
I used WireShark to capture data.

Sample Data:
04 01 2a 2a
04 02 2a 2a
04 03 2a 2a
04 04 2a 2a
04 05 2a 2a
etc.....

What version of the IDE are you using? That sounds like a bug in IDE v1.0 and v0022. The high byte set to 04 would be bit 10 set, correct?

From where in the data stream was that found?

For example:
04 01

would be 0x0401 = 1025. So depending on where in the packet that data came from, it could be correct.

Software 1.0.1

The very first packet should start with 00 01 2A 2A 00 02 2A 2A 00 03 2A 2A, etc.
I just put the asterisks in there for separation while viewing.

I have tested some more:
If I set the counter value from 1500 to 513 or 512 or 500, it works fine.
If I set the counter value to 514 or more, it exhibits the problem.

500 = 0000 0001 1111 0100 works fine.
513 = 0000 0010 0000 0001 works fine.
514 = 0000 0010 0000 0010 problem.

Could it be an issue with the highbyte function converting your unsigned values to signed values?

highbyte and lowbyte are defined in Arduino.h as:

#define lowByte(w) ((uint8_t) ((w) & 0xff))
#define highByte(w) ((uint8_t) ((w) >> 8))

I'm not sure if this is the case here, but sometimes unintended conversions to "int" values can hose you.

I don't believe so.

Here i have changed everything to ints.
I also am printing to serial the highByte and lowBtye, and they both appear fine in the serial monitor.
The highByte data looks fine in the serial monitor, but not in the packets.

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address for this device
IPAddress ip(192, 168, 1, 170);      // IP for this device
unsigned int localPort = 8880;      // local port to listen on

IPAddress remoteIP(192, 168, 1, 240);	// the remote IP address
unsigned int remotePort = 7770;		// the destination port

EthernetUDP Udp;

int MyArray1[1502];
int MyIndex = 1;
int x = 0;
int temp1 = 0;


void setup() {
    // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  
  // initialize serial communications at 9600 bps:
  Serial.begin(9600); 
  Serial.println("Here we go.");
  int StartTime;
  
  
}


void loop() {
  if(MyIndex < 1500){
    MyArray1[MyIndex] = MyIndex;
    MyIndex = MyIndex + 1;
  }

    
  if(MyIndex == 1500){
    Udp.beginPacket(remoteIP, remotePort);
    for(x = 1; x < MyIndex; x++){    
    temp1 = highByte(MyArray1[x]);
    Serial.println(temp1);
    Udp.write(temp1);
    temp1 = lowByte(MyArray1[x]);
    Serial.println(temp1);
    Udp.write(temp1);
    //Udp.write("**");
    }
    Udp.endPacket();
    
    delay(5000);
    MyIndex = 1;
  }
  
}

I also am printing to serial the highByte and lowBtye, and they both appear fine in the serial monitor.
The highByte data looks fine in the serial monitor, but not in the packets.

That was going to be my next question. You are using the ethernet library included with the IDE? No custom stuff, right?

edit: I looked at the code. Try sending it as two bytes like this.

    byte outBuf[2];
    Udp.beginPacket(remoteIP, remotePort);
    for(x = 1; x < MyIndex; x++){    
    outBuf[0] = highByte(MyArray1[x]);
    Serial.println(outBuf[0]);
    outBuf[1] = lowByte(MyArray1[x]);
    Serial.println(outBuf[1]);
    Udp.write(outBuf,2);
    }
    Udp.endPacket();

Does that still send the 04 first?

no custom imports or anything.
I'm too new to this to know about anything that doesn't come with the software.

i've also tried:
temp1 = (MyArray1[x]) >> 8;
it gives the same results when sent via Udp.write() .

Did you see my edit to my last post above? Try sending it as a 2 byte array. That will avoid one routine in the library.

edit: It may be possible you are overflowing the w5100 tx buffer. You are sending 3000 bytes, and the tx buffer is something like 1500 bytes. How does it do if you send 2 bytes? Skip the for loop as a test.

I tried your suggestion, but same result.

you may be correct re: the transmit buffer:

http://code.google.com/p/arduino/issues/detail?id=642

i'll have to figure out how to implement this "fix".

thanks for the help.
Jeff.

If you have any problems with issue 642, let me know. I'm the author.

Did you try sending just two bytes?

i didn't try just 2 bytes, but earlier i was able to send up to 1026 bytes and it worked ok. 513 x 2 bytes was the max.

i'm guessing that other post infers that i'll have recompile?

it's been a few years since had to compile, and have never done so with this platform.
so, if you have any clues, i'd appreciate it.

Change the .cpp and .h files, then recompile your sketch. The library is compiled with the sketch. That's it. :slight_smile:

thanks,

i changed the EthernetClient.cpp and EthernetClient.h files.

In my code I added:

      while(Client.free() < 10) {
        Udp.endPacket();
        Udp.beginPacket(remoteIP, remotePort);
      }

I have tried to change the "Client.free" to Udp.free, EthernetClient.free, etc to no avail.

Errors:
expected primary-expression before '.' token
'class EthernetUDP' has no member named 'free'

That is my bad. The change was for TCP, not UDP. I did not modify the code for the UDP functions.

Do you need the function to be non-blocking? That is the only reason for using that free function.

I need to dump all the data as fast as it can , only after it has collected it all.

This works, but is too slow between packets.

  for(x = 1; x < MyIndex; x++){    
      outBuf[0] = highByte(MyArray1[x]);
      Serial.println(outBuf[0]);
      outBuf[1] = lowByte(MyArray1[x]);
      Serial.println(outBuf[1]);
      
      if(x == 720 or x == 1440 or x == 2160 or x == 2880){
        Udp.endPacket();
        Udp.beginPacket(remoteIP, remotePort);
      }
      Udp.write(outBuf,2);
    }
    Udp.endPacket();