Problems with reading and storing binary data on SD card

Hello everyone!

As Hardware, I’m using an Arduino Due and a GPS Receiver from u-Blox (SparkFun GPS-RTK Board - NEO-M8P-2 (Qwiic) - GPS-15005 - SparkFun Electronics) for my project. I basically want to retrieve a certain message (UBX-RXM-RAWX). As I will parse it in post-processing, it would be fine to just read all binary data and store it directly onto an SD card. Unfortunately, not being very experienced in C++, I’m having troubles storing binary data into any file.

I see I’m missing some general knowledge there, and so I wanted to ask if you could help me out? My code is attached as well or can be found on github: https://github.com/dariopa/GPS-Logging-Station/blob/master/GPS%20Station/_UBX_GPS_StoreBinaryMessage_RAWX_DUE/_UBX_GPS_StoreBinaryMessage_RAWX_DUE.ino

Thanks for any help!

_UBX_GPS_StoreBinaryMessage_RAWX_DUE.ino (2.41 KB)

I'm having troubles storing binary data into any file.

What trouble(s)? Without knowing that, we can't help you.

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

A 300 element array to hold one byte is completely pointless.

      binaryFile.println(Coord);

The Coord array is NOT a string, and should NOT be passed to any function that expects a string. The println() method is NOT used to write binary data to a file.

Hello PaulS,

Thanks for your feedback. I managed to get it right now, your inputs were very helpful. Also, I will try to be more clear than "I'm experiencing troubles in storing binary data to the SD card".

The idea about the char Coord[300] was to create a buffer where all bytes could be stored, because right now, I'm opening and closing the file on the SD in every loop, which consumes a lot of time, hence not all bytes are written to the SD.

The current code is here: https://github.com/dariopa/GPS-Logging-Station/blob/master/GPS%20Station/_UBX_GPS_StoreBinaryMessage_RAWX_DUE/_UBX_GPS_StoreBinaryMessage_RAWX_DUE.ino

In the next step I'll try to store the data in a buffer, and then write the whole buffer at once.

Thanks again!

In the next step I'll try to store the data in a buffer, and then write the whole buffer at once.

That will not improve performance, since the SD class already print()s and write()s to a buffer, and commits the buffer to the file only when the buffer gets full or the flush() function is used or the file is closed.