Redirect Output from SdFat library to Uart

Status
Not open for further replies.

Experimentalist

Well-known member
Hi, can anyone give me some guidance on how I may redirect the output from the SdFat library (https://github.com/greiman/SdFat.git) to the hardware serial output?

Some code to list the files on the SD card, using the standard Serial object:

Code:
#include <SdFat.h>
// SD card chip select pin.
const uint8_t SD_CHIP_SELECT = 6;

// File system object.
SdFat sd;

// Create a Serial output stream.
ArduinoOutStream cout(Serial);

// Buffer for Serial input.
char cinBuf[40];

// Create a serial input stream.
ArduinoInStream cin(Serial, cinBuf, sizeof(cinBuf));

// Error messages stored in flash.
#define error(msg) error_P(PSTR(msg))
//------------------------------------------------------------------------------
void error_P(const char* msg) {
  sd.errorHalt_P(msg);
}

void setup() {
  Serial.begin(9600);
  while (!Serial) {} // wait for Leonardo
  delay(1000);
  
  cout << pstr("Type any character to start\n");
  // Wait for input line and discard.
  cin.readline();

  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();

    cout << pstr("List of files on the SD.\n");
  sd.ls("/", LS_R);
}

void loop(){
}

Using the HardwareSerial object:

Code:
#include <SdFat.h>
// SD card chip select pin.
const uint8_t SD_CHIP_SELECT = 6;

// File system object.
SdFat sd;

HardwareSerial Uart = HardwareSerial();

// Serial output stream
ArduinoOutStream cout(Uart);

// input buffer for line
char cinBuf[40];
ArduinoInStream cin(Uart, cinBuf, sizeof(cinBuf));

// Error messages stored in flash.
#define error(msg) error_P(PSTR(msg))
//------------------------------------------------------------------------------
void error_P(const char* msg) {
  sd.errorHalt_P(msg);
}

void setup() {
  Uart.begin(38400);
  Serial.begin(9600);
  while (!Serial) {} // wait for Leonardo
  delay(1000);
  
  cout << pstr("Type any character to start\n");
  // Wait for input line and discard.
  cin.readline();

  if (!sd.begin(SD_CHIP_SELECT, SPI_HALF_SPEED)) sd.initErrorHalt();

    cout << pstr("List of files on the SD.\n");
  sd.ls("/", LS_R);
}

void loop(){
}

The problem I have is that the output from the line
Code:
sd.ls("/", LS_R);
is still directed to the software serial rather than the hardware serial. I presume I have to redirect the standard output stream some how but I am unsure how to achieve this.

Anybody?

Ex.
 
I use the SD Library by Limor and Tom Igoe (http://arduino.cc/en/Reference/SD), works very well, if you replace Serial with Serial1/Serial2/Serial3 then you will redirect it to the desired UART. Or isn't that what you are looking for (Assuming you use a Teensy 3.x)?
 
I haven't ever tried this before but it looks like you should be able to do something like

Code:
sd.ls(&Serial1, "/", LS_R);

It compiles but I'm not set up to test that at the moment. Should work though.
 
I haven't ever tried this before but it looks like you should be able to do something like

Code:
sd.ls(&Serial1, "/", LS_R);

It compiles but I'm not set up to test that at the moment. Should work though.

That is exactly what I needed, thanks for taking the time to reply

Ex
 
Status
Not open for further replies.
Back
Top