Writing binary data to SD card

Posted on
  • I'm trying to write 16bit integers to a file, but didn't got the result I was expecting. For example:

    >var d = new Uint16Array([255, 1111, 42])
    =new Uint16Array([255, 1111, 42])
    >d[0]
    =255
    >d[1]
    =1111
    >d[2]
    =42
    >fs.appendFile("log0.txt", d)
    =true
    >var dr = fs.readFile("log0.txt")
    ="\xFFW*"
    >dr.charCodeAt(0)
    =255
    >dr.charCodeAt(1)
    =87
    >dr.charCodeAt(2)
    =42
    

    \xFF is 255, ok for one byte.
    1111 is 0100 0101 0111 in binary, and W is 87 character code, 0101 0111 binary. Looks like the lower byte of the 16 bit integer is saved.
    * is 42, ok.

    My question is: Looks like saving typed arrays directly is not supported. Will it be, or what would be the best way to save this data as fast and efficient as possible?

    Board is a Pixl running 2v08.188 (and of course shouldn't call a binary file .txt)

  • not solution but maybe workaround or thing to check - you can retype the array as Uint8Array in place (they share same memory) and see how that one gets written or check how it compares to 'bad' file you have.

    >var d8 = new Uint8Array(d.buffer)
    

    or even check d.buffer - that will be probably the same byte array as d8 would be

  • Much better, file length matches the buffer-s length. First couple of bytes match. I think all I need is read back on the computer.

    Thank you!

  • Yeah, the issue is that fs.appendFile("log0.txt", d) (and pretty much any 'write' command) iterates over the given array, assuming each element is a byte (even if you supply a UintXYZArray). It makes more sense if you think of what you'd want if you supplied just a simple array like [1,2,3,4].

    All you need to do to write is fs.appendFile("log0.txt", d.buffer)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Writing binary data to SD card

Posted by Avatar for AkosLukacs @AkosLukacs

Actions