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

Strange problem help me !!!--solved

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



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

Strange problem help me !!!--solved
PostPosted: Wed Oct 26, 2011 6:24 am     Reply with quote

I use a test code, detail as follow.
I wished it can make LED1 and printf work when used "strstr()" function to search char "OK", but my code is just the opposite.
Code:

#include <18f452.h>       
//#Device PASS_STRINGS=IN_RAM
#include <string.h>
#fuses HS,NOWDT,PUT,NOPROTECT
#use delay (clock = 20000000)
//#use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8,stream=GSM)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,parity=N,bits=8)
//#use i2c(MASTER,SDA=PIN_C4,SCL=PIN_C3,ADDRESS=0X9A,FORCE_HW)//I2C

#zero_ram 
#define  PORTA 0xF80
#define  PORTB 0xF81
#define  PORTC 0xF82                           
#define  PORTD 0XF83

#bit A1=PORTA.1  //
#bit col8=PORTC.0 //Matrix LED common 
#bit LED1=PORTD.0 //
#bit LED2=PORTD.1 //
#bit LED3=PORTD.2 //
#bit LED4=PORTD.3 //
#bit IGT =PORTD.4  //
#bit LED5=PORTD.5 //Test
#bit LED6=PORTD.6 //SWO
#bit LED7=PORTD.7 //SW1

char num="OK";
char Rec_Buf[]="123";

int1 Hand(unsigned char *a)
{     
    if(strstr(Rec_Buf,a)!=NULL)
      { return (1);}
   else
      return (0);
}
     

void main()

  set_tris_d(0x00);
  set_tris_c(0x00);
  set_tris_a(0x11);
  set_tris_b(0xff);
  output_d(0x00);
  port_b_pullups(TRUE);

  A1=1;
  col8=1;
  LED5=1;
  delay_ms(100);

  while(true)
   { 
      while(!Hand(num))
       {
      printf("AT\r\n");
      delay_ms(500);
      LED1=1;
   }
    LED2=1;
   }
 
   LED3=1;
   LED4=1;
   
}


Last edited by leevise on Thu Oct 27, 2011 7:57 pm; edited 1 time in total
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Oct 26, 2011 8:05 am     Reply with quote

Maybe I don' t express my question.
I want to use the "strstr(str1,str2)" function to query if str1 has "OK" .
If it has not "OK", the LED1 light and printf output ,
other, it is has "OK", the LED2 light.

But this code make a opposite running, I can't find my fault in this code, so please help me and analyse it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Oct 26, 2011 8:39 am     Reply with quote

I posted earlier, but the forum seems to have had a hiccup.
_Simplify_. Use defined names, (TRUE/FALSE), rather than 0/1, and avoid logical negations if possible.
Now, think about what happens:

'strstr', returns the location of a 'found' string.
So why not code as:

Code:

#define NOT_FOUND (NULL)

int1 Hand(unsigned char *a) {     
    if(strstr(Rec_Buf,a)==NOT_FOUND)
      return(FALSE); //Not found
    return(TRUE); //only get here if test if FALSE
}


This now makes the operation much simpler to understand if the string is 'not found' the code returns FALSE.

There is a glaring problem with the declaration of 'num'. It is currently a char, while it needs to be a char array or pointer....

There are then some other problems

Add ERRORS to the RS232 declaration. Otherwise if more than two characters arrive while you are sitting in the delay, the RS232 _will_ become completely hung. ERRORS should _always_ be present when using the hardware UART, unless _you_ explicitly handle error conditions yourself.

Then consider using interrupt driven serial (EX_SISR.C), or you _will_ lose characters if anything arrives during the 500mSec.

How is anything ever going to get into Rec_Buf ?. Currently it is "123'. As posted the code will sit forever printing AT...

Best Wishes
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Wed Oct 26, 2011 7:42 pm     Reply with quote

Ttelmah wrote:
I posted earlier, but the forum seems to have had a hiccup.
_Simplify_. Use defined names, (TRUE/FALSE), rather than 0/1, and avoid logical negations if possible.
Now, think about what happens:

'strstr', returns the location of a 'found' string.
So why not code as:

Code:

#define NOT_FOUND (NULL)

int1 Hand(unsigned char *a) {     
    if(strstr(Rec_Buf,a)==NOT_FOUND)
      return(FALSE); //Not found
    return(TRUE); //only get here if test if FALSE
}



First thank you very much for your advice.

But i don't make it work normal, it also acted reversed.


And you said to me about the "num" should be an array or pointer, I think my code that Hand() function set the "a" to pointer.
Is this function parameter wrong?
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 7:02 am     Reply with quote

No.
Your 'hand' function, expects to receive a pointer to an array of characters.
However 'num', is _not_ declared as such a pointer. it is declared as a _character_. In fact the character 'O' (0x4F). Hand then treats this as a pointer, resulting in completely indeterminate behaviour (will depend on what is in location '4F' in memory....). The functions behaviour is not 'reversed'. It is doing what you are telling it, but you are _not_ giving it the right values....

Best Wishes
leevise



Joined: 05 Aug 2010
Posts: 89

View user's profile Send private message

PostPosted: Thu Oct 27, 2011 7:07 pm     Reply with quote

Ttelmah wrote:
No.
Your 'hand' function, expects to receive a pointer to an array of characters.
However 'num', is _not_ declared as such a pointer. it is declared as a _character_. In fact the character 'O' (0x4F). Hand then treats this as a pointer, resulting in completely indeterminate behaviour (will depend on what is in location '4F' in memory....). The functions behaviour is not 'reversed'. It is doing what you are telling it, but you are _not_ giving it the right values....

Best Wishes


Thank you very much!

I solved it ,my code is modified
Code:
num[]="OK";
,or
Code:
*num="OK";
.
then the code running is normal!
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