1

At the moment I have an Arduino board with an Ethernet Shield connected to a router. My computer connects to this router via Wi-Fi. My board and my computer send UDP messages back and forth to each other. My computer is a client, and the board is a server. However I noticed, that when I send a longer UDP message from my computer, and then a shorter UDP message, the Arduino accepts the shorter message, then followed by remaining bits from the longer message.

For instance: if I send "Hello World" from my computer, followed with "Test"; the Arduino will not read the second message as "Test", but rather: "Testo World".

I thought perhaps in was a problem from the Arduino end first. The Arduino stores the messages temporarily in an array called packetBuffer. I tried clearing this buffer before I receive a new message each time. The buffer would clear, but then I would receive the faulty message again.

So I assume the culprit is the computer, the client. On the computer end I have a processing sketch that sends the UDP messages. The example below is not the sketch itself; however it is by far a simpler example that still provides the exact symptoms as I described with my original sketch.

import hypermedia.net.*;

UDP udp;  // define the UDP object


void setup() {
    udp = new UDP( this, 6000 );  // Create a new datagram connection on port 6000
    //udp.log( true );            // <-- printout the connection activity
    udp.listen( true );           // and wait for incoming message
}


void keyPressed() {
    String IPaddress  = "192.168.1.177"; // The remote IP address
    int port          = 8888;  // The destination port
    if (keyCode == UP)
    {
        udp.send("Test", IPaddress, port );
    }
    else
        if (keyCode == DOWN)
        {
            udp.send("Hello World", IPaddress, port );
        }
}

void receive( byte[] data ) {    // <-- default handler
    //void receive( byte[] data, String IPaddress, int port ) { // <-- extended handler
    for(int i=0; i < data.length; i++)
        print(char(data[i]));
    println();
}

How could I get the sketch to send the right messages?

Of course I am more than willing to provide more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nick Williams
  • 253
  • 2
  • 12
  • 2
    I would start by using Wireshark to see if the message is being transmitted wrong. If the transmission is OK, it must be at the receiving side. – Martin Thompson Jul 19 '12 at 09:13
  • Good suggestion; surprisingly to me, wireshark shows that the messages are not faulty being sent from the computer. Perhaps my problem is with the Arduino. Thanks for that! – Nick Williams Jul 20 '12 at 01:14
  • Can you show the code for your UDP class? – Martin Thompson Jul 20 '12 at 10:09
  • Does that mean WireShark show that two different messages are sent? – Independent Jul 21 '12 at 06:40
  • Wireshark will show one message per send. If I first sent "Hello World"; wireshark will show that it sent. If I send "Test" after that; wireshark will show just "Test", not "Testo World". For the UDP class, are you referring to the code for the receiving end: the Arduino or the imported UDP library? – Nick Williams Jul 21 '12 at 15:41

2 Answers2

1

There wasn't a direct solution to this problem; so I ended up resorting to a work around. The work around involves dynamically adding zeros to all strings sent to the Arduino so there is always 10 characters sent. For instance:

If I am to send "Hello Bot", the actual string sent is "Hello Bot0". If I sent an additional message like "Test" after that, the string sent to the Arduino would be "Test000000". The additional zeros would cover up the overlapping characters. One problem with this work around is that I had to prepare the Arduino to accept the zeros also. This work around is also kind of messy for the code. It does work though.

Here's a snippet of code from the computer (client) side. The Arduino code obviously just had to be adjusted to account for the zeros.

public void Send() {      //bang button named "Send" activates function           
  String txtSend = comField.getText();   //Grab text from a textbox to be sent
  int txtSendLength = txtSend.length();   
  for(int i = 0; i < 10-txtSendLength; i++){  //Add zeros until it has 10 char
    txtSend = txtSend + "0";                
  }
  udp.send(txtSend, ip, port);
  comField.clear();             //Clear the textbox
}

Behold, the very simple and crude solution!

Nick Williams
  • 253
  • 2
  • 12
0

I believe your issue is with properly clearing the buffer. I found a line of code that goes through and clears each character, since it is a character array. There is theoretically no buffer to clear once it is read. Use:

for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;

Once you read the data, and that will clear the array. I also found out that when trying to do logic on the data that was received, in order to control some output, I needed to first convert the packetBuffer to a string. After that, all seemed to work correctly. Hope that help.