I would like to get the serial numbers of SD cards, with follow code:
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
cid_t cid; // *****at the top with the other globals (for get cid)
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
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 Leonardo only
}
Serial.print("\nInitializing SD card...");
// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
return;
} else {
Serial.println("Wiring is correct and a card is present.");
}
// print the type of card
Serial.print("\nCard type: ");
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println("SD1");
break;
case SD_CARD_TYPE_SD2:
Serial.println("SD2");
break;
case SD_CARD_TYPE_SDHC:
Serial.println("SDHC");
break;
default:
Serial.println("Unknown");
}
// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
return;
}
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("\nVolume type is FAT");
Serial.println(volume.fatType(), DEC);
Serial.println();
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize *= 512; // SD card blocks are always 512 bytes
Serial.print("Volume size (bytes): ");
Serial.println(volumesize);
Serial.print("Volume size (Kbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (Mbytes): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.println();
// *****list all files in the card with date and size
//Serial.println("\nFiles found on the card (name, date and size in bytes): ");
//root.openRoot(volume);
//root.ls(LS_R | LS_DATE | LS_SIZE);
card.readCID(&cid);
Serial.print("Serial number: "); Serial.println(cid.src); // psn is the serial number field of the struct
}
void loop(void) {
}
I tried to get serial from SD card but I only get this "Serial number: 2065301974". I really need it in hex format, for example "0x0033515c".
Also, I would like to get the complete CID like this:
CID: 0x1B, 0x53, 0x4D, 0x30, 0x30, 0x30, 0x30, 0x30, 0x10, 0x45, 0x5D, 0x04, 0xE5, 0x00, 0xF9, 0x69
-
TLDR. Is your question "how do I convert a decimal string into a hex number"?Mark Smith– Mark Smith2018年02月28日 15:22:35 +00:00Commented Feb 28, 2018 at 15:22
-
I see no "src" in the cid_t structure....Majenko– Majenko2018年02月28日 16:36:36 +00:00Commented Feb 28, 2018 at 16:36
-
the CID is not a true "serial number" it's easily hackable. don't use this for security.dandavis– dandavis2018年02月28日 18:09:34 +00:00Commented Feb 28, 2018 at 18:09
2 Answers 2
cid_t
is a structure that has all sorts of information in it in different formats. The serial number (psn
) is just a 32-bit unsigned integer. You can print it as hex by printing it as hex.
Serial.println(cid.psn, HEX);
If you want to get the raw data of the structure (which I assume may correspond to the raw data read from the card, but don't quote me on that) you can access the struct as 8-bit values:
uint8_t *p = (uint8_t *)&cid;
for (int i = 0; i < sizeof(cid); i++) {
Serial.print("0x");
Serial.print(p[i], HEX);
Serial.print(" ");
}
Serial.println();
-
Thank you for your help. I tried this and I got: 12 10 17 4D 49 42 53 54 2 0 0 5 A2 0 19 11 0 0 0 0 I know that is wrong because I got serial of sd card on linux and the real serial number ir "0x000005a2" and what I really want is the complete cid number as shown here a.d-cd.net/ec9effas-960.jpg I´m very grateful –Marco– Marco2018年03月02日 15:17:17 +00:00Commented Mar 2, 2018 at 15:17
-
These are the data obtained from Linux: cid: 041641534d492d534403000000001601 serial: 0x03000000 And these are the data obtained from ESP8266: cid (with your code): 1210174E4361726410001C5019AF0000 serial (whit cid.psn): 0xAF1900C5 Then, apparently it is not showing the correct dada... I think something is wrong with the code. Please help me.Marco– Marco2018年03月02日 18:30:40 +00:00Commented Mar 2, 2018 at 18:30
-
How are you getting the data in Linux?Majenko– Majenko2018年03月02日 18:51:15 +00:00Commented Mar 2, 2018 at 18:51
-
The CID I find in this file SYS->BLOCK->MMCBLK0->DEVICE->cid and the serial I find in this file SYS->BLOCK->MMCBLK0->DEVICE->serialMarco– Marco2018年03月02日 19:46:41 +00:00Commented Mar 2, 2018 at 19:46
-
Are you sure that you are looking at the right device?Majenko– Majenko2018年03月02日 20:03:58 +00:00Commented Mar 2, 2018 at 20:03
To print the serial number in hex Serial.println accepts a format parameter:
Serial.println(cid.psn, HEX);
The cid variable is a struct so you can access the the other fields via the structure. But if you really want to access the raw bytes you could cast cid to a pointer then access the individual bytes:
uint8_t *rawcid = reinterpret_cast<uint8_t*>(&cid);
Serial.println(*(cid+3), HEX); // print the 4th byte
Be aware that casting like this can be tricky and you will likely shoot yourself in the foot more than once.
-
Thank you for your help. I tried this and I got: 12 10 17 4D 49 42 53 54 2 0 0 5 A2 0 19 11 0 0 0 0 I know that is wrong because I got serial of sd card on linux and the real serial number ir "0x000005a2" and what I really want is the complete cid number as shown here a.d-cd.net/ec9effas-960.jpg I´m very gratefulMarco– Marco2018年03月02日 14:15:23 +00:00Commented Mar 2, 2018 at 14:15
-
The serial number is just a small part of the Cid. You can see 0 0 5 A2 which as uint32_t is 000005a2 in the string of bytes you have.Majenko– Majenko2018年03月02日 14:55:45 +00:00Commented Mar 2, 2018 at 14:55
-
These are the data obtained from Linux: cid: 041641534d492d534403000000001601 serial: 0x03000000 And these are the data obtained from ESP8266: cid (with your code): 1210174E4361726410001C5019AF0000 serial (whit cid.psn): 0xAF1900C5 Then, apparently it is not showing the correct dada... I think something is wrong with the code. Please help me.Marco– Marco2018年03月02日 18:29:52 +00:00Commented Mar 2, 2018 at 18:29