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

help in strings comparison

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



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

help in strings comparison
PostPosted: Mon Mar 17, 2014 8:33 pm     Reply with quote

Could you please help me to find what's wrong with the following code. I am trying to send AT commands to test my gsm which is connected to pic16f876 through TX and RX pins. But when compiling I always get the following error:
Attempt to create a pointer to a constant
many thanks in advance.
here is the code:
Code:

#include <16F876.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#fuses  XT NOWDT NOPUT NOPROTECT BROWNOUT NOLVP NOCPD NOWRT NODEBUG
#use delay(clock=4000000) // you must use this line becuse the LCD.C use //the delay_us() functions
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)

//#pragma use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define use_portb_lcd TRUE
#include <lcd.c>

void main()
{
int i;
char buffer[];
char rec_data;
SET_TRIS_A( 0xFF );
SET_TRIS_B( 0x00 );
SET_TRIS_C( 0x00 );
i=0;
lcd_init();
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"hallo");
DELAY_ms(3000);
for(;;)
  {
   lcd_putc('\f');   
   printf(lcd_putc,"pressez bouton");
   if(input(PIN_A0))

      {
      printf("at\r"); //test gsm
      do
       {
        rec_data=getc();
       }
      while(rec_data!='\n');  //wait for gsm response
      do
      {
      rec_data=getc();
      buffer[i]=rec_data;  //save response in a string
      i++;
      }
      while(rec_data!='\n');
if(strcmp(buffer,"ok",2 )!=0) //see if response is "ok"
         {
           lcd_putc('\f');
           printf(lcd_putc,"error...");
         }

            lcd_putc('\f');
            printf(lcd_putc,"Error");

      }
  }
}

_________________
i am newbe
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 17, 2014 9:15 pm     Reply with quote

Quote:
But when compiling I always get the following error:
Attempt to create a pointer to a constant

That problem can be fixed by adding the line shown below:
Quote:
#include <16F876.h>
#device PASS_STRINGS=IN_RAM


But your program has many other problems.
Quote:

char buffer[];
buffer[i]=rec_data;

In the code above, you have not declared any size for the buffer array.
Therefore, the compiler doesn't allocate any RAM to it. But then your
code writes to buffer[i]. What RAM location is it writing to ?
Fix this. Declare buffer with a suitable size so that it will hold all your
data. Be sure to allow an additional byte for the string terminator 0x00
at the end of every string.


Quote:
if(strcmp(buffer,"ok",2 )!=0)

This line has a few bugs.
1. 'buffer' has no size.
2. You have "ok" in lower case. The response I have always seen from
any device that accept AT commands is the "OK" is in upper case.
3. Where did you see that strcmp() has 3 parameters ? What document
did you read that shows it has 3 parameters ?
4. Where did you see that strcmp() returns a non-zero value if the
strings match ? Again, what document did you read that says this ?

It's possible that you meant to use strncmp(). If so, you have to spell
the function name correctly. Details are very important in programming.
Everything will fail if you don't watch the details.


Your program might have other bugs. I didn't look any farther.
pathmasugu



Joined: 21 Feb 2014
Posts: 25

View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger ICQ Number

PostPosted: Tue Mar 18, 2014 1:28 am     Reply with quote

dont compare direct string in strcmp function

dont use like this

if(strcmp(buffer,"ok")==0)

try use to following

char check[2]="ok";
.
.
.
if(strcmp(buffer,check)==0)
_________________
ROCK RAJ
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

PostPosted: Tue Mar 18, 2014 6:16 am     Reply with quote

Thank you very much for your help "PCM programmer" and "pathmasugu".
You're right "PCMprogrammer" it must be strncmp and not strcmp and by adding "#device PASS_STRINGS=IN_RAM" my problem was resolved. I get no more error. I have added a size for my array buffer too.
many many thanks
_________________
i am newbe
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

can't read gsm response
PostPosted: Mon Mar 24, 2014 10:38 am     Reply with quote

could you please help me ,
what is wrong in the following code ,i can't read the answer of the gsm to "at" command.i always get error.
Code:

