c_cpp NMEA格式解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp NMEA格式解析相关的知识,希望对你有一定的参考价值。

static void UART_ProcessGPS(char ch) {
    if ((UART_GpsIdx == UART_GPSSIZE-1) || (ch == '$')) {
        UART_GpsIdx = 0;
    }

    UART_GpsBuf[UART_GpsIdx] = ch;
    UART_GpsIdx++;

    if ((UART_GpsIdx > 6) && (UART_GpsBuf[0] == '$') && (ch == '\r')) {
        NMEA_Parse(UART_GpsBuf, UART_GpsIdx);
        UART_GpsIdx = 0;
        // remove $ sign
        UART_GpsBuf[0] = ' ';
    }
}

/*
void DMA1_Channel4_IRQHandler() {
    DMA_Cmd(DMA1_Channel4, DISABLE);
    DMA_ClearITPendingBit(DMA1_IT_TC4);
}
*/

/* USART1 ISR routine */
void USART1_IRQHandler() {
    if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
        /* Read one byte from the receive data register */
        UART_ProcessGPS(USART1->DR & 0xff);
    }
}
gpsdata_t* gprmc_parse(const char*p) {
    float time, date, speed, heading;
    double lat, lon;
    char ns, ew;
    int valid;
    int i = sscanf(p, "$GPRMC,%f,%d,%lf,%c,%lf,%c,%f", &time, &valid, &lat, &ns, &lon, &ew, &speed);

    gpsdata_t* gps = malloc(sizeof(gpsdata_t));
    if(gps == NULL)return NULL;
    gps->time = time;
    gps->valid = valid;
    gps->lat = geo_resolve(lat * ((ns == 'N') ? 1 : -1));
    gps->lon = geo_resolve(lon * ((ew == 'E') ? 1 : -1));
    gps->speed = speed * 0.51444f;
    //gps->date = date;
    if(i == 9) {
        gps->heading = heading;
    }
    return gps;
}
volatile gpsdata_t gpgga;

void USART3_IRQHandler(void) {
    static uint32_t i = 0;
    static char nmea[USART3_RX_MAX];
    static gpsdata_t gps[sizeof(gpsdata_t)];
    char p = (char) USART_ReceiveData(USART3);
    switch(p) {
        case '$': nmea[i = 0] = '$'; return;
        case '\n': nmea[i] = '\n'; break;
        default: nmea[(++i) % (USART3_RX_MAX - 1)] = p; return;
    }


    if(nmea[3] == 'G' && nmea[4] == 'G' && nmea[5] == 'A') {
        if(gpgga_parse(nmea, gps) == 0) {
            gpgga = *gps;
        }
    }
}
int main() {
    float dat,tim,lat,lon,speed,heading;
    char nw,ew,av;
    int valid,sat;
    char *str="$GPGGA,082706.00,3054.25254,N,12155.34335,E,1,03,14.11,1.9,M,11.0,M,,*6F";
    int i=sscanf(str, "$GPGGA,%f,%f,%c,%f,%c,%d,%d", &tim,&lat,&nw,&lon,&ew,&valid,&sat);
    printf("%f,%f,%c,%f,%c,%d,%d", tim,lat,nw,lon,ew,valid,sat);

    printf("\nsscanf i=%d\n", i);

    str="$GPRMC,121252.000,A,3958.3032,N,11629.6046,E,15.15,359.95,070306,,,A*54";
    i=sscanf(str, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,%f,%f", &tim,&av,&lat,&nw,&lon,&ew,&speed,&heading,&dat);
    printf("%f,%c,%f,%c,%f,%c,%f,%f,%f", tim,av,lat,nw,lon,ew,speed,heading,dat);

    printf("\nsscanf i=%d", i);
}

以上是关于c_cpp NMEA格式解析的主要内容,如果未能解决你的问题,请参考以下文章

NMEA-0183协议解析

高通平台开发系列讲解(GPS篇)NMEA数据包解析

高通平台开发系列讲解(GPS篇)NMEA数据包解析

NMEA 解析程序

GPS NMEA协议,0183 定位数据格式 双模定位:GNXXX GPS+BD 完整版

GPS(NMEA)数据解析