CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

string with case function

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
filjoa



Joined: 04 May 2008
Posts: 260

View user's profile Send private message

string with case function
PostPosted: Thu Jan 21, 2010 11:39 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 21, 2010 12:00 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 21, 2010 12:08 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 21, 2010 12:40 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 21, 2010 1:16 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 21, 2010 3:37 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jan 21, 2010 5:27 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Jan 22, 2010 12:01 pm     Reply with quote

hi

Anyone have an idea why this don't work?

best regards
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Jan 22, 2010 1:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 8:37 am     Reply with quote

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: 19474

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 9:22 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 9:50 am     Reply with quote

Great... benoitstjean bumped a 2.5 years old thread.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group