UDP buffer problem

Hi Everyone
This is driving me mad....I am using UDP but i cant seem to clear the buffer. If i send "Rob" a can read it using something like buffer[0] to get the 'R' no problem but if i send "Pete" and read it still says 'R' instead of 'P' i've tried
for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer = 0;
Doesnt seem to work.
Please help

Please help

Please post your code.

Hi, Thank you for your time. Code below

#include <EthernetUdp.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
//IPAddress ip(217, 36, 45, 231);
IPAddress ip(192,168,1,200);
unsigned int localport = 55555;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

EthernetUDP Udp;

void setup(){
Ethernet.begin(mac,ip);
Udp.begin(localport);
Serial.begin(9600);
pinMode(13, OUTPUT);

}

void loop() {
int packetSize = Udp.parsePacket();
if(packetSize) {
packetSize = packetSize - 8; // subtract the 8 byte header
//Serial.print("Received packet of size ");
//Serial.println(packetSize);

Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
if(packetBuffer[0] == 'F' && packetBuffer[1] =='u'){
Serial.println("Message:");
Serial.print(packetBuffer[0]);
Serial.println(packetBuffer[1]);
digitalWrite(13, HIGH);
for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer = 0;

  • }*
  • }*
    // Serial.println("Buffer clear");
    //Serial.println(packetBuffer);
  • delay(100);*
    }
if(packetSize)

If packetSize is true? You have a value for the packet size. Presumably, you want that value to be greater than some value. That greater than should be explicitly stated.

    Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);

You are now asking to read more data from that packet than the packet contains. Why?

    if(packetBuffer[0] == 'F' && packetBuffer[1] =='u'){
      Serial.println("Message:");
      Serial.print(packetBuffer[0]);
      Serial.println(packetBuffer[1]);

If the first value is 'F' and the second value is 'u', print those values. Otherwise, don't print anything.

I don't see how this relates to the packet containing "Rob" or "Pete" or anything.

Some Serial.print() statements when the if part is not not true might provide some clues. Printing the whole packet read might, too.

Thank you
The "Rob" "pete" were just examples psuedo.
I'll try what you suggest.
I have printed the whole packet and it does contain loads of junk..why? or is that ok?

Sorry, i am new to this.

I have printed the whole packet and it does contain loads of junk..why? or is that ok?

Can't answer that without seeing the whole packet received, and knowing how that compares to what was sent. Possibly, the part of the array after the packetSize value is where the junk is, and that would be OK.

Hi
Thank you. I am sending the data from my android phone using a app for testing UDP
There is a lot of odd chars in the packet. I dont really see how.
I thought that if you sent 'Fu' then all that was received was 'Fu' am i wrong?

I thought that if you sent 'Fu' then all that was received was 'Fu' am i wrong?

No, you are right. But, when you ask Udp.read() to fill packetBuffer with UDP_TX_PACKET_MAX_SIZE characters, you are going to get UDP_TX_PACKET_MAX_SIZE characters in packetBuffer. Some of them will be garbage.

If, instead, you asked Udp.read() to fill the buffer with packetSize characters, you'd get a lot less garbage.

I see, i'll implement that and see if it helps.
I'm still not able to get rid of the existing chars.
If i send 'Fu' the condition is met .... thats fine, however if i send 'Rb' the buffer still seems to contain the 'Fu'
Thanks again for your help

I'm still not able to get rid of the existing chars.

It's not that you are not getting rid of them. It's that after you initialize the array, you are asking Udp.read() to read more than is available, so it goes and finds some junk to pad the array with.

Hi
Now packetSize returns this...
1264?¨J?

Any ideas?
Ta

Any ideas?

The usual one. Post your current code and all your serial output.

Heres the code with a few of the mods you suggested..

#include <EthernetUdp.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
//IPAddress ip(217, 36, 45, 231);
IPAddress ip(192,168,1,200);
unsigned int localport = 55555;

char packetBuffer[UDP_TX_PACKET_MAX_SIZE];

EthernetUDP Udp;

void setup(){
   Ethernet.begin(mac,ip);
   Udp.begin(localport);
   Serial.begin(9600);
   pinMode(13, OUTPUT);
  
}

void loop() {
  int packetSize = Udp.parsePacket();
  if(packetSize) {
    packetSize = packetSize - 8; // subtract the 8 byte header
    Serial.println(packetSize);
    
    Udp.read(packetBuffer, packetSize);
    if(packetBuffer[0] == 'F' && packetBuffer[1] =='u'){
      Serial.println("Message:");
      Serial.print(packetBuffer[0]);
      Serial.println(packetBuffer[1]);
      digitalWrite(13, HIGH);
      for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;
    }
  }
  delay(10);
}

Heres the output

1264?¨J?1264?¨J?1264?¨J?1264?¨J?1264?¨J?

Thanks again

It appears that the only Serial.print() that ever gets executed is the Serial.print(packetSize) statement. But, the strange symbols don't make sense when printing an int.

I'd suggest changing that one statement to:

Serial.print("Packet size: [");
Serial.print(packetSize);
Serial.println("]");

The Udp.read() method returns a value - the number of bytes read. It would be interesting to know that value.

You are assuming that there are 8 bytes in the packet that form a header, according to the comments, so assuming that the data is in the first few bytes doesn't make sense.

Heres the outcome. Seems like something is going wrong here...

void loop() {
  int packetSize = Udp.parsePacket();
  if(packetSize) {
    //packetSize = packetSize - 8; // subtract the 8 byte header
    //Serial.println(packetSize);
    Serial.print("Packet size: [");
    Serial.print(packetSize);
    Serial.println("]");
    
    Udp.read(packetBuffer, packetSize);
    if(packetBuffer[0] == 'F' && packetBuffer[1] =='u'){
      Serial.println("Message:");
      Serial.print(packetBuffer[0]);
      Serial.println(packetBuffer[1]);
      digitalWrite(13, HIGH);
      for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;
    }
  }

The out put is ...Pack?¨J?

So you can see only a small amount of the Serial.println statement is running???? confused

Does the behavior change if you add delay(500) at the end of loop()?

PaulS:
Does the behavior change if you add delay(500) at the end of loop()?

No its still the same. I also added some delays in other places to no avail. Could it be hardware?

Hi
Some progress... i used my Mega board and the println performs ok. However i dont think the Ethershield is now working ok on this board. Do i have to change any pin assignments?
Thanks