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

problem in getting a set of characters from string?!

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



Joined: 03 May 2016
Posts: 20

View user's profile Send private message

problem in getting a set of characters from string?!
PostPosted: Tue May 03, 2016 4:22 pm     Reply with quote

Hi ,

I need to set date & time of the real time clock connected to PIC.
I want to use a GSM in my project to control some devices also to set the real time clock with the correct time and date via sms Smile

The first thing I did was write a program to get the time & date from the received sms.

The received string of the new message has the following form:

+CMT: "+00962780055097","+962788990088","16/04/30,18:42:37+08",20
CLOCK=16/05/03,19:50:00

I need to get the characters after '=' character which are represent the date and time to be set Smile

But i faced a problem to get those characters. When I tried to print the string where i store the characters in i got garbage characters or some of characters before the '=' character.. OR NO CHARACTERS APPEARS ON THE TERMINAL ^_^

I am using
- compiler V5.056
- GSM type SM5100B
Code:

#include <16F877a.h>
#include <string.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6, rcv=PIN_C7,STREAM=GSM,ERRORS)

#define buffer_size 96
#define NULL '\0'       
#DEFINE CMTI     0         
#DEFINE CLOCK     1

UNSIGNED char RECSTRING[buffer_size];
unsigned int8 CT=0;
UNSIGNED CHAR *STRINGS[]={"+CMT\0","CLOCK\0"};
CHAR *CLL;


void CLEAR_BUFFER()
{
   memset(RECSTRING,NULL,buffer_size);      // Set all elements to NULL
   CT=0;                           // Reset index
}


#int_rda
void serial_isr() {

RECSTRING[CT]=Fgetc(GSM);
CT++;
if(CT==buffer_size){CT=0;}

}


int1 SEARCH_STRING(int8 index){
if(strstr(RECSTRING,STRINGS[index])!=NULL) return (1);
else return (0);
}


void main() {

CLEAR_BUFFER();
enable_interrupts(int_rda);
enable_interrupts(global);
Fprintf(GSM,"AT\r");                     
DELAY_MS(5000);
CLEAR_BUFFER();
Fprintf(GSM,"AT+CMGF=1\r");           
DELAY_MS(3000);
CLEAR_BUFFER();
Fprintf(GSM,"%s","AT+CNMI=3,3,0,0\r");               
DELAY_MS(3000);
CLEAR_BUFFER();

while(1){

  WHILE(!SEARCH_STRING(CMTI));

  if(SEARCH_STRING(CLOCK)){
    CLL=strrchr(RECSTRING,'='); Fprintf(GSM,"%s",CLL);
    CLEAR_BUFFER();}

   }
}

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 03, 2016 5:56 pm     Reply with quote

1. You don't have any code in your #int_rda routine that looks at the
incoming bytes for an end-of-message marker. So how does your main()
code know when you have a complete string available to process ?

2. The next thing is, you're using string functions, but you don't put a
string terminator byte of 0x00 at the end of the received string. Without
that marker, string functions (like strstr, etc.) don't know when to quit.

However, this may not be a problem because you zero the receive buffer
and reset the index to 0 prior to a string coming in. But this is a band-aid.
It would be better if you explicitly put a 0x00 at the end of the string
after it's received.

3. Your string extraction problem itself is a pure code problem.
That means it can be totally inspected and solved with a test program
in MPLAB simulator. I used MPLAB vs. 8.92 simulator to do this.

A. I took your program and stripped out all the data acquisition code that
I just don't need. I substituted your sample string as hard-coded data
in the receive buffer. I wasn't sure about the 20 before the CLOCK.
Did you really mean a space ? Is there a space in there after the 20 ?
But it doesn't really matter for this test.

B. Next I put in marker messages (printf's) that tell me where I'm at
in the program.

Before the next step, I put in a printf() statement to display the hard-
coded received string in MPLAB simulator, just to make sure that I
did it correctly, with the escape sequences on the double-quotes, etc.
After looking at the displayed string, I comment out the first printf.

C. Then I compile and run the test program and I get this and it's working
just fine:
Quote:

Next search string for CMD1:
Next search string for CLOCK:
=16/05/03,19:50:00
Done

This means the problem is going to be items 1 or 2 above, or something else.

Test program.
Code:
#include <16F877a.h>
#include <string.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, STREAM=GSM, ERRORS)

#define buffer_size 96
//#define NULL '\0'       
#DEFINE CMTI     0         
#DEFINE CLOCK    1

UNSIGNED char RECSTRING[buffer_size] = "+CMT: \"+00962780055097\",\"+962788990088\",\"16/04/30,18:42:37+08\",20 CLOCK=16/05/03,19:50:00";

UNSIGNED CHAR *STRINGS[]={"+CMT\0","CLOCK\0"};
CHAR *CLL;

//====================================
int1 SEARCH_STRING(int8 index)
{
if(strstr(RECSTRING, STRINGS[index]) != NULL)
   return (1);
else
   return (0);
}

//======================================
void main()
{
//printf(recstring);

printf("Next search string for CMD1:\r");

WHILE(!SEARCH_STRING(CMTI));

printf("Next search string for CLOCK:\r");

if(SEARCH_STRING(CLOCK))
  {
   CLL = strrchr(RECSTRING,'=');
   Fprintf(GSM,"%s",CLL);
  }

printf("\rDone\r");

while(TRUE);   
}
 
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed May 04, 2016 5:34 am     Reply with quote

OK, FIRST you MUST step back and get the HARDWARE correct !

You have a 5 volt PIC and a 3 volt GSM module, at least according to the Googling that found the SM5100B as a Sparkfun product.

Even proper code will not run right on incorrect hardware.
You also stand a good chance of destroying the GSM according to the datasheet when you force 5 volts to it!!

Always read and understand the 'electrical specification' chapter of the PIC and peripherals BEFORE wiring up and applying power.

With 99% of all 'peripherals' or 'modules' these days they are 3 volt devices, so choose a PIC with the 'L', like xxxLFyyyyy OR a PIC that does work at 3 volts like the 18F46K22.

Jay
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