Sensor reading to UDP Problems

I have been working on a system that reads from the 6 analog inputs calculates the low and high byte and places them in and array, it then adds an end character and then sends it over UDP. It is a modified (slightly) example from the 'Arduino Cookbook'.
Everything works fine as long as the value read by the sensors is higher than 255. As soon as the value drops below 255 and it has to send a zero the packet length of the data packet then shortens. I was wondering if anyone can help me make a system that always sends 12 bytes of data even if a sensor reads 0.

My code is as follows.

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUDP.h>  // Arduino 1.0 UDP library

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC address to use
byte ip[] = {10, 0, 0, 201 };    // Arduino's IP address
byte send_ip[] = {10, 0, 0, 255 };
unsigned int localPort = 50000;      // local port to listen on

char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
;
}

void loop() {

    sendAnalogValues(send_ip, 50000); // tell the sender the values of our analog ports

  delay(1000);  //wait a bit
}

void sendAnalogValues( IPAddress targetIp, unsigned int targetPort )
{
  int index = 0;
  for(int i=0; i < 6; i++)
  {
     int value = analogRead(i);
      Serial.println(value);
     packetBuffer[index++] = lowByte(value);  // the low byte);
     packetBuffer[index++] = highByte(value); // the high byte); 
  }
  packetBuffer[12] = 0x78;// end of data
  //send a packet back to the sender
  Udp.beginPacket(targetIp, targetPort);
  Udp.write(packetBuffer);
  Udp.endPacket();
}

Thanks for any help.

As soon as the value drops below 255 and it has to send a zero the packet length of the data packet then shortens.

Does UDP::write() have an overload that lets you tell it how many elements in the array? You really should not be using char as the array type. chars are signed. bytes are not. chars have a range of -128 to 127. bytes have a range of 0 to 256.

Does UDP::write() have an overload that lets you tell it how many elements in the array?

Yes, it does. It writes zeros and all.

Udp.write(packetBuffer,12);

It is another undocumented function. From EthernetUdp.h

  // Write size bytes from buffer into the packet
  virtual size_t write(const uint8_t *buffer, size_t size);

Yes, it does.

Thanks for spoiling the surprise. 8)

PaulS:

Yes, it does.

Thanks for spoiling the surprise. 8)

Me sorry! 8)

I'll submit that as a change also.

I'll submit that as a change also.

Good idea. And, thank you.

I wanted OP to learn that documentation only goes so far. Sometimes, it's necessary to look at the library source code and/or header file. Sometimes, there are useful clues there. Even something as simple as knowing what class a class derives from. Often, inherited methods are not documented, but that certainly doesn't mean that they aren't available.

I agree. Didn't mean to spoil the surprise, but that is a useful addition to the reference docs, and it should have been there already. It is the only way to send zero bytes in the UDP packet.