Problem creating data logger code with Hexdata 9

Hi I recently bought a DFR robot CO2 sensor Infrared_CO2_Sensor_0-50000ppm_SKU__SEN0220-DFRobot This was all good and I was able to get a simple code running and get the data displayed on the serial monitor. but my project idea was to store this data on SD card and use a battery so the computer does not have to attached. I found a simple data-logger example in the Arduino software and started to work on it, but I found this hard because the sensor doesn't seem to use there same sort of data string as other sensor code. I also found the sensor uses digital pins unlike most other sensors I have seen.
It would be greatly appreciated if anyone could show me how form this data-logger code.
these are the two codes.

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
unsigned char hexdata[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79}; //Read the gas density command /Don't change the order
void setup() {
  
  Serial.begin(9600);
  while (!Serial) {

  }
  mySerial.begin(9600);

}

void loop() {
   mySerial.write(hexdata,9);
   delay(500);

 for(int i=0,j=0;i<9;i++)
 {
  if (mySerial.available()>0)
  {
     long hi,lo,CO2;
     int ch=mySerial.read();

    if(i==2){     hi=ch;   }   //High concentration
    if(i==3){     lo=ch;   }   //Low concentration
    if(i==8) {
               CO2=hi*256+lo;  //CO2 concentration
      Serial.print("CO2 concentration: ");
      Serial.print(CO2);
      Serial.println("ppm");      
               }

  }   
  
 } 

 }

and

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

const int chipSelect = 4;

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


  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  // make a string for assembling the data to log:
  String dataString = "";

  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

these are the two codes.

That work. Some other code doesn't. You didn't post THAT code. But, that is the code you want help with.

Get a clue.

Sorry I forgot to mention the fact that the data logger code doesn't work and thats the code I need help with because I don't no how to incorporate the "hexdata 9" into it

Sorry I forgot to mention the fact that the data logger code doesn't work

That's as lame an explanation as you could possibly give. The code does something. If that something is not what you expect, then your expectations are wrong.

What does the code ACTUALLY do? What did you expect the code to do?

something like this you mean (untested):

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

SoftwareSerial mySerial(10, 11); // RX, TX
const int chipSelect = 4;
unsigned char hexdata[9] = {0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79}; //Read the gas density command /Don't change the order
unsigned long log_cnt = 0;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);

  mySerial.begin(9600);

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop() {
  long hi, lo, CO2;
  String dataString = "";

  mySerial.write(hexdata, 9);
  delay(500);

  // read sensor and append to the string:
  for (int i = 0; i < 9; i++)
  {
    if (mySerial.available() > 0)
    {
      int ch = mySerial.read();

      if (i == 2) {
        hi = ch;    //High concentration
      }
      else if (i == 3) {
        lo = ch;    //Low concentration
      }
      else if (i == 8) {
        CO2 = hi * 256 + lo; //CO2 concentration
        ++log_cnt;
        // make a string for assembling the data to log:
        dataString = "Log" + String(log_cnt) + " CO2 concentration(ppm): " + String(CO2);
      }

    }
  }

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

something like this you mean

NO! There is NO excuse for using the String class in that code.

sherzaad:
something like this you mean (untested):

cheers that's perfect.