【问题标题】:GPS output being incorrectly written to file on SD card- ArduinoGPS输出被错误地写入SD卡上的文件-Arduino
【发布时间】:2014-03-12 19:38:04
【问题描述】:

我有一个草图,用于从 EM-406a GPS 接收器获取信息(纬度、经度)并将信息写入 Arduino 扩展板上的 SD 卡。

程序如下:

#include <TinyGPS++.h>

#include <SoftwareSerial.h>

#include <SD.h>

TinyGPSPlus gps;
SoftwareSerial ss(4, 3); //pins for the GPS
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;

void setup()
{
  Serial.begin(115200);  //for the serial output
  ss.begin(4800);  //start ss at 4800 baud
  Serial.println("gpsLogger by Aaron McRuer");
  Serial.println("based on code by Mikal Hart");
  Serial.println();
  //initialize the SD card 
  if(!card.init(SPI_FULL_SPEED, 9))
  {
    Serial.println("card.init failed"); 
  }
  //initialize a FAT volume
  if(!volume.init(&card)){
    Serial.println("volume.init failed"); 
  }
  //open the root directory
  if(!root.openRoot(&volume)){
    Serial.println("openRoot failed"); 
  }

  //create new file
  char name[] = "WRITE00.TXT";
  for (uint8_t i = 0; i < 100; i++){
    name[5] = i/10 + '0';
    name[6] = i%10 + '0';
    if(file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)){
      break; 
    } 
  }
  if(!file.isOpen())
  {
    Serial.println("file.create"); 
  }
  file.print("Ready...\n");
}

void loop()
{
  bool newData = false;

  //For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
    while (ss.available())
    {
      char c = ss.read();
      //Serial.write(c);  //uncomment this line if you want to see the GPS data flowing
      if(gps.encode(c))  //did a new valid sentence come in?
        newData = true;
    } 
  }

  if(newData)
  {
    file.write(gps.location.lat());
    file.write("\n");
    file.write(gps.location.lng());
    file.write("\n");
  }

  file.close();
}

当我在程序完成执行后打开 SD 卡上的文件时,我收到一条消息,提示它有编码错误。

我目前在里面(无法获得 GPS 信号,因此是 0),但需要解决编码问题,并且应该有与设备开启的秒数一样多的线路。只有那个。我需要做些什么才能使事情在这里正常工作?

【问题讨论】:

    标签: encoding gps arduino sd-card


    【解决方案1】:

    在循环中关闭文件并且从不重新打开它是文件中只有一组数据的原因。

    您确定 gps.location.lat() 和 gps.location.lng() 返回字符串,而不是整数或浮点数吗?这将解释您看到的二进制数据和“编码错误”。

    【讨论】:

    • 我已经处理了编码错误(或其他任何错误),但即使我将 file.open(&amp;root, name, O_CREAT | O_EXCL | O_WRITE) 的另一个副本移动到 loop 函数的头部,我仍然得到只有一行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2017-07-09
    • 2021-12-30
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    相关资源
    最近更新 更多