作为硬件,我正在使用ublox(https://www.sparkfun.com/products/15005)的Arduino和GPS接收器。我基本上想通过UART检索某个消息(UBX-RXM-RAWX)。正如我将在后处理中解析此消息时,只要读取所有二进制数据并将其直接存储在SD卡上是可以的。不幸的是,在C 方面,我遇到了麻烦,将二进制数据存储到任何文件中。
我看到我在那里缺少一些一般知识,所以我想问一下您是否可以帮助我?我的代码也已附加,也可以在GitHub上找到:https://github.com/dariopa/gps-logging-station/blob/master/gps station/_ubx_gps_storebinarybinarymessage_rawx_rawx_rawx_rawx_rawx_rawx_gps_gps_gps_gps_storebinarybinarymessage_rawx_rawx_rawx_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()
。Coord
阵列的299个成员的其余成员以零为初始化。
如果Serial1.read()
返回整数int
。如果是 -1
,则不是read bes noccessfull。否则,这是一个有效的字符。
您想一次阅读一个字符并存储它:
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);
}
}