2016-05-01 14 views
1

こんにちはGNSS - NMEAの文字列を解析するコードを作ったのですが、現時点ではstrtokが文字を間違えて文字列を正しく分割していないようです。パーズGNSS - NMEAの文字列

このコードの問題点は何ですか?

奇妙なことに、私は基本的にFixstatusを取得しません。 GNSSrunstatusと同じですが、それがSerial.println(Fixstatus)とstrcpy(Fixstatus)で始まった理由です。

p.s .: Arduinoプロジェクト用です。

Fixstatus 

UTCdatetime 
20160501124554.000 
latitude 
47.199923 
logitude 
9.442710 
altitude 
474.100 
speedOTG 
0.37 
course 

fixmode 
1 
HDOP 
1.3 
PDOP 
0.8 
VDOP 
?>0 

また: - 解析コード(ない毎回)の

AT 

OK 
AT+CGNSPWR=1 

OK 
AT+CGNSINF 

+CGNSINF: 1,1,20160501124254.000,47.199897,9.442750,473.500,0.35,36.8,1,,1.1,1.9,1.6,,13,7,,,39,, 

OK 

出力:私は文字列を解析していない場合、出力は次のようになります

#include <SoftwareSerial.h> 

char frame[100]; 
char GNSSrunstatus[1]; 
char Fixstatus[1]; 
char UTCdatetime[18]; 
char latitude[10]; 
char logitude[11]; 
char altitude[8]; 
char speedOTG[6]; 
char course[6]; 
char fixmode[1]; 
char HDOP[4]; 
char PDOP[4]; 
char VDOP[4]; 
char satellitesinview[2]; 
char GNSSsatellitesused[2]; 
char GLONASSsatellitesused[2]; 
char cn0max[2]; 
char HPA[6]; 
char VPA[6]; 

boolean state; 
double serialnr; 

SoftwareSerial mySerial(10, 11); // RX, TX 

void setup() { 
    // Open serial communications and wait for port to open: 
    Serial.begin(9600); 
    while (!Serial) { 
    ; // wait for serial port to connect. Needed for native USB port only 
    } 


    Serial.println("Alles OK"); 

    // set the data rate for the SoftwareSerial port 
    mySerial.begin(9600); 
    mySerial.println("AT"); 
    mySerial.println("AT+CGNSPWR=1"); 
} 

void loop() { // run over and over 

    get_GPS(); 

} 

