need help with strstr function for microcontroller

This is a discussion on need help with strstr function for microcontroller within the C Programming forums, part of the General Programming Boards category; Hi I'm working on this GPS project for microcontroller, I'm getting an error with the strstr function saying it is ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    need help with strstr function for microcontroller

    Hi I'm working on this GPS project for microcontroller, I'm getting an error with the strstr function saying it is a type mismatch. I not sure if the error is because it is not fully ANSI compliant. are there any ideas what to do to make it compile?
    Compiler is Microchip C18

    if I take out the string.h library it will compile if I change the line

    found = strstr(gpsbuff, "$GPRMC"); to this
    *found=strstr(gpsbuff, "$GPRMC");

    it will compile but is this really doing anything different?

    Thank you

    Code:
    #include <p18cxxx.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <delays.h>
    #include "usart.h"
    #include "lcdInit.h"
    #include "MainScreen.h"
    
    #define BUFFER_SIZE 100
    //**********************CONFUGURATION BITS***********************
    #pragma config OSC = HS
    #pragma config LVP = OFF
    #pragma config WDT = OFF
    #pragma config BOR = OFF
    #pragma config PWRT = ON
    #pragma config STVR = ON
    //***************************************************************
    
    //******************** INTERUPT ROUTINE STUFF********************
    int gps_buffa_rx_count = 0;
    unsigned char gpsbuff[BUFFER_SIZE] = {NULL};
    
    //************** GPS PARSING STUFF ******************************
    void parsenmea(void);
    unsigned char *found;
    unsigned int incomplete = 0;
    unsigned int length =0;
    unsigned int done =0;
    unsigned int i =0;
    unsigned char GPRMCbuff[BUFFER_SIZE] = {NULL};
    unsigned char UTC[10] = {NULL};
    unsigned char LAT[9] = {NULL};
    unsigned char LONG[10] = {NULL};
    unsigned char NS[2] = {NULL};
    unsigned char EW[2] = {NULL};
    
    //***************************************************************
    
    void high_isr(void);
    #pragma code high_vector = 0x08 // high priority interrupt vector
    void interrupt_at_high_vector(void){
    _asm goto high_isr _endasm // assembly language instructions to jump
    }
    #pragma code
    #pragma interrupt high_isr
    
    void high_isr(void) {
    	if(PIR1bits.RCIF = 1) {
    		PIE1bits.RCIE =0;
      		gpsbuff[gps_buffa_rx_count] = RCREG;
     
      		if(gps_buffa_rx_count>210){
       			gps_buffa_rx_count=0;
      		}else{
       		      gps_buffa_rx_count++;
      		} 
    	}
    gpsbuff[gps_buffa_rx_count]=0x00; 
    PIR1bits.RCIF = 0;
    USARTReset();
    PIE1bits.RCIE = 1;
    }
    // Main routine
    void main(){
    INTCONbits.GIE = 1; //enable all interupts
    INTCONbits.PEIE = 1; //enable peripherial interupts
    ADCON1 = 0x0F; //all ports digital
    TRISC = 0b10000000; //RC7(RX) as input & RC6(TX) as output 
    SPBRG = 0x40; //baud rate = 4800
    RCSTA = 0b10010110; //serial port enable, 8-bit data
    
    OpenUSART(USART_TX_INT_OFF &
    USART_RX_INT_ON &
    USART_ASYNCH_MODE &
    USART_EIGHT_BIT &
    USART_CONT_RX &
    USART_BRGH_LOW,64);
    
    PIE1bits.RCIE = 0; // disables receive interrupt
    PIR1bits.RCIF = 0; // clear interupt flag if set
    PIE1bits.RCIE = 1; // enables receive interrupt
    USARTReset();     // sets CREN to 0 and back to 1
    lcd_Init();
    
    
    while(1)
    {
    	 if(gps_buffa_rx_count>=100) {
    		parsenmea();
    		gps_buffa_rx_count = 0;	
    	}
    }
    
    } //end main
    
    
    void parsenmea(void)
    {
    			// GPRMC sentence parse
    			// 
    			// go through buffer looking for a $ start command....
                 			
    		               found=strstr(gpsbuff,"$GPRMC");
    		
    			incomplete=0;
    
    			if(found!=0x00)
    			{
    				for (i=0;i<100;i++)
    				{
    					//if we find a null then incomplete sentence mark incomplete=1
    					if (*found==0x00)
    					{
    						incomplete=1;
    						i=169;
    					}
    					// if we find this then end of string, incomplete=0
    					if ((*found==0x0d)|(*found==0x2a))
    					{
    						incomplete=0;
    						i=169;		
    					}
    					if ((*found !=0x00)&(*found !=0x0d)&(*found!=0x2a))
    					{
    						GPRMCbuff[i]=*found;
    						DrawMainScreen(1,1,1,1);
    						*found++;
    					}
    				}
    			}
    
    	gps_buffa_rx_count = 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    strstr should return a char *, so your code as it stands should work OK. I wouldn't compare found with 0x00 tho', compare it will NULL, as that is what the function strstr() will return if the string is not found.

    Some comments on your code:
    Your formatting (indentation) is very poor.

    Code:
    						i=169;
    Is a bad way to "end a loop" - use either break, or a flag (e.g. "done = 1") and combine that with your condition for the loop. What if you decide to change the maximum string length to 200?

    Code:
    if ((*found==0x0d)|(*found==0x2a))
    Unless you explcitly need your processor to read found again to check for 0x2a (what's wrong with '*', by the way), then I would suggest you use || instead of |.

    Code:
    if ((*found !=0x00)&(*found !=0x0d)&(*found!=0x2a))
    As above, but && instead of &.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    133
    You have "char *found;" so you must use "found=strstr();", and you must use "found++;" later in code, because "*found++;" will increment contents (string char), not pointer itself (or it can increment pointer and do dummy-dereference, not 100% sure).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21