Go Down

Topic: Sending commads and parameters via UDP (Read 693 times) previous topic - next topic

Lyncos

Hi,
    I'm trying to figure out how to implement a pretty simple protocol for sending commands + parameters to an arduino board via UDP datagrams.

The part where I send UDP packets is working fine ... My arduino receive the data... But where I need help is to interpret these commands+params.

At the moment my protocol is simple and may become more complex in the future as I wand to be able to add checksum and stuff like that.

For the moment it's like that:

first 3 character is the function number
All the other character excluding the 3 first ones is the parameter

simple example:

001test    will print test to the LCD ...


This 'sort of protocol' works well when I have  a command and 1 parameter.. but if I want to send more parameter for the same command or if I would like to add checksumming it start to be more difficult..


here is my actual code:


Code: [Select]
// Check if new UDP packet arrived

  int packetSize = Udp.parsePacket();
  if(packetSize)
  {
    Serial.print("[DEBUG] Received packet: [");
    Serial.print(packetSize);
    Serial.print("] From: ");
   
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
         Serial.print(".");
      }
    }
   
    Serial.print("  port:");
    Serial.print(Udp.remotePort());

    Udp.read(packetBuffer,packetSize);

    Serial.print(" Content:");
    Serial.println(packetBuffer);

  // read packet
    Serial.println("[DEBUG] Looping packet start");
    char command[4];
    char param[packetSize -2];
    int index=0;
    for (int i = 0; i < packetSize; i++)
    {
      if (i < 3)
      {
        command[i] = packetBuffer[i];
        command[i+1] = NULL;
      }
      else
      {
        param[i-3] = packetBuffer[i];
        param[i-2] = NULL;
      }
    }
 
   Serial.print("Command:");
   Serial.println(command);
   Serial.print("Param:");
   Serial.println(param);
 
   Serial.println("[DEBUG] Looping packet end");



I would like to have more comments before I continue just to see if there is better way to do it...

I thought about using the cmdMessenger library but I never been able to make it work with UDP messages.

All idea are welcome !

Thanks all

PaulS

My first thought is that when I type a response, it does not look like this:
MyfirstthoughtisthatwhenItypearesponseitdoesnotlooklikethis:

So, then I wonder why you jam stuff together. Much simpler to parse things when there are delimiters.

If you were sending stuff like:
001 test string for the LCD
you would easily parse the arguments. That's what the strtok() function is made for.

Lyncos

Thanks PaulS,

I didn't know about the strtok() function, I'm going to read about it right now and see how it could help me.

I'll post my findings when I've mastered strtok function :-)

Thanks for your quick answer


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy