-1
  #include "mbed.h"
  #include "C12832_lcd.h"
  #include<cstring>
  #include<string>
  #include<sstream>

     C12832_LCD lcd;//creating LCD object
     Serial s_comms(USBTX, USBRX);//creating a serial comms object

     DigitalIn Button(p14);//using button to change pages


   int main()

  {

    char str[100] =   "$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A";

char*point;
point = strtok(str, ",");

int page_state = 0;

for (int i = 0; point != NULL; i++){


    //time
    if (i == 1 and page_state == 0){
        //using substrings to extract time elements
        string time = point;
        string hrs = time.substr(0, 2);
        string mins = time.substr(2, 2);
        string sec = time.substr(4, 2);

        //using string streams to reformat time string
        ostringstream tim;
        tim << hrs << ":" << mins << ":" << sec;
        time = tim.str();

        lcd.cls();
        lcd.locate(0, 1);
        lcd.printf("%s\n", time.c_str());

    }

    //date
    if (i == 9 and page_state == 0){
        string date = point;
        string day = date.substr(0, 2);
        string month = date.substr(2, 2);
        string year = date.substr(4, 2);

        //Converting the numerical month into abbreviation ect. 
        if (month == "03"){
            month = "Mar";
        }

        if (month == "04"){
            month = "Apr";
        }

        ostringstream dat;
        dat << day << "-" << month << "-20" << year;
        date = dat.str();

        lcd.locate(0, 9);
        lcd.printf("%s\n", date.c_str());

    }

    //latitude
    if (i == 3 and page_state == 0){
        string lati = point;
        string lati_deg = lati.substr(0, 2);
        string sml_latideg = lati.substr(2, 6);

        ostringstream lat;
        lat << "Lat: " << lati_deg << " deg " << sml_latideg << "'";
        lati = lat.str();

        lcd.locate(0, 18);
        lcd.printf("%s", lati.c_str());
    }


    //latitude direction (N or S)
    if (i == 4 and page_state == 0){
        string lat_dir = point;
        lcd.printf("%s\n", lat_dir.c_str());
        }

     point = strtok(NULL, ",");
    }


    //Change page

    if (Button == 1){
        page_state = !page_state;//toggle page state    
        wait(0.2);//debounce timer
        lcd.cls();
        }


    //second page
    for (int j = 0; point != NULL; j++){

        char str[100]      ="$GPRMC,123519,A,4807.038,N,01131.000,E,022.4,084.4,230394,003.1,W*6A";
         char*point;
        point = strtok(str, ",");
         //longitude
         if (j == 5 and page_state == 1){
          string lngi = point;
          string lngi_deg = lngi.substr(0, 2);
          string sml_lngideg = lngi.substr(2, 6);

         ostringstream lng;
         lng << "Lng: " << lngi_deg << " deg " << sml_lngideg << "'";
         lngi = lng.str();

         lcd.locate(0, 1);
         lcd.printf("%s", lngi.c_str());
         }

        //longitude direction (E or W)
        if (j == 6 and page_state == 1){
        string lng_dir = point;

        lcd.printf("%s\n", lng_dir.c_str());
        }

        //speed
        if (j == 7 and page_state == 1){
        string speed = point;

        ostringstream spd;
        spd << "Speed: " << speed;
        speed = spd.str();

        lcd.locate(0, 9);
        lcd.printf("%s\n", speed.c_str());
        }

        point = strtok(NULL, ",");
        }


    return 0;

}

您好,尝试在 mbed 应用程序板上获取板载按钮以允许我清除屏幕并输入新信息,该按钮当前没有任何作用,我正在屏幕上获取前 4 部分信息但是当按下按钮,我需要帮助才能完成这项工作

4

1 回答 1

0

这并不能直接回答 OP 的问题,但从长远来看这应该更有帮助。与其尝试在有限的环境中调试程序逻辑,不如将特定于平台的功能替换为允许在通用计算硬件上模拟平台而无需更改代码的函数和类通常是有帮助的。

通过添加

#include <cstdarg>
#include <iostream>

和一个假的 C12832_lcd.h

#pragma once
#include <cstdarg>
#include <iostream>

// Sim LCD class. Just writes LCD commands to console
class C12832_LCD
{
public:
    void cls()
    {
        std::cout << "LCD: Cleared" << std::endl;
    }
    void locate(int row, int col)
    {
        std::cout << "LCD: positioned " << row << "," << col << std::endl;
    }
    void printf(const char * fmt, ...)
    {
        char buffer[4096];
        va_list args;

        va_start(args, fmt);
        vsnprintf(buffer, sizeof(buffer), fmt, args);
        std::cout << buffer << std::endl;
        va_end(args);
    }
};

还有一个虚假的 mbed.h

#pragma once

// Sim DigitalIn class. Toggles true and false on the button. First call will be true
class DigitalIn
{
private:
    bool mVal;
public:
    DigitalIn(int): mVal(false)
    {

    }
    bool operator==(int)
    {
        mVal = !mVal;
        return mVal;
    }

};

//Sim serial Does nothing yet.
class Serial
{
public:
    Serial(int, int)
    {

    }
};

//sim wait. We don't need to wait in simulation, so does nothing.
void wait(double)
{

}

const int p14 = 14;
const int USBTX = 0;
const int USBRX = 0;

对于 OP 的代码,我现在可以在 Visual Studio IDE 的桌面上编译和运行,并将调试器的强大功能用于解决问题。快速浏览代码会发现两个逻辑错误中的第一个。第二个更微妙一些。注意你的范围。

快速推荐:

考虑使用带字符分隔符的重载,strtok而不是使用。这允许std::getline

std::stringstream stream(str);
std::string token;
while (std::getline(stream, token, ','))
{
    // do stuff
}

读取逗号分隔的输入流,如 NMEA 字符串。

于 2016-04-08T19:31:45.680 回答