Car-PC.info

Developer - Windows und COM-Ports

JTRipper - Di 21 Aug, 2007 20:05
Titel: Windows und COM-Ports
Hey Leute!
Ich habe ein Problem...
Ich habe keine Lust mehr, wenn ich mal wieder Straßen für OpenStreetMap mitlogge, jedes Mal einen Logger zu starten, dann eine Datei zum speichern auszuwählen, und anschließend das ganze von NMEA nach GPX umzuwandeln...
Also dachte ich mir, schreibe ich mir ein Programm.
Das Programm tut zwar fast was es soll, aber eben nur fast. Es öffnet den Com-Port, liest 1 Zeile vom Com-Port und schreibt in die Ausgabedatei. Ich hätte aber gern, dass er liest, bis das Programm beendet wird. Was mache ich falsch?

Hier mal der Code:

Code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool fileExists(const std::string& fileName)
{
  std::fstream fin;
  fin.open(fileName.c_str(),std::ios::in);
  if( fin.is_open() )
  {
    fin.close();
    return true;
  }
  fin.close();
  return false;
}

int main(){
  string line;
  ifstream myfile ("COM3");
  int i = 1;
  string filename = "C:\logs\gpslog";
  filename.push_back((char)i+48);
  filename.append(".gpx");
  while(fileExists(filename)){
    i++;
    filename = "C:\logs\gpslog";
    filename.push_back((char)i+48);
    filename.append(".gpx");
  }
  ofstream outfile (filename.data());

  if (myfile.is_open() && outfile.is_open())
  {
    outfile << "<xml>" << endl;
    outfile << "<gpx>" << endl;
    outfile << "<trk>" << endl;
    outfile << "<trkseg>" << endl;

    do
    {
      getline (myfile,line);
// Parsen der Daten und ausgeben
   }
    while (! myfile.eof() );
    outfile << "</trkseg>" << endl;
    outfile << "</trk>" << endl;
    outfile << "</gpx>" << endl;
    myfile.close();
    outfile.close();
  }

  else cout << "Fehler!n";

  return 0;
}


Hoffe ihr könnt mir helfen...

Gruß,
JTRipper
da_user - Di 21 Aug, 2007 20:49
Titel:
Ich kenn jetzt die Programmiersprache nicht, und habs nur überflogen, aber folgender Gedanke:
Hast du einen entsprechende Schleife programmiert?
billy - Di 21 Aug, 2007 21:00
Titel:
stimmt, das teil macht nur einen umlauf, dann ist es fertig.

ich denke du hast wie da_user schon schreibt einen timer o.ä vergessen der das ganze immer wieder ablaufen lässt.

gruß
billy
dridders - Di 21 Aug, 2007 21:08
Titel:
nein, das Teil läuft endlos, aber es liest nur und wandert über einen Kommentar drüber. Sofern da kein Code ausgeschnitten worden ist würd ich empfehlen in der Schleife nicht nur zu lesen sondern auch zu schreiben.
JTRipper - Di 21 Aug, 2007 22:08
Titel:
also eigentlich sollte das ding lesen bis es das ende der "datei" erreicht hat, dafür ist die do{....} while();-schleife da, das scheint aber ziemlich schnell zu gehen... habs unter linux programmiert und mit "/dev/ttyUSB0" als serielles device probiert, da ging es ohne probleme... nur im auto unter windows will es nicht so ganz.

da wo der kommentar steht hab ich code ausgeschnitten, weil ich dachte der sei unnötig... aber hier habt ihr nochmal den kompletten code:

Code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

bool fileExists(const std::string& fileName)
{
  std::fstream fin;
  fin.open(fileName.c_str(),std::ios::in);
  if( fin.is_open() )
  {
    fin.close();
    return true;
  }
  fin.close();
  return false;
}

