UDP Parsing text in Arduino (with Ethernet shield) from Processing

Hello there,
I have looked for this issue over the Forum and Google, but I couldn't find anything suitable to my needs.
It is something pretty simple, I know, only problem is that I don't know how to make it to work>

I have Processing sending strings to Arduino via Ethernet with the Udp library.
So Processing at the moment is sending 4 groups of numbers (ints) as strings.
Each values looks pretty much like P10, Q36, R53, S8 (having one single number reads well in Processing but comes with an added '9' in Arduino).

if (n <= 0) {
    fetchData();
    lastTime = millis();
    String ip = "192.168.1.72"; // the remote IP address
    int port = 8888;        // the destination port
    String cone="P"+categ1;
    println(cone);
    udp.send(cone, ip, port );   // the message to send
    myDelay(250);

    String ctwo="Q"+c2;
    println(ctwo);
    udp.send(ctwo, ip, port );   // the message to send
    myDelay(250);
    
    String cthree="R"+c3;
    println(cthree);
    udp.send(cthree, ip, port );   // the message to send
    myDelay(250);
    
    String cfour="S"+categ4;
    println(cfour);
    udp.send(cfour, ip, port );   // the message to send
    myDelay(250);
    }

this way every group of values starts with an identifier, which I sould be able to read back in Arduino to identify it.

Then in Arduino I receive the package with the following code

Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);

Now that I have the packetBuffer I would like to extract the values so that I can use them in Arduino.
I thought I could do that by building an array:

 char buff = 000;
for (int i=0; i<3; i++) {
      buff[i]=buff[i+1];
    }
    buff[3]=Serial.read();
    if (buff[0]=='P') {
      r1val=int(buff[3]);
    }
    if (buff[0]=='Q') {
      r2val=int(buff[3]); 
    }
    if (buff[0]=='R') {
      r3val=int(buff[3]); // reads the value coming from Processing related to category 3
    }

     if (buff[0]=='S') {
      r4val=int(buff[3]);

And then I will covert the string into an int.
But for now it is not working.
Can anybody spare some little help?

Many thanks

Best

F

Thanks

I am still learning parsing myself and have been messing with some UDP stuff.

I can tell you that you will have to do some converting from ASCII to int. you wont just be able to cast it like it appears you are trying to do.

Try to check out atoi()

Thanks for the suggestion, I am checking the atoi() function, it makes lot of sense.
the reason I am trying to use the array with the char is because I was able to make it to work through serial communication and I thought that doing it through Ethernet would have been just as easy.
But it isn't apparently :slight_smile:
I will dig into the atoi() function and will send an update as soon as I will have some success...
hopefully!

Okay, I thin I've figured that out thanks to this posting:
http://forums.adafruit.com/viewtopic.php?f=25&t=25264

I used the second example.

Basically I added this on top of what I already had (I stopped using that char buff I had created):

#define numBufSize 3
char numStr[numBufSize] = { '0','1', '2' }; // just as an example

Then is setup I changed the baudrate to:

Serial.begin(115200);

this won't hopefully make a mess, I usually use 9600...

And finally

 // Convert decimal string in numStr to an unsigned integer in num.
  // The string is in a fixed size buffer.
  // There is no checking for invalid digit characters or number too large.
  unsigned long num = 0;
  for (int i = 0; i < numBufSize; i++) {
    num = num * 10 + (packetBuffer[i] - '0');
  }
  
//  Serial.println(num);
  
      if (packetBuffer[0]=='P') {
      r1val=num - 3200;
    }
    if (packetBuffer[0]=='Q') {
      r2val=num - 3300; 
    }
    if (packetBuffer[0]=='R') {
      r3val=num - 3400; // reads the value coming from Processing related to category 3
    }

     if (packetBuffer[0]=='S') {
      r4val=num - 3500;
      
    }

Now, I know this isn't the cleanest code of all, but for now it does the job.
Just in case it might be helpful for someone else.
So, everything works, except for those values which only have one unit (1, 2, 3, 4 etc to 9), they become (19, 29, 39.... 99)
Does anyone know what is happening in the UDP translation of the values from Processing.

You might also find some useful information with sscanf()
http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

Thanks!
I've found it by accident indeed, looks like a very good tool!
Have you tried it already?

Very briefly. Have had a couple other projects going recently so i haven't been able to mess much with my UDP ethernet stuff.