#include <16F876.h>
#device PASS_STRINGS=IN_RAM
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#fuses  XT NOWDT NOPUT NOPROTECT BROWNOUT NOLVP NOCPD NOWRT NODEBUG
#use delay(clock=4000000) // you must use this line becuse the LCD.C use //the delay_us() functions
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
#define use_portb_lcd TRUE
#include <lcd.c>


void main()
{
int i;
char *reponse , buffer[20];
unsigned char rec_data;

SET_TRIS_A( 0xFF );
lcd_init();

lcd_putc('\f');

printf(lcd_putc,"hallo");
DELAY_ms(3000);
lcd_putc('\f'); 
printf(lcd_putc,"pressez bouton");
for(;;)
 
 {   
   if(!input(PIN_A0))
      {     
      lcd_putc('\f');
     
      printf(lcd_putc,"waiting...");
      DELAY_ms(3000);
      printf("at\r"); //send at command
      i=0;
      do
      {
      rec_data=getchar();
      buffer[i]=rec_data;  //save response in a string
      i++;
      }
      while(rec_data!='\n');
   
    //DELAY_ms(3000);
    //if (memcmp("OK",&buffer,2) != 0)
     reponse=strstr(buffer,"OK");
     if (reponse == NULL)
//!if(strcmp(buffer,"OK")!=0) //see if response is "ok"
            {
            lcd_putc('\f');
           
            printf(lcd_putc,"error");
            DELAY_ms(3000);
            }
    else {     
           lcd_putc('\f');
         
           printf(lcd_putc,"ready");
       
           DELAY_ms(3000);
         }
            lcd_putc('\f');
            printf(lcd_putc,"pressez bouton");
      }
  }
}

_________________
i am newbe
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 11:21 am     Reply with quote

Quote:
i always get error.

If you get an error, tell us what the error is, in detail.
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 11:41 am     Reply with quote

THANKS PCM PROGRAMMER FOR YOUR INTEREST,
I mean that when i run the program, i get on my lcd display "error" but never "ready"".
As you can see in the code, i send at command and i read the answer from gsm through rs232((tx and rx on the pic16f876,connected to rx and tx of my gsm)), when the response is "OK" i display on the lcd "ready" otherwise i display "error".
_________________
i am newbe
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 12:24 pm     Reply with quote

A string needs to be null terminated.
When you receive the '\n', you need to write '\0' to the buffer so it has the termination. strstr, and other 'string' functions, _require_ the string to have the proper termination.

Add 'errors' to the RS232 declaration. Otherwise if data is received in the long delays, the RS232 receive will become hung.

The clock and fuse declarations should always be before all the includes except the processor definition. Sequence should be:
Code:

#include <16F876.h>
#device PASS_STRINGS=IN_RAM
#fuses  XT NOWDT NOPUT NOPROTECT BROWNOUT NOLVP NOCPD NOWRT NODEBUG
#use delay(clock=4000000) // you must use this line becuse the LCD.C use //the delay_us() functions
#use rs232(baud=9600,UART1, ERRORS)
//end of processor setup - this should be completed _before_ loading
//anything else

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define use_portb_lcd TRUE
#include <lcd.c>
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 5:09 pm     Reply with quote

Thanks Ttelmah for your help,
What do you mean with "When you receive the '\n', you need to write '\0' to the buffer" which buffer are talking about and how can i do that?
_________________
i am newbe
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 24, 2014 10:36 pm     Reply with quote

This is your routine which reads data and puts it in a buffer until you get \n.
Quote:
do
{
rec_data=getchar();
buffer[i]=rec_data; //save response in a string
i++;
}
while(rec_data!='\n');

Look at your code while reading Ttelmah's comments.
ILLIAS28



Joined: 11 Jan 2011
Posts: 42

View user's profile Send private message

PostPosted: Tue Mar 25, 2014 9:39 am     Reply with quote

thank's guys for your help it's very kind of you.
i did the corrections you told me ,and i have found a hardware problem too that i resolved.now it's working fine and i can send at commands from pic to gsm.the difficulty for me now is when receiving response or sms from gsm,how can i detect the end of it.'cause the "\n" isn't any more enough ,since some at commands response send many time "\n"
_________________
i am newbe
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