This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Fstorage read and write at SDK11

I want to use fstorage to write data to a file. The File with three records: 0x1111="Tablet1", 0x2222="data: abcdef", 0x3333="26". How to implement it? Can anyone guide me?

Parents
  • I think you might want to use the 'fds' library for that. Have a look here: infocenter.nordicsemi.com/index.jsp

  • I think the problem might be with your conversion from byte to words. The string "data: abcdef" is 12 characters long, plus the implicit string terminator character '\0', that's a total of 13 bytes. When you set length_words field of the chunk like this:

    record_chunk.length_words = (sizeof(device_data)+1)/4;

    My guess is that sizeof(device_data) will be 13 + 1 (which you add manually) / 4 equals to 3.5, which gets rounded down to 3, which is wrong since the string is 12 characters long but you need the 13th character (string terminator) to print correctly.

    It works with "data abcdef" because that's one character shorter, thus 12 + 1 / 4 = 3,25 which gets rounded down to 3, which is right in this case because that's exactly the length of the string including the terminator character (implicit).

    If you want approximate you should add 3. I would try like this: length_words = (sizeof(data) + 3) / 4

Reply
  • I think the problem might be with your conversion from byte to words. The string "data: abcdef" is 12 characters long, plus the implicit string terminator character '\0', that's a total of 13 bytes. When you set length_words field of the chunk like this:

    record_chunk.length_words = (sizeof(device_data)+1)/4;

    My guess is that sizeof(device_data) will be 13 + 1 (which you add manually) / 4 equals to 3.5, which gets rounded down to 3, which is wrong since the string is 12 characters long but you need the 13th character (string terminator) to print correctly.

    It works with "data abcdef" because that's one character shorter, thus 12 + 1 / 4 = 3,25 which gets rounded down to 3, which is right in this case because that's exactly the length of the string including the terminator character (implicit).

    If you want approximate you should add 3. I would try like this: length_words = (sizeof(data) + 3) / 4

Children
No Data
Related