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

时间:2019-02-26 22:02:46

标签: c++ parsing binary-data

作为硬件,我在项目中使用了Arduino Due和u-Blox(https://www.sparkfun.com/products/15005)的GPS接收器。我基本上想通过UART检索特定消息(UBX-RXM-RAWX)。我将在后处理中解析此消息,因此只需读取所有二进制数据并将其直接存储到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);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

char Coord[300] = {Serial1.read()};

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

如果Serial1.read()返回整数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);
  }
}