SD Card Read -uint8_t from memory - help

Hello, I have installed an SD formatted to FAT32, and I am reading a txt file with data 0-255.
the problem is not how to read these numbers, because the function that reads, memory reads as bytes. "read ()"

#include <SPI.h>
#include <SD.h>

File myFile;


union {                // This Data structure lets
  byte asBytes[4];    // us take the byte array
  uint8_t asint[1];    // sent from processing and
}                      // easily convert it to a
foo;


void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
 
   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
 
  myFile = SD.open("data/R0.txt");
  if (myFile) {
 
  
  for(int i=0;i<1;i++){
    
        int index=0;
    while(index<4)
  {
       foo.asBytes[index] = myFile.read();
     Serial.println(foo.asBytes[index]);

      index++;

  }
  if(index==4  )
  {
      	Serial.println(foo.asint[0]);

  }
       
    }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
	// nothing happens after setup
}

I was reading in this way, but the serial port from the processing. but I think that happens to be similar, but not
them.

/ / data from txt this:

184,179,179,179,179,179,179,179,179,179,179,179,179,179,179,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,171,

.................. and I want to read the data I have in the SD, in a uint8_t.

Omnimusha:
.................. and I want to read the data I have in the SD, in a uint8_t.

No, you can not read the date in(as a ) uint8_t.

  1. "184," is equal to 4bytes in your txt.
  2. read the 184 in any buffer(by any logic which you like e.g parsing, use of delimeter",") Edit: myFile.read(foo.asBytes, 3) reads the first three byte of file
    3) then convert this buffer into integer e.g atoi Edit: My mistake, thanks for the correction (PaulS)

Edit: if i am not wrong you want to read whole value 182 at a time as uint8_t.

Edit: if i am not wrong you want to read whole value 182 at a time as uint8_t.

It appears as though the file contains binary data, not ASCII data, and that the data represents 32 bit ints, using 4 bytes.

If that is the case, then reading the bytes one at a time, and populating the byte array of the union IS the correct approach.

The problem is that the union is defined incorrectly. Every element in the union needs to be exactly the same size. The uint8_t item is 8 bits. The 4 byte array is 32 bits. Not the same size at all.

If you change the uint8_t to uint32_t (or unsigned long), then both items in the union will be the same size.

PaulS:

Edit: if i am not wrong you want to read whole value 182 at a time as uint8_t.

It appears as though the file contains binary data, not ASCII data, and that the data represents 32 bit ints, using 4 bytes.

If that is the case, then reading the bytes one at a time, and populating the byte array of the union IS the correct approach.

The problem is that the union is defined incorrectly. Every element in the union needs to be exactly the same size. The uint8_t item is 8 bits. The 4 byte array is 32 bits. Not the same size at all.

If you change the uint8_t to uint32_t (or unsigned long), then both items in the union will be the same size.

First thanks for the correction.

To learn this new technique(for me). I tried this code but still confuse how to use it for useful purpose.

union {                
  byte asBytes[4];   
  uint32_t asInt[1];    
}                      
tst;

void setup()
{
  Serial.begin(9600);
  
  tst.asBytes[0] = 1;
  tst.asBytes[1] = 1;
  tst.asBytes[2] = 1;
  tst.asBytes[3] = ',';

  Serial.println(tst.asInt[0] ,HEX);
  Serial.println(tst.asInt[0] ,DEC);
  Serial.println(tst.asInt[0] ,BIN);
}

void loop()
{
  
}

OUTPUT:
2C010101
738263297
101100000000010000000100000001

I think so, I did some mistake.

Thanks

I think so, I did some mistake.

You would need to do something like this:

union {                
  byte asBytes[4];   
  uint32_t asInt[1];    
}                      
tst;

