Serial + SD Card

#include <SPI.h>
#include <SD.h>
int r;
byte b;

const int chipSelect = 4;
void setup() {
  // put your setup code here, to run once:
  //int r;
  Serial.begin(57600);
  Serial3.begin(57600);
  Serial.print("This is a test and should be ignored");
   if (!SD.begin(chipSelect)) {
    Serial.println("\nCard failed, or not present");
    // don't do anything more:
    return;
  }
  else{
  Serial.println("\ncard initialized.");

  File OpenFile = SD.open("abc.txt", FILE_WRITE);
         OpenFile.println(1);
       OpenFile.close();
}
}

void printHex2Digit(byte b)
{
  if (b <= 0xF) Serial.print("0");
  Serial.print(b, HEX);
//  OpenFile.println(b);

}

void loop() 
{
  // put your main code here, to run repeatedly
File OpenFile = SD.open("abc.txt", FILE_WRITE);
  if(OpenFile and Serial3.available());
  { 
    int r=Serial3.read();
    if (r!= -1)
       {
       //Serial.write(r);
       printHex2Digit(r);
       OpenFile.println(r);
       OpenFile.close();
       }
    }
}

Trying to read data over serial and write the same in SD card…serial data is visible on serial monitor, but nothing gets stores in SD card.
Also is am receiving data in HEX format (commands for another device). I need to again send out the same values I store on SD card in HEX format.
EF01FFFFFFFF01000606010010001E is example of one such command.

Your goal is not clear. You are printing data in textual (ASCII string) hex into the serial port, but printing the same data as textual decimal into the file.

Which one you want to do? Write binary data or the same output as the serial?

@nimit393, do not hijack. Thread split.

@nimit393, do not cross-post. Thread locked.