Switch String

Hey,

I’m making a UDP connection with labview and arduino.
On my arduino i like to use a case structure for the received data of my UDP message, this UDP message is a Char array. (same ass string)

if i write my case structe:

      switch(UDP_ontvangDAta)
      {
        case UDP_command_Aanwezig
           UDP_beginPacket();
           UDP_add2packet(IPinterface);
           UDP_add2packet(';');
           UDP_add2packet(poortInterface);
           UDP_add2packet(';');
           break; 
        
      }

i get an error: switch quantity not an intger.

how can i make an case structure whit a char arry or String?

thnx for the reactions

You can't, basically. Either convert each possible message into an integer in advance, or just do if ... else and so on.

really?? that suck big time :D

but fortunately you can do magic in software [you know magic is a trick]

this code I wrote last year as a demo for someone you can do nice tricks with the C-preprocessor

//
//    FILE: strswitch.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-03-31
//
// PUPROSE: demo 
//

#define STRSWITCH(STR)      char _x[16]; strcpy(_x, STR); if (false) 
#define STRCASE(STR)        } else if (strcmp(_x, STR)==0){
#define STRDEFAULT          } else {

void setup()
{
  Serial.begin(9600);
  char x[10];
  strcpy(x, "BEER"); 

  STRSWITCH(x)
  {
    STRCASE ("NOT") 
      Serial.println(1,DEC); 
      Serial.println(1,DEC); 
    STRCASE ("APE") 
      Serial.println(2,DEC);
      Serial.println(2,DEC);
    STRCASE ("BEER") 
      Serial.println(3,DEC);
    STRCASE ("WINE") 
      Serial.println(4,DEC);
    STRDEFAULT 
      Serial.println("not in the list");
  }
}

void loop()
{
};

Note that the STRDEFAULT should be the last in the list.

Furthermore this version does not include BREAK and fall through mechanisms, that can be done => you need to define STRSWITCH as a for loop and use BREAK=break to exit the loop, left as an exercise ;)

And yes it is trick coding and I would not recommend to use it in a commercial / serious project