Write a series of bytes to SD card.

29 Jan 2010 . Edited: 29 Jan 2010

I need to send a group of 83 bytes as a header file for my SD card datalogger.  I am trying to write these 83 bytes in hex.  The problem is that the compiler writes the hex as two separate characters.  for example. the first byte is 0x46.  I end up with a 4 and a 6 as two separate bytes rather than a single byte representing 46 (ASCII 'F').  This means the software I am trying to interface with gets confused because the header is 166 bytes long instead of 83.

The software looks for individual bytes with an offset.  No spaces or line breaks can exist.

My code snippet is as follows.

FILE *fp = fopen("/sd/datalog.frd", "a");
//print header for MS.frd file
fprintf(fp,"%x",string of bytes);
fclose(fp);

Here is the string of bytes that is pasted into the fprintf line.  I added spaces and linebreaks for clarity.

 

 

0x46 0x52 0x44 0x00 0x00 0x00 0x00 0x01 0x4b 0x61 0xf1 0x63 0x4d 0x53 0x49 0x49 0x20 0x52 0x65 0x76 0x20 0x32 0x2e 0x38 0x38 0x30 0x30 0x30 0x20 0x20 0x20 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x51 0x00 0x70 0x01 0x00

 

Thanks for your help straightening out my format isses.  

29 Jan 2010 . Edited: 29 Jan 2010

Also, I have tried several formats.  Here are some formats of the first three bytes that still result in a split.

ox46 0x52 0x44

0x460x520x44

0x46,0x52,0x44

46 52 44

465244

29 Jan 2010

Can you explain how the data is stored and how fprintf access it ?

 

My guess is you should use a loop and a putc() function, but I am no expert.

29 Jan 2010

You're writing the text representation of the bytes into the file. Do it like this instead:

uint8_t header[] = { 0x46, 0x52, 0x44, 0x00, ... }; // can be split in several lines
...
size_t headerlen = sizeof(header);
if ( headerlen != fwrite(header, headerlen, 1, fp) )
error("Error writing header");

30 Jan 2010

I thought that is what was happening.  I will give it a try.

 

01 Feb 2010

Could you save the data as an array of char?

i.e. char MyData[] = {0x00, 0x01, 0x02... etc};

I have been doing something like this for storing bitmap data in a file.

01 Feb 2010

Martin,   this will not work for exactly the same problem.  They will write as a character not a hex value.  0x00,  will write 5 characters - 3 zeros, an x and a comma.

Thanks for the suggestion (I did try it).

Igor,  I get the code to compile but the device gives the "unhappy LED dance".  I have to go back and check the code.

01 Feb 2010

I finally got fwrite to work.  It helps when the header length and byte length are in the correct order for syntax.

 

Thanks for all your help.

 

example code.

 

char myheader1[] = {0x46,0x52,0x44...};

//file header setup
FILE *fp = fopen("/sd/datalog.frd", "a");
//print header for MS.frd file
fwrite(myheader1,81,1,fp);
fclose(fp);