int8_t get_GPS() { 

    int8_t counter, answer; 
    long previous; 

    // First get the NMEA string 
    // Clean the input buffer 
    while (mySerial.available() > 0) mySerial.read(); 
    // request Basic string 
    mySerial.println("AT+CGNSINF"); //sendATcommand("AT+CGNSINF", "AT+CGNSINF\r\n\r\n", 2000); 

    counter = 0; 
    answer = 0; 
    memset(frame, '\0', sizeof(frame)); // Initialize the string 
    previous = millis(); 
    // this loop waits for the NMEA string 
    do { 

    if (mySerial.available() != 0) { 
     frame[counter] = mySerial.read(); 
     counter++; 
     // check if the desired answer is in the response of the module 
     if (strstr(frame, "OK") != NULL) 
     { 
     answer = 1; 
     } 
    } 
    // Waits for the asnwer with time out 
    } 
    while ((answer == 0) && ((millis() - previous) < 2000)); 

    frame[counter - 3] = '\0'; 

    // Parses the string 
    strtok(frame, ","); 
    strcpy(Fixstatus, strtok(NULL, ",")); // Gets Fix status 
    strcpy(UTCdatetime, strtok(NULL, ",")); // Gets UTC date and time 
    strcpy(latitude, strtok(NULL, ",")); // Gets latitude 
    strcpy(logitude, strtok(NULL, ",")); // Gets longitude 
    strcpy(altitude, strtok(NULL, ",")); // Gets MSL altitude 
    strcpy(speedOTG, strtok(NULL, ",")); // Gets speed over ground 
    strcpy(course, strtok(NULL, ",")); // Gets course over ground 
    strcpy(fixmode, strtok(NULL, ",")); // Gets Fix Mode 
    strtok(NULL, ","); 
    strcpy(HDOP, strtok(NULL, ",")); // Gets HDOP 
    strcpy(PDOP, strtok(NULL, ",")); // Gets PDOP 
    strcpy(VDOP, strtok(NULL, ",")); // Gets VDOP 
    strtok(NULL, ","); 
    strcpy(satellitesinview, strtok(NULL, ",")); // Gets GNSS Satellites in View 
    strcpy(GNSSsatellitesused, strtok(NULL, ",")); // Gets GNSS Satellites used 
    strcpy(GLONASSsatellitesused, strtok(NULL, ",")); // Gets GLONASS Satellites used 
    strtok(NULL, ","); 
    strcpy(cn0max, strtok(NULL, ",")); // Gets C/N0 max 
    strcpy(HPA, strtok(NULL, ",")); // Gets HPA 
    strcpy(VPA, strtok(NULL, "\r")); // Gets VPA 

Serial.println("Fixstatus"); 
    Serial.println(Fixstatus); 
Serial.println("UTCdatetime"); 
    Serial.println(UTCdatetime); 
Serial.println("latitude"); 
    Serial.println(latitude); 
Serial.println("logitude"); 
    Serial.println(logitude); 
Serial.println("altitude"); 
    Serial.println(altitude); 
Serial.println("speedOTG"); 
    Serial.println(speedOTG); 
Serial.println("course"); 
    Serial.println(course); 
Serial.println("fixmode"); 
    Serial.println(fixmode); 
Serial.println("HDOP"); 
    Serial.println(HDOP); 
Serial.println("PDOP"); 
    Serial.println(PDOP); 
Serial.println("VDOP"); 
    Serial.println(VDOP); 
Serial.println("satellitesinview"); 
    Serial.println(satellitesinview); 
Serial.println("GNSSsatellitesused"); 
    Serial.println(GNSSsatellitesused); 
Serial.println("GLONASSsatellitesused"); 
    Serial.println(GLONASSsatellitesused); 
Serial.println("cn0max"); 
    Serial.println(cn0max); 
Serial.println("HPA"); 
    Serial.println(HPA); 
Serial.println("VPA"); 
    Serial.println(VPA); 

    return answer; 
} 

SIM808およびGNSS NEMAストリングのマニュアル(ページ9-10): Manual

答えて

2

このコードは今まで私が持っていた質問に完全に対応しています:

#include <SoftwareSerial.h> 

char frame[200]; 
char GNSSrunstatus[2]; 
char Fixstatus[2]; 
char UTCdatetime[19]; 
char latitude[11]; 
char logitude[12]; 
char altitude[9]; 
char speedOTG[7]; 
char course[7]; 
char fixmode[2]; 
char HDOP[5]; 
char PDOP[5]; 
char VDOP[5]; 
char satellitesinview[3]; 
char GNSSsatellitesused[3]; 
char GLONASSsatellitesused[3]; 
char cn0max[3]; 
char HPA[7]; 
char VPA[7]; 

boolean state; 
double serialnr; 

SoftwareSerial mySerial(10, 11); // RX, TX 

void setup() { 
    // Open serial communications and wait for port to open: 
    Serial.begin(9600); 
    while (!Serial) { 
    ; // wait for serial port to connect. Needed for native USB port only 
    } 

    Serial.println("Alles OK"); 

    // set the data rate for the SoftwareSerial port 
    mySerial.begin(9600); 
    mySerial.println("AT"); 
    mySerial.println("AT+CGNSPWR=1"); 
} 

void loop() { // run over and over 
    get_GPS(); 
} 

