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

Compare string ?? return always false !!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
L.Belanger
Guest







Compare string ?? return always false !!
PostPosted: Thu Sep 20, 2007 2:57 am     Reply with quote

Dear CCS members,

I'm currently testing USB stuff ... just to start a simple program that i've made ... there's a problem with the get_string_usb() function...

Code:

 #include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use  standard_io(A)
#include <usb_cdc.h>


#int_rda

char string[4];

void flash()
   {
      disable_interrupts(INT_RDA);
     
      printf(usb_cdc_putc,"Starting Flashing\r\n");
     
      delay_ms(1000);
      Output_High(PIN_A1);
      delay_ms(1000);
      output_low(PIN_A1);

      printf(usb_cdc_putc,"Stopping Flashing\r\n");

      enable_interrupts(INT_RDA);
   }


void main(void) {


   usb_cdc_init();
   usb_init();

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

   while (TRUE) {
      usb_task();
         if (usb_cdc_kbhit())
               {
                  get_string_usb(string,4);

                  if (string == "1")
                    {
                     flash();
                     }

                     else{
                     printf(usb_cdc_putc,"You entered:%s\r\n",string);}
                     
               }
   }
}


the problem is that , if i wrote 1 in the hypertermina,, it always jump to ELSE statement ,,, tried get_int_usb function (to try grabbing a integer) and i got a not defined error ... i'm clueless ,,, how to get the led flashing in reponse to a commands from hyperterminal throught CCS USB to UART driver.

I'm using CCS 4.049

thx !
Ttelmah
Guest







PostPosted: Thu Sep 20, 2007 3:07 am     Reply with quote

Several comments.
1) You are enabling INT_RDA. Why?.
2) With this interrupt enabled, you have a supposed 'handler' header, but no code. If anything is received on the serial interrupt, this will hang the PIC.
3) You are trying to do a numeric compare, on a string. As it stands, you are comparing the low byte of the address of the string, with the value '1'... This is _not_ C syntax for a string compare. Look at the 'strcmp' function.
4) You are assigning 4 characters of storage for the 'string'. This is not enough. A 4 character 'string', requires _5_ characters of storage (one extra for the terminating '\0'). This will result in overflows.

'3', is the one that is catching you at present, but the others are waiting to get you.

Best Wishes
L.Belanger
Guest







PostPosted: Thu Sep 20, 2007 3:16 am     Reply with quote

1. Friend recommended to use interrupts (also clueless about interrupts ,, based on what i've saw on CCS forum)
2.same above
3. how the hell do you compare to a constant string ?? i mean you can't do if string = "yes" { do things} i use it often in php which is the same pratical syntax as C ... ?? i need clarification here

4.thx for the info ,, i've noted that ,, i though that only \r\n are the 2 caracters send after a caracter entered in a terminal (windows)

thx Roger Smile
SET



Joined: 15 Nov 2005
Posts: 161
Location: Glasgow, UK

View user's profile Send private message Send e-mail Visit poster's website

PostPosted: Thu Sep 20, 2007 7:24 am     Reply with quote

Quote:
3. how the hell do you compare to a constant string ?? i mean you can't do if string = "yes" { do things} i use it often in php which is the same pratical syntax as C ... ?? i need clarification here


No, php is 'C-like' - of course in C++ you could use = Smile
Anyway, strcmp is easy enough to use..
Ttelmah
Guest







PostPosted: Thu Sep 20, 2007 8:42 am     Reply with quote

Interrupts are _already_ being used. The USB handler, is completely interrupt based, and enables the ones it needs. INT_RDA, is for the hardware serial UART, which you are not using. You are enabling the interrupt for this, and if this interrupt triggers, it will only ever 'clear', if the character is read from the UART. You do not read such a character, so the chip would hang. Fortunately, since you don't have a #use RS232 statement, the UART does not get enabled. However it is an 'accident waiting to happen'...
The storage, is not to do with what is sent, but with what is _stored_. In 'C', a string is a simple sequence of bytes, _terminated with a 'zero'_. Now you are allowing 4 charaters to arrive, and the receive routine (since you are using a string receive routine), will _add_ the terminating zero, to make the stored data 'into' a string. With four characters received, the 'string' needs five characters.
You can compare in two ways:
First, manually compare the individual bytes. Simply:

if (string[0]=='1')

Here a ASCII character inside single inverted commas, is converted into the binary 'value' (in this case 0b00110001), and this is copared with te binary value stored in the first byte of the 'string'. Since this is a single character numeric comparison, this is fine.
The second way is to use the strcmp function, which is designed to compare whole strings.

Best Wishes
L.Belanger
Guest







PostPosted: Thu Sep 20, 2007 3:01 pm     Reply with quote

wow thanks for your replies,
everything work fine :D

+1 Ttelmah

just curious ,, if my string was declared as char string instead of string[6] (kinda like an array) i guess if (string =='nice') would work since its not splitted in 6 chars ... strcmp is used to compare a string which have been splitter in pieces hence string[6] compared to 'nice' ..

all strcmp does its splitting nice and comparing

a DOT denote a byte into string N. [0] I. [1] C. [2] E. [3]

ex: does N.I.C.E (4 individual char for string[6]) == 'N.I.C.E' (which i have entered 'nice' in strcmp ?

thank you
L.Belanger
Guest







PostPosted: Thu Sep 20, 2007 3:04 pm     Reply with quote

or in a simpler way to understand strcmp ...

if ( (string[0]=='n') && (string[1]=='i') && (string[2]=='c') && (string[3]=='e') )

{do things}

strcmp save us all the code above right ?
Ttelmah
Guest







PostPosted: Thu Sep 20, 2007 3:35 pm     Reply with quote

Yes.
It is just a simple 'loop', walking through the string, till it sees the terminator character, and comparing character by character as it goes.
The source code, is actually in string.h, so you can see exactly how it works. It uses pointers, rather than array indexes (but these are functionally the same in C), and does the 'bonus', of returning -1, or +1, if the strings are less than, or greater than each other.

Best Wishes
L.Belanger
Guest







PostPosted: Thu Sep 20, 2007 4:20 pm     Reply with quote

thank you for the explanation ... Smile

Best Wishes you too :P

L.Belanger
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