Hi Community,
My txt file in sd card looks like this :
385&10&0&10&0&0&0&0&255&
897&0&0&0&0&0&0&0&255&
385&10&0&10&0&0&0&0&255&
and I want to go through this file line by line, then convert each value after & into Hex value and save as array of byte. Converting the values into Hex is easy. However, i cant figure out a nice and novel way to go thorugh the values. readStringUntil("&") was my first option, but then the second value contains the first value and the third value contains the fist and second ....
Does anybody have a tip?
#include <SPI.h>
#include <SD.h>
int i ;
File myFile;
char ch;
String arr;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
myFile = SD.open("test2.txt");
i=0;
arr = "";
}
void loop() {
// re-open the file for reading:
if (myFile.available() ) {
// read from the file until there's nothing else in it:
ch = myFile.read();
if(ch != '\n')
{
arr = arr + ch;
}
if(ch == '\n')
{
Serial.println(arr);
arr ="";
}
}
}
Above is my current code.
if(ch == '\n')
Do you want to do something when you get a null? Or when you see an '&'?
Or maybe both but the two things are different?
Does anybody have a tip?
Avoid Strings, they cause memory problems and program crashes.
Use C-strings instead, and the standard functions associated with them.
It is trivial to read a line like you've posted, separating out the ASCII numbers from the resulting C-string using the function strtok().
The ASCII numbers (which will be short C-strings) can be converted to binary using the function atoi().
So now, I changed my txt in SD a bit to make the reading more convenient.
Before :
385&10&0&10&0&0&0&0&255&
Now:
385&100100000255
The 385 indicates the ID of the data and 100100000255 indicates the dataset of INT8U (consisting of 8 INT8U).
Another problem occured :
print_received_buf(val,len,data_arr);
with val = 385 and len = 8 and data_arr = 100100000255 (data_arr is a char array ; char data_arr[100])
void print_received_buf(unsigned long rxId, byte *len, byte *rxBuf)
{
//received data
sprintf(msgString, "Standard ID: 0x%.3lX DLC: %1d Data:", rxId, len);
Serial.print(msgString);
for(int i=0; i<8;i++)
{
Serial.print(rxBuf[i]);
//Serial.print(" ");
}
Serial.println();
}
I expected that the output is like
Standard ID: 0x381 DLC: 8 Data:100100000255
However the actual output is
Standard ID: 0x381 DLC: 8 Data:4848484848484850
What am I missing here?
You can't mash integers together like that. How is the receiving code supposed to know that "100100000255" is
"10 0 10 0 0 0 0 255" and not "100 10 0 0 0 0 2 55"?
If you keep spaces or '&' between integers you could use myFile.parseInt();. It skips anything that doesn't look like a number (like ' ' or '&'), gathers up stuff that looks like a number (digits) and returns the value when it hits something that doesn't look like a number (like ' ' or '&'). Use "if (myFile.available())" to see if you have reached the end of the file. Note: If your file doesn't end with a digit you will get an extra 0 at the end when the .parseInt() times out.
What am I missing here?
strtok(), mentioned in reply #2.