I wanna turn to diverse functional entry acording to the received datas. The datas received from the serial port are at the length of 2 bytes . so I can't simply use Switch sentence. How can I make it?

a map of function pointers using the received bytes (turned into a string maybe) as a key would work nicely.

If the data received is 2 bytes, why can't you use a switch?

#define SWITCH_DATA( L, R ) ((L << 8) | R)

switch (SWITCH_DATA( firstByte, secondByte ))

case SWITCH_DATA( 'A', 'B' ):
<do stuff for the "AB" command>
break;

and so on. Make sure the data is unsigned char, not just char, otherwise bytes >= 128 will cause trouble with sign extension.

a map of function pointers using the received bytes (turned into a string maybe) as a key would work nicely.

but my gcc complier seems not support the string type ,when I transform the two bytes data to a string and then use switch sentence, the complier says:"switch quantity is not an integer constant" ,"case label does not reduce to an integer constant"

If the data received is 2 bytes, why can't you use a switch?

#define SWITCH_DATA( L, R ) ((L << 8) | R)

switch (SWITCH_DATA( firstByte, secondByte ))

case SWITCH_DATA( 'A', 'B' ):
<do stuff for the "AB" command>
break;

and so on. Make sure the data is unsigned char, not just char, otherwise bytes >= 128 will cause trouble with sign extension.

I get even more confused. Would you please show me more examples.what should be the type of L and R. I mean (L<<8),it will clean data L.

I use redhat linux 9.0 to complie the program, and seems switch sentence does not support diverse types

This article has been dead for over six months. Start a new discussion instead.