0
Offline
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« on: January 06, 2011, 07:24:39 pm » |
Hi I am trying to parse some incoming data with a switch case command with the following code: void parseReceivedText() { switch (textBuff[8]) { case 'printf_ip' : IPPrint(); break; case 'poe_resta' : PoeRestart(); break; case 'cha_dname' : ChangeDeviceName(); break; case 'change_ip' : ChangeIP(); break; case 'change_gw' : ChangeGW(); break; case 'change_sm' : ChangeSM(); break; case 'rfinished' : ReadyToContinue(); break; case 'adminlock' : AdminLockdown(); break; case 'adminover' : AdminOverride(); break; case 'term_teln' : telnet_session.stop(); break; case 0x0d : break; default : telnet_session.println("invalid command"); break; } } But I get a duplicate case error and since i have no idea what the error means i don't even know what to look for. anybody have ideas that could help with this? thanks
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5160
CMiYC
|
 |
« Reply #1 on: January 06, 2011, 07:47:22 pm » |
Duplicate Case Error means you have defined two cases with the same value in the switch statement. You are probably looking at your code thinking "but they are all different." To you they are different. To the complier they look much different. You have defined case statements using the character notation. Single quotes are meant for characters, not strings. Without seeing how you defined textBuff[], I could be wrong about the following: you are probably only comparing one character from textBuff[] (whatever character is at element #8) to whatever constant value the complier generates for the "junk" inside of the single quotes. Read up on strings. http://arduino.cc/en/Reference/String
|
|
|
|
« Last Edit: January 06, 2011, 07:48:17 pm by cmiyc »
|
Logged
|
|
|
|
|
0
Offline
Jr. Member
Karma: 0
Posts: 92
|
 |
« Reply #2 on: January 06, 2011, 07:51:15 pm » |
To add to the above. Its hard to compare strings in a switch case statement. the only way to do it that I know of is harder and messier than just using if else.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #3 on: January 06, 2011, 07:58:10 pm » |
oh i see, so if i had a different character at the end of each string then there won't be an duplicate case errors
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #4 on: January 06, 2011, 08:00:39 pm » |
i changed some stuff in my code (made sure the 9th character was different for each case) and all is well now, thanks alot guys. i learn something new everyday from this forum
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #5 on: January 06, 2011, 08:01:30 pm » |
so if i had a different character at the end of each string then there won't be an duplicate case errors The point that you seemed to have missed is that cases needed to be constant integer or character values, NOT string values.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #6 on: January 06, 2011, 08:25:42 pm » |
do they have to be though? it seems that with string values the commands can make more sense to the user
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #7 on: January 06, 2011, 08:35:33 pm » |
do they have to be though? Yes. It's not my rule, but it is THE rule. if(strcmp(textBuff, "printf_ip") == 0) IPPrint(); else if(strcmp(textBuff, "poe_resta") == 0) PoeRestart(); else if(strcmp(textBuff, "cha_dname") == 0) ChangeDeviceName(); This is really not any more difficult than a switch statement, and takes no more room, and isn't much more difficult to read.
|
|
|
|
« Last Edit: January 06, 2011, 08:36:19 pm by PaulS »
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5160
CMiYC
|
 |
« Reply #8 on: January 06, 2011, 09:33:00 pm » |
it seems that with string values the commands can make more sense to the user Depends on what you mean by user. The Arduino isn't human. Using integers, #defines, constants, comments, etc you can make the code more readable. At the same time, you can use those same elements to create a user interface that makes sense to both the user and the Arduino.
|
|
|
|
|
Logged
|
|
|
|
|
Phoenix, Arizona USA
Offline
Faraday Member
Karma: 27
Posts: 5075
Where's the beer?
|
 |
« Reply #9 on: January 06, 2011, 09:44:30 pm » |
This is really not any more difficult than a switch statement, and takes no more room, and isn't much more difficult to read. I think the following would be "equivalent" (haven't tried compiling it, tho): switch (0) { case strcmp(textBuff, "printf_ip"): IPPrint(); break; case strcmp(textBuff, "poe_resta"): PoeRestart(); break; case strcmp(textBuff, "cha_dname"): ChangeDeviceName(); break; default: } It could probably be simplified/cleaned up with a #define or some other manner... :-?
|
|
|
|
« Last Edit: January 06, 2011, 09:45:45 pm by keeper63@cox.net »
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 0
Posts: 122
Arduino rocks
|
 |
« Reply #10 on: January 06, 2011, 09:48:19 pm » |
well I will try all those options after classes tomorrow and post how it goes
also, will the whole command typed it be put into the textbuff then?
thanks alot
|
|
|
|
|
Logged
|
|
|
|
|
Austin, TX
Offline
Faraday Member
Karma: 41
Posts: 5160
CMiYC
|
 |
« Reply #11 on: January 06, 2011, 09:56:44 pm » |
@cr0sh, You can't make function calls for a case statement. The complier is expecting constants.
|
|
|
|
|
Logged
|
|
|
|
|
Phoenix, Arizona USA
Offline
Faraday Member
Karma: 27
Posts: 5075
Where's the beer?
|
 |
« Reply #12 on: January 07, 2011, 12:38:02 am » |
@cr0sh, You can't make function calls for a case statement. The complier is expecting constants. Ah, you're right; just tried it in the IDE. It's too bad that wouldn't work (I think it would work in PHP - not sure). The logic I think is sound, at least... ;D Oh well... :-/
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #13 on: January 07, 2011, 07:57:17 am » |
It's too bad that wouldn't work Even if it had "worked", strcmp returns either -1, 0, or +1, depending on the lexigraphical order of the strings. So, you'd be limited to 3 cases.
|
|
|
|
|
Logged
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 86
Posts: 9355
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #14 on: January 07, 2011, 08:43:02 am » |
You can use the Enum type in a switch as it is an integer type under the hood - http://www.arduino.cc/playground/Code/Enumenum country { FRANCE, ENGLAND, ITALY, UNKNOWN } country_var = FRANCE; switch (country_var) { case FRANCE: ... break case ENGLAND: ... etc
|
|
|
|
|
Logged
|
|
|
|
|
|