UDPsendreceivestring adds previous message letters

All Knowing Forum!

I am using the standard UDP send receive string sketch and I have these problems-

Problem#1
I have UDP tool on my Iphone and succesfully SENT a message to the Arduino set up… but I could not use the Serial Monitor to SEND a message back to the Iphone.

Problem #2
The next test message I sent FROM the Iphone was shorter than the first message, and it left on the letters from the first message??? TEST was the first message and JOY was the second message, so it displayed on the Serial Monitor as JOYT…

I tested it several more times and it always did the same thing, I could correctly send longer messages, but when I went to a shorter message, it always left on the extra letters from the previous message!

(all working on my LAN{arduino hardwired by computer to USB com to Arduino/EthernetSD to Router} and the Phone by WiFi tether to my Network)

The goal is to send UDP text messages back and forth from the Iphone UDP tool to my computer.

ANY HELP?

THANKS!

/*
  UDPSendReceive.pde:
 This sketch receives UDP message strings, prints them to the serial port
 and sends an "acknowledge" string back to the sender
 
 A Processing sketch is included at the end of file that can be used to send 
 and received messages for testing with a computer.
 
 created 21 Aug 2010
 by Michael Margolis
 
 This code is in the public domain.
 */


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <Udp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xa2, 0xda, 0x00, 0x1d, 0xb8 };
byte ip[] = { 10, 10 , 10 , 122 };

unsigned int localPort = 8888;      // local port to listen on

// the next two variables are set when a packet is received
byte remoteIp[4];        // holds received packet's originating IP
unsigned int remotePort; // holds received packet's originating port

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back


void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);

  Serial.begin(9600);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.available(); // note that this includes the UDP header
  if(packetSize)
  {
    packetSize = packetSize - 8;      // subtract the 8 byte header
    Serial.print("Received packet of size ");
    Serial.println(packetSize);

    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

    Udp.sendPacket( ReplyBuffer, remoteIp, remotePort);
  }
  delay(10);
}

I have UDP tool on my Iphone and succesfully SENT a message to the Arduino set up... but I could not use the Serial Monitor to SEND a message back to the Iphone.

Of course not, the iPhone is not connected to the serial port. The Arduino is.

The next test message I sent FROM the Iphone was shorter than the first message, and it left on the letters from the first message??? TEST was the first message and JOY was the second message, so it displayed on the Serial Monitor as JOYT...

You know how many characters are supposed to be in packetBuffer (packetSize). Why are you passing a larger value to Udp.readPacket()? My guess is that the NULL is being stored in packetBuffer[UDP_TX_PACKET_MAX_SIZE], which is out of bounds, instead of in packetBuffer[packetSize].

To make a character array look like a null-terminated string you have to supply the null terminator:

// read the packet into packetBufffer and get the senders IP addr and port number
Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, remoteIp, remotePort);

Serial.println("Contents:");

packetBuffer[packetSize] = '\0';
Serial.println(packetBuffer);

Thank you John Wasser- I did a clear buffer line as a temp fix but I like your solution better... it is the proper way. 8)