void setup()
{
  Serial.begin(9600);

  tst.asInt[0] = 185473;

Then, print the 4 bytes in tst.asBytes. Then, use those values in your code, in place of:

tst.asBytes[0] = 1;
tst.asBytes[1] = 1;
tst.asBytes[2] = 1;
tst.asBytes[3] = ',';

You are assuming that the 4 bytes of the unsigned long are going to look like regular characters. You'll note that the value I chose at random is more than 4 characters in length. But, it still fits in an unsigned long.

You are assuming that the 4 bytes of the unsigned long are going to look like regular characters. You'll note that the value I chose at random is more than 4 characters in length. But, it still fits in an unsigned long.

union {                
  byte asBytes[4];   
  uint32_t asint;    
}                      
txtst;

union {                
  byte asBytes[4];   
  uint32_t asint;    
}                      
rxtst;

void setup()
{
  Serial.begin(9600);
  
  txtst.asBytes[0] = 1;
  txtst.asBytes[1] = 2;
  txtst.asBytes[2] = 2;
  txtst.asBytes[3] = ',';
  
  Serial.println(txtst.asint ,HEX);
  Serial.println(txtst.asint ,DEC);
  Serial.println(txtst.asint ,BIN);
  
  //Reverse Process
  
  rxtst.asint = txtst.asint;
  
  Serial.println( rxtst.asBytes[0] );
  Serial.println( rxtst.asBytes[1] );
  Serial.println( rxtst.asBytes[2] );
  Serial.println( rxtst.asBytes[3] );
}

void loop()
{
  // nothing happens after setup
}

2C020201
738329089
101100000000100000001000000001
1
2
2
44

Thanks, now i got it how to use it.
Last question: Any suitable example of using it in any application

Last question: Any suitable example of using it in any application

Are you reading binary data from a file? Is the binary data really unsigned long?

By the way, if you name the union, you can create multiple instances of it, instead of redefining it for each instance.

union mashStuffTogether
{
  byte asBytes[4];   
  uint32_t asint;    
};

mashStuffTogether txtst, rxtst;

By the way, if you name the union, you can create multiple instances of it, instead of redefining it for each instance.

Oops, i was in hurry

union 
{                
  byte asBytes[4];   
  uint32_t asint;    
}                      
rxtst;

I was assuming this is the part of Node#2.

Are you reading binary data from a file? Is the binary data really unsigned long?

At this time ANSWER: NO. Actually i am thinking to use that kind of thing in Wireless Sensor Network(WSN). Thats why i ask the above question may be someone using/used it in WSN application.

Thats why i ask the above question may be someone using/used it in WSN application.

That would be a good use for a union. Sending binary data is faster than converting it to a string, sending the string, and then converting the string back to a numeric value. Partly because the number of bytes can be smaller, but also because the conversion steps are removed. The result is exact, to, not an approximation that is the result of converting the value to a string (and back).

PaulS:

Thats why i ask the above question may be someone using/used it in WSN application.

That would be a good use for a union. Sending binary data is faster than converting it to a string, sending the string, and then converting the string back to a numeric value. Partly because the number of bytes can be smaller, but also because the conversion steps are removed. The result is exact, to, not an approximation that is the result of converting the value to a string (and back).

Thanks, Your help and guidance really appreciable.

Good Night. 8)

#include <SPI.h>
#include <SD.h>

File myFile;
char str[3] ;
uint8_t  i=0;
uint8_t RGB[3];

void setup()
{
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  Serial.print("Initializing SD card...");
   pinMode(10, OUTPUT);
   
  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");
  
  
  myFile = SD.open("data/ALL0.txt");
  if (myFile) {
    
    // read from the file until there's nothing else in it:
   if (myFile.available()) {
     for(int a=0;a<60;a++){

            for(int x=0;x<3;x++){

       
      char c=myFile.read();
      i=0;
      str[2]=' ';
      while(c != ','){
  
        str[i]= c;
       c=myFile.read();
       i++;
    }
     RGB[x] =atof(str);
            }
    Serial.println( RGB[0]);
    Serial.println( RGB[1]);
    Serial.println( RGB[2]);

  
  
   }  
   }
    // close the file:
    myFile.close();
  } else {
  	// if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop()
{
	// nothing happens after setup
}
      i=0;
      str[2]=' ';
      while(c != ','){
  
        str[i]= c;
       c=myFile.read();
       i++;
    }
     RGB[x] =atof(str);

Doesn't it matter that you might be running past the end of the array? Yes, it does.
Doesn't it matter that str is not a NULL terminated array of characters? Yes, it does.

does not run beyond the series, because I know how much data I have available, this number is fixed.

beyond which change the value of the data.

so as I'm reading, reading in the library is full speed?

because I know how much data I have available, this number is fixed.

The values are all 2 digit numbers? That is all you have room for.

sorry did not understand your question.

I can read more than 1 txt file at once?

PaulS:
That would be a good use for a union.

Hello again.

After some time giving to design of packet and some experiments i came to this format of packet

union uPacket{
  struct packet{
    char sDelimeter; //To mantian Backward Compatibilty
    uint8_t id;
    uint8_t payload[10];
    char mDelimeter; //To mantian Backward Compatibilty
    uint16_t msgId;
    char eDelimeter; //To mantian Backward Compatibilty
  }
  sPac;
  uint8_t asBytes[16];
}
uRx, uTx;

Cycle of Communication

NODE 1 NODE 2
Fill(struct) -->Send(uTx.asBytes) -... ...- Recv(&buffer) -->copy(uRx.asBytes) -->extract(struct)

Suggestion required (anything need to keep in mind?).

Thanks in advance.

May be i need to start a new discussion but it will mix up the things with previous topic.

Edit:
New discussion link: http://arduino.cc/forum/index.php/topic,160553.0.html