int8_t get_GPS() { 

    int8_t counter, answer; 
    long previous; 

    // First get the NMEA string 
    // Clean the input buffer 
    strcpy(frame, "+CGNSINF: 1,1,20160501124254.000,47.199897,9.442750,473.500,0.35,36.8,1,,1.1,1.9,1.6,,13,7,,,39,, OK"); 

    // Parses the string 
    strtok_single(frame, " "); 
    strcpy(GNSSrunstatus, strtok_single(NULL, ","));// Gets GNSSrunstatus 
    strcpy(Fixstatus, strtok_single(NULL, ",")); // Gets Fix status 
    strcpy(UTCdatetime, strtok_single(NULL, ",")); // Gets UTC date and time 
    strcpy(latitude, strtok_single(NULL, ",")); // Gets latitude 
    strcpy(logitude, strtok_single(NULL, ",")); // Gets longitude 
    strcpy(altitude, strtok_single(NULL, ",")); // Gets MSL altitude 
    strcpy(speedOTG, strtok_single(NULL, ",")); // Gets speed over ground 
    strcpy(course, strtok_single(NULL, ",")); // Gets course over ground 
    strcpy(fixmode, strtok_single(NULL, ",")); // Gets Fix Mode 
    strtok_single(NULL, ","); 
    strcpy(HDOP, strtok_single(NULL, ",")); // Gets HDOP 
    strcpy(PDOP, strtok_single(NULL, ",")); // Gets PDOP 
    strcpy(VDOP, strtok_single(NULL, ",")); // Gets VDOP 
    strtok_single(NULL, ","); 
    strcpy(satellitesinview, strtok_single(NULL, ",")); // Gets GNSS Satellites in View 
    strcpy(GNSSsatellitesused, strtok_single(NULL, ",")); // Gets GNSS Satellites used 
    strcpy(GLONASSsatellitesused, strtok_single(NULL, ",")); // Gets GLONASS Satellites used 
    strtok_single(NULL, ","); 
    strcpy(cn0max, strtok_single(NULL, ",")); // Gets C/N0 max 
    strcpy(HPA, strtok_single(NULL, ",")); // Gets HPA 
    strcpy(VPA, strtok_single(NULL, "\r")); // Gets VPA 

Serial.println("GNSSrunstatus"); 
    Serial.println(GNSSrunstatus); 
Serial.println("Fixstatus"); 
    Serial.println(Fixstatus[0]); 
Serial.println("UTCdatetime"); 
    Serial.println(UTCdatetime); 
Serial.println("latitude"); 
    Serial.println(latitude); 
Serial.println("logitude"); 
    Serial.println(logitude); 
Serial.println("altitude"); 
    Serial.println(altitude); 
Serial.println("speedOTG"); 
    Serial.println(speedOTG); 
Serial.println("course"); 
    Serial.println(course); 
Serial.println("fixmode"); 
    Serial.println(fixmode); 
Serial.println("HDOP"); 
    Serial.println(HDOP); 
Serial.println("PDOP"); 
    Serial.println(PDOP); 
Serial.println("VDOP"); 
    Serial.println(VDOP); 
Serial.println("satellitesinview"); 
    Serial.println(satellitesinview); 
Serial.println("GNSSsatellitesused"); 
    Serial.println(GNSSsatellitesused); 
Serial.println("GLONASSsatellitesused"); 
    Serial.println(GLONASSsatellitesused); 
Serial.println("cn0max"); 
    Serial.println(cn0max); 
Serial.println("HPA"); 
    Serial.println(HPA); 
Serial.println("VPA"); 
    Serial.println(VPA); 

    return answer; 
} 

/* strtok_fixed - fixed variation of strtok_single */ 
static char *strtok_single(char *str, char const *delims) 
{ 
    static char *src = NULL; 
    char *p, *ret = 0; 

    if (str != NULL) 
     src = str; 

    if (src == NULL || *src == '\0') // Fix 1 
     return NULL; 

    ret = src;       // Fix 2 
    if ((p = strpbrk(src, delims)) != NULL) 
    { 
     *p = 0; 
     src = ++p; 
    } 
    else 
     src += strlen(src); 

    return ret; 
}