I am trying to use an STM32 microcontroller (STM32L010C6T6 to be exact) to parse NMEA sentences from a GPS module over UART. I receive the messages fine, but run into issues when I look for specific strings within the messages.
I read in 700 characters from the GPS module over UART and store them in a buffer. I want to find a "GNGLL" sentence within the buffer. I use the C function strstr()
to search my buffer for the string "GNGLL" but the function never finds the string, even when it should be there. I print my buffer contents over a different UART to a serial terminal on my PC, and I've even used the "live expressions" debug feature of the STM32cubeide to verify that the string is there. Here is the relevant code.
#include "main.h"
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <math.h>
uint8_t gps_uartbuf[700] = {0};
char gps_uartbuf_char[700] = {'A'};
void getlatlon(char latlon[]){
char *mylat = NULL;
const char gps_gngll[5] = {'G','N','G','L','L'};
__HAL_UART_CLEAR_IT(&huart2, UART_CLEAR_NEF|UART_CLEAR_OREF);
HAL_UART_Receive(&huart2, gps_uartbuf, (uint16_t)700, 1000);
int x = 0;
for(x=0; x<700; x++){
gps_uartbuf_char[x] = (char)gps_uartbuf[x];
}
gps_uartbuf_char[699] = '\0';
mylat = strstr(gps_uartbuf_char, gps_gngll);
if(mylat == NULL){
//DID NOT FIND GPGLL
return;
}else{
//do stuff
}
}
Why is the strstr()
never finding the string?