在 SD 卡上读取和存储二进制 GPS 数据时出现问题

Problem at reading and storing binary GPS data on SD Card

作为硬件,我在我的项目中使用了 u-Blox (https://www.sparkfun.com/products/15005) 的 Arduino Due 和 GPS 接收器。我基本上想通过 UART 检索某个消息 (UBX-RXM-RAWX)。因为我将在 post-processing 中解析此消息,所以只读取所有二进制数据并将其直接存储到 SD 卡上就可以了。不幸的是,我在 C++ 方面经验不足,无法将二进制数据存储到任何文件中。

我发现我在那里缺少一些常识,所以我想问问你是否可以帮助我?我的代码也已附上,或者可以在 github 上找到:https://github.com/dariopa/GPS-Logging-Station/blob/master/GPS%20Station/_UBX_GPS_StoreBinaryMessage_RAWX_DUE/_UBX_GPS_StoreBinaryMessage_RAWX_DUE.ino

感谢您的帮助!

// RETRIEVE RAWX MESSAGE FOR RINEX GENERATION.

// Microcontroller: Arduino DUE
// GPS Receiver: NEO-M8P-2 (https://www.sparkfun.com/products/15005)

#include <SD.h>

File binaryFile;
const int CS = 10; // ChipSelect

const char UBLOX_INIT[] PROGMEM = {
  // Disable NMEA
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x24, // GxGGA off
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x2B, // GxGLL off
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x32, // GxGSA off
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x39, // GxGSV off
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x40, // GxRMC off
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x05, 0x47, // GxVTG off

  // Disable UBX
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0x02, 0x15, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x26, 0x46, // RXM-RAWX off

  // Enable UBX
  0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0x02, 0x15, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x27, 0x4B, // RXM-RAWX on

  // Rate
  0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0xE8, 0x03, 0x01, 0x00, 0x01, 0x00, 0x01, 0x39, //(1Hz)
  // 0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0xD0, 0x07, 0x01, 0x00, 0x01, 0x00, 0xED, 0xBD, // (0.5Hz)
  // 0xB5, 0x62, 0x06, 0x08, 0x06, 0x00, 0xB8, 0x0B, 0x01, 0x00, 0x01, 0x00, 0xD9, 0x41, // (0.33Hz)

};

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  delay(3000);
  // send configuration data in UBX protocol
  for (int i = 0; i < sizeof(UBLOX_INIT); i++) {
    Serial1.write( pgm_read_byte(UBLOX_INIT + i) );
    Serial.write( pgm_read_byte(UBLOX_INIT + i) );
    delay(10); // simulating a 38400baud pace (or less), otherwise commands are not accepted by the device.
  }

  // SD CARD
  // Initialize SD Card
  pinMode(CS, OUTPUT);
  if (!SD.begin(CS)) {
    Serial.println("Initialization of SD card failed - Freeze!");
    while (1) {}
  }
  else {
    Serial.println("Initialization done.");
  }
}

void loop() {
  if (Serial1.available()) {
    // read from port serial, send to port Serial:
    char Coord[300] = {Serial1.read()};
    Serial.write(Coord);
    binaryFile = SD.open("Data.bin", FILE_WRITE);
    if (binaryFile) {
      binaryFile.println(Coord);
    }
  }
}
char Coord[300] = {Serial1.read()};

这将 Coord[0] 初始化为 Serial1.read() 的 return。 Coord 数组的其余 299 个成员初始化为零。

如果 Serial1.read() return 是一个整数 int。如果它是 -1,则读取不成功。否则它是一个有效字符。

您想一次读取一个字符并存储它:

void setup() {
    ...

    // is there a point in opening the binaryFile each loop!?
    binaryFile = SD.open("Data.bin", FILE_WRITE);
    if (!binaryFile) { 
         // handle errror
         assert(0);
     }
}

void loop() {
  if (Serial1.available()) {
    int ci = Serial1.read();
    if (ci == -1) {
         // handle errpr
         return;
    }
    char c = ci;
    Serial.write(c);
    binaryFile.write(c);
  }
}