View previous topic :: View next topic |
Author |
Message |
filjoa
Joined: 04 May 2008 Posts: 260
|
string with case function |
Posted: Thu Jan 21, 2010 11:39 am |
|
|
Hi
Is it possible to use a string with case function?
I have this example:
Code: |
#int_rda
void rda_isr(void)
{
char c[10];
//c=getc();
c=fgetc();
switch(c)
{
case '1':
output_high(PIN_B7);
break;
case '2':
output_high(PIN_B6);
break;
case '3':
output_low(PIN_B7);
break;
case '4':
output_low(PIN_B6);
break;
case '99':
output_low(PIN_B5);
break;
}
}
|
With char c; and c=getc(); all work fine if I don't have "case '99'".
If I put "case '99'", it give an error. I try to define an array char c[10]; and c=fgets(); but don't work.
Someone can explain me how I can use case with more than one char?
best regards |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Thu Jan 21, 2010 12:00 pm |
|
|
Why does your variable c have to be an array? Why can't you just do this:
Code: |
#int_rda
void rda_isr(void)
{
char c;
c=fgetc();
switch statements....
|
Also you are using fgetc() instead of just getc(), did you specify the right stream in your #use rs232 statement? |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Thu Jan 21, 2010 12:08 pm |
|
|
You might want to look at gets() to get a string instead of fgetc() which only returns one character.
After that, maybe an atoi() to convert the string to an integer and do the switch on the integer. |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Jan 21, 2010 12:40 pm |
|
|
The switch statement can only evaluate a 16 bit number (65535 to -32768). Anything that is larger than 16 bits will not work. Converting a string to an integer would, most likely, end up with a result that is larger than the switch can handle.
Ronald |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Thu Jan 21, 2010 1:16 pm |
|
|
hi
mkuang wrote: | Why does your variable c have to be an array? Why can't you just do this:
Code: |
#int_rda
void rda_isr(void)
{
char c;
c=fgetc();
switch statements....
|
Also you are using fgetc() instead of just getc(), did you specify the right stream in your #use rs232 statement? |
I try this and it don't work compiler give me an error on "case '99' "
pmuldoon
when I write on terminal I write an char don't an integer when I write 99 it is an string, I only need witch case function understand this.
rnielsen
'99' don't have more than 16bits correct?
my problem is, why I don't save on "c" variavel an string to use on case function.
thanks for help me!!!
best regards |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Thu Jan 21, 2010 3:37 pm |
|
|
When you're trying to have '1' as one of your case statements, '1' is a character constant. A character constant is ONE character like: 'a' or 'A' or '1' or 'H' and so on. When you try to bundle '99' together, it is not a valid character. Using single quotes creates ONE character. '99' is not a valid character. You can have '0' '1'..... 'a' 'b'..... 'A' 'B'.... and so on up to 'Z'. These are all character constants. "99" is a String but the compiler will not accept that either since it is not a Constant.
If you input characters from the serial port and then convert them to an integer (atoi) then you can use the value it is converted to in your switch() as long as you make sure the converted value will not exceed the maximum allowed.
Ronald |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Thu Jan 21, 2010 5:27 pm |
|
|
Hi
Thanks Ronald
You suggest some like that?
Code: |
#int_rda
void rda_isr(void)
{
char string[10];
int c;
gets(string);
c = atoi(string);
switch(c)
{
case 1 :
output_high(PIN_B7);
break;
case 2 :
output_high(PIN_B6);
break;
case 3 :
output_low(PIN_B7);
break;
case 4 :
output_low(PIN_B6);
break;
case 99 :
output_low(PIN_B5);
break;
}
}
|
when I write on terminal 1 or 2 or 99 any appens...
where is the problem now?
Best regards |
|
|
filjoa
Joined: 04 May 2008 Posts: 260
|
|
Posted: Fri Jan 22, 2010 12:01 pm |
|
|
hi
Anyone have an idea why this don't work?
best regards |
|
|
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Fri Jan 22, 2010 1:28 pm |
|
|
First, int_rda is should be used to receive ONE character at a time. You are trying to read an entire String which does not work. Every time a character is received an interrupt will cause int_rda to be entered. Use getc() to capture that character and then move it to your string array, while keeping track of where you placed the last character. When a 'return' is entered on the keyboard have your ISR set a flag and then do your atoi().
Remember, read ONE character with int_rda, not an entire string. Make sure you have ERRORS in your #use rs232() statement too. This will make sure your ISR does not hang your program.
Ronald |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Fri Sep 07, 2012 8:37 am |
|
|
That's because '99' is not a 'character'. When you put something in between single quotes, the compiler reverts internally to the ASCII value. '99' is not valid. 'C' is ASCII decimal 67 or hex 0x43 but '99' is nothing. ASCII value 99 is letter 'c' BUT you either put
case 'c':
break;
OR
case 99:
break;
NOT
case '99':
break.
Another example, if you want to check if the character is '?', you'd litterally put case '?' but NOT case ? as this would not work.
You're evaluating a character therefore you either pass the actual character in single quotes OR you put the numerical ASCII value representation:
case '?':
break;
OR
case 63:
break;
It's the same thing, both equate to a question mark, but between single quotes it's the actual character as opposed to 63, it's the numerical ASCII value of the character. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19549
|
|
Posted: Fri Sep 07, 2012 9:22 am |
|
|
Also, on why the atoi doesn't work. You need to assemble a _string_. In C, a string is a sequence of _null terminated_ characters.
Look at how EX_SISR receives characters.
Add one thing to this. A test if the character received is a line feed. Set a global flag when this happens.
Then in your _main_ code, when the flag is set saying 'line feed received', put the characters from the buffer into an array. when you see the line feed, instead of copying this, put a NULL (0) character into the array. You now have an array of characters, with the null terminator, and atoi, can accept this, and you can perform the switch.
Best Wishes |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Sep 07, 2012 9:50 am |
|
|
Great... benoitstjean bumped a 2.5 years old thread. |
|
|
|