View previous topic :: View next topic |
Author |
Message |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
very dumb question... Solved |
Posted: Sat Oct 22, 2011 9:22 am |
|
|
hi,
I have a serial device that ouputs via RS232 numbers 0-9, *, #
(serial keypad).
Code: |
IF(var=='#'){blah;}
|
# is a special character... how do I make the above comparison?
What is the special character I should place before # so that its take as a value and not as if were trying to do a #define....
Code: |
IF(var=='\#'){blah;}
|
like above? I had a similar problem trying to print (") .... where I had to use \"
Sorry for the dumb question...
thanks. _________________ CCS PCM 5.078 & CCS PCH 5.093
Last edited by Gabriel on Mon Oct 24, 2011 7:09 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Oct 22, 2011 9:52 am |
|
|
'#' doesn't need to be escaped, neither in printf format strings nor other character and string literals. |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9178 Location: Greensville,Ontario
|
|
Posted: Sat Oct 22, 2011 10:09 am |
|
|
Nothing 'special' about the # key.
Since your device outputs the keypresses as serial, just use a 'terminal program'(I use Realterm) to show you what the serial keyboard is sending, vis RS232, for the keypresses.Be sure to get the baud,parity,stops bits correct !
If it's a straight ASCII lookup table then your device should send a decimal 35 ( hex 23) for the # key.
There are NO 'carved in stone' formats for serial keyboards so you should decode each keypress as I've stated above. |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sun Oct 23, 2011 5:31 pm |
|
|
short summary:
If you know the ASCII code for the 8 bit char you want to compare
it is just that 8 bit unsigned number compared to the var.
I suggest the VAR be either byte or unsigned int8
otherwise you need to cast the value.
Code: |
if ( var==ascii_code ) { action(); etc(); }
|
|
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Oct 24, 2011 6:55 am |
|
|
hey all, thanks for your replies...
yeah.. i agree, the ascii code should work fine.... Duh!
however, the # character when used as:
Code: | IF(var=='#'){blah;} |
does give me an error, and shows up in Green like when used in a #device statement.... maybe im missing somthing...
thanks for your help....i cant believe i didnt think about using the hex value of #.....
sad thing is i posted last week a comment to some other guy related to the interpretation of characters in terminals.... i should sleep,
thank you all...
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Oct 24, 2011 9:41 am |
|
|
is best to assume
that #, _
and possibly other reserved characters used as compiler directive prefix
symbols could share the same fate
HEX or decimal constants are always safe tho ;-)) |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19383
|
|
Posted: Tue Oct 25, 2011 2:26 am |
|
|
Gabriel wrote: | hey all, thanks for your replies...
yeah.. i agree, the ascii code should work fine.... Duh!
however, the # character when used as:
Code: | IF(var=='#'){blah;} |
does give me an error, and shows up in Green like when used in a #device statement.... maybe im missing somthing...
thanks for your help....i cant believe i didnt think about using the hex value of #.....
sad thing is i posted last week a comment to some other guy related to the interpretation of characters in terminals.... i should sleep,
thank you all...
G |
Just took the experiment, of trying this, with a crude test program.
Tested with 3.249, 4.099, 4.118. None complained.
Code: |
//With header and RS232 setup:
void main(void) {
int8 kval;
do {
if (kbhit()) {
kval=getc();
if (kval=='#')
putc('M');
else
putc('n');
} while(TRUE);
}
|
One 'reason', would be if there was something a line or two earlier, that is looking for the '#'. You _may_ be hitting the old CCS problem of not seeing an earlier syntax error, that then makes a latter line complain, and it just happens to be the #, that is triggering this. Another possibility is that 'IF' in upper case, is the preprocessor form, and the compiler will complain about this if #case is selected.
So though using a hex value has solved the problem, you might want to look at your syntax round the area with a fine tooth comb.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Tue Oct 25, 2011 6:43 am |
|
|
Hi!... thanks for diggin deeper...
ill check it out tonight when i get home, and post the code tomorrow morning as well...or my findings... which ever comes first...
Thanks!
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
![](templates/subSilver/images/spacer.gif) |
|