int main(){
  string line;
  ifstream myfile ("COM3");
  int i = 1;
  string filename = "C:logsgpslog";
  filename.push_back((char)i+48);
  filename.append(".gpx");
  while(fileExists(filename)){
    i++;
    filename = "C:logsgpslog";
    filename.push_back((char)i+48);
    filename.append(".gpx");
  }
  ofstream outfile (filename.data());

//  ifstream myfile ("/dev/ttyUSB0");
  if (myfile.is_open() && outfile.is_open())
  {
    outfile << "<xml>" << endl;
    outfile << "<gpx>" << endl;
    outfile << "<trk>" << endl;
    outfile << "<trkseg>" << endl;

    do
    {
      getline (myfile,line);
      try{
        if(line.compare(1, 5, "GPRMC") == 0){
          size_t pos1;
          size_t pos2;
          size_t pos3;
          size_t pos4;
          size_t pos5;
          size_t pos6;
          size_t pos7;
          size_t pos8;
          size_t pos9;
          size_t pos10;
          size_t pos11;
          string time_hour;
          string time_minute;
          string time_second;
          string status;
          string latG;
          string latM;
          string lonG;
          string lonM;
          string latsign;
          string lonsign;
          string speed;
          string course;
          string date_day;
          string date_month;
          string date_year;
          pos1 = line.find(",")+1;
          pos2 = line.find(",", pos1+1)+1;
          pos3 = line.find(",", pos2+1)+1;
          pos4 = line.find(",", pos3+1)+1;
          pos5 = line.find(",", pos4+1)+1;
          pos6 = line.find(",", pos5+1)+1;
          pos7 = line.find(",", pos6+1)+1;
          pos8 = line.find(",", pos7+1)+1;
          pos9 = line.find(",", pos8+1)+1;
     time_hour = line.substr(pos1, 2);
          time_minute = line.substr(pos1+2, 2);
          time_second = line.substr(pos1+4, 2);
          status = line.substr(pos2, 1);
          date_day = line.substr(pos9, 2);
          date_month = line.substr(pos9+2, 2);
          date_year = line.substr(pos9+4, 2);
          if(status.compare("A") == 0){
            size_t poslat = line.find(".", pos3+1)+1;
            latG = line.substr(pos3, 2);
            latM = line.substr(pos3+2, pos4-pos3);
            latsign = line.substr(pos4, 1);
            size_t poslon = line.find(".", pos5+1)+1;
            lonG = line.substr(pos5, 3);
            lonM = line.substr(pos5+3, pos6-pos5);
            lonsign = line.substr(pos6, 1);
            speed = line.substr(pos7, pos8 - pos7 -1);
            course = line.substr(pos8, pos9 - pos8 -1);
            float x;
            float y;
            int xg;
            float xm;
            int yg;
            float ym;
            sscanf(latG.data(), "%d", &xg);
            sscanf(latM.data(), "%f", &xm);
            sscanf(lonG.data(), "%d", &yg);
            sscanf(lonM.data(), "%f", &ym);
            x = xg + (xm/60);
            y = yg + (ym/60);
            outfile.precision ( 9 );
            outfile << "<trkpt lat="" << x << "" lon="" << y << "">" << endl;
            outfile << "  <time>20" << date_year << "-" << date_month << "-" << date_day << "T";
            outfile << time_hour << ":" << time_minute << ":" << time_second << "Z</time>" << endl;
            outfile << "<course>" << course << "</course>" << endl;
            outfile << "<speed>" << speed << "</speed>" << endl;
            outfile << "</trkpt>" << endl;
          }
       
        }
      }
      catch(std::exception& e){
      }
    }
    while (! myfile.eof() );
    outfile << "</trkseg>" << endl;
    outfile << "</trk>" << endl;
    outfile << "</gpx>" << endl;
    myfile.close();
    outfile.close();
  }

  else cout << "Unable to open filen";

  return 0;
}


edit: sagt nix, ich weiß, der code ist schrecklich. ich hab normal nciht so viel mit c++ am hut
Alle Zeiten sind GMT + 1 Stunde
Powered by phpBB2 Plus and Kostenloses Forum based on phpBB