1

I would like to be able to read the serial numbers of SD cards using the SD.h library. Only reading the values is enough, I am not looking to modify the CID. This should be possible but I do not have the programming skills to get it running.

I am using an esp8266 and already have the SD card running and working correctly. Only the chipselect needed to be modified for the SD library to work.

So far I have found this in Sd2Card.h

      /**
   * Read a cards CID register. The CID contains card identification
   * information such as Manufacturer ID, Product name, Product serial
   * number and Manufacturing date. */
  uint8_t readCID(cid_t* cid) {
    return readRegister(CMD10, cid);
  }
  /**

And in Sd2Card.cpp there is this:

/** read CID or CSR register */
uint8_t Sd2Card::readRegister(uint8_t cmd, void* buf) {
  uint8_t* dst = reinterpret_cast<uint8_t*>(buf);
  if (cardCommand(cmd, 0)) {
    error(SD_CARD_ERROR_READ_REG);
    goto fail;
  }
  if (!waitStartBlock()) goto fail;
  // transfer data
  for (uint16_t i = 0; i < 16; i++) dst[i] = spiRec();
  spiRec();  // get first crc byte
  spiRec();  // get second crc byte
  chipSelectHigh();
  return true;

 fail:
  chipSelectHigh();
  return false;
}

So it seems that the underlying code has been prepared in the library already, my question is how would I include it into a sketch like CardInfo.ino?

1

The method is visible from within your sketch. You just need to first create a cid_t struct like this in your sketch:

cid_t cid;  // at the top with the other globals

Then call readCID() anywhere in setup() after the card has been initialized.

card.readCID(&cid);
Serial.print("Serial number: "); Serial.println(cid.psn); // psn is the serial number field of the struct

Check out SdInfo.h for the other struct members.

1
  • Thank you, this is working for me and the explanation easy to follow. I tried additional information and can also read it. The SdInfo sketch is much better but it wouldn't run on my hardware, but I can see now how to use it as an example. Thanks again. – mochaMame Jun 25 '17 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.