C program to read and write raw data on SD card

17,902

Circuit Image

Read and write raw data from an SD card using a computer without a microprocessor, bypassing the FAT32 file system. The goal is to create a C script that can store specific parameters on the SD card for future use by a microprocessor, allowing for data storage without direct computer connections. Assistance is needed with the CreateFile() and ReadFile() functions in Windows, as the user is facing challenges with the code provided. The code attempts to open the SD card, read data, and display it, but encounters errors. There is a suggestion that accessing raw data outside a file system may require low-level I/O calls (ioctls) and that using FAT32 could simplify programming. The user is also interested in maximizing the storage capacity of the SD card.

The task involves reading and writing raw data from an SD card directly using a computer, without relying on a microprocessor or the FAT32 file system. This approach requires a basic understanding of the Windows API, specifically the CreateFile() and ReadFile() functions, which are essential for file operations in a Windows environment. The provided code snippet demonstrates an attempt to open the SD card as a file and read data from it. However, errors in the code prevent successful execution, particularly in the areas of file handling and data reading.

To effectively read and write to the SD card, it is necessary to ensure that the device is correctly identified and opened. The CreateFile() function should be called with the correct device path (e.g., "\\.\E:") and appropriate access flags (GENERIC_READ | GENERIC_WRITE). The code must also check if the handle returned by CreateFile() is valid, as an invalid handle indicates that the device could not be opened.

Once the SD card is successfully opened, the ReadFile() function can be employed to read data into a buffer. It is important to allocate sufficient space in the buffer and to ensure that the read operation accounts for the null-terminating character. The error handling mechanism must be robust, capturing any issues during the read operation and providing meaningful feedback to the user.

For applications requiring raw data access, low-level I/O operations (ioctls) may be necessary. These operations involve direct communication with device drivers and require a deeper understanding of the Windows operating system's architecture. Alternatively, utilizing the FAT32 file system could simplify the process, as it is widely supported and allows for easier data management.

In the context of microcontroller applications, interfacing with an SD card can be accomplished via SPI (Serial Peripheral Interface), allowing for straightforward command and data transmission. This method may provide a more accessible means of accessing SD card data without delving into complex low-level programming.

In conclusion, while direct access to raw data on an SD card presents challenges, utilizing the appropriate Windows API functions and considering the use of FAT32 can facilitate the development of a functional C program for reading and writing data.Read and write the raw data from an SD card using just a computer (no microprocessor) forgetting the FAT32. I would like to do this in basic C. I can look at the raw data with a program called winhex. Long run I want someone who is less computer savvy to run a C script that will store certain parameters on SD card (where I say so) that the microprocessor

would use. Then store data on it allowing me to never have to connect directly to a computer. I`m having a hard time getting started with this program partly because I`m not even sure what to search for (I keep getting sights trying to sell me stuff). So if anyone has knowledge of the CreateFile() and ReadFile() you help would be great. Here is my code so far with problem area commented (Windows XP, CodeBolcks10. 05) mostly from some people #include #include #include #include #include #include #define BUFFER_SIZE 8 int main(void) { HANDLE hFile; DWORD dwBytesRead = 1; char ReadBuffer[BUFFER_SIZE+1] = {0}; hFile = CreateFile("\\.

\E:", GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile = INVALID_HANDLE_VALUE) { printf("Could not open file (error %d) ", GetLastError(); return 1; } /// Code isent getting past next step or reading data /// // Read one character less than the buffer size to save room for // the terminating NULL character. if( FALSE = ReadFile(hFile, ReadBuffer, BUFFER_SIZE-2, &dwBytesRead, NULL) ) { printf("Could not read from file (error %d) ", GetLastError(); CloseHandle(hFile); return 2; } if (dwBytesRead > 0) { ReadBuffer[dwBytesRead+1]=``; // NULL character printf(TEXT("Text read from %s (%d bytes): "), dwBytesRead); printf("%s ", ReadBuffer); } else { printf(TEXT("No data read from file %s "); } CloseHandle(hFile); return 0; } So it`s (I`m) having a hard time reading data.

I like trouble shooting programming problems by breaking code up but I don`t even know where to start with this problem, any ideas Thanks To access raw data outside a file system you have to use ioctl`s which are low level io calls that get passed directly to device drivers. Typically information is passed in the ioctl with pointers to various system structures and likewise returned.

It`s a little tougher than regular application level programming. Seems to me it would be easier to go ahead and use FAT to keep the PC programming simpler and it won`t be too hard on the AVR side either because it`s been done many times and there are several open source libraries on the net. Actually I`m not so sure about having to use ioctls to read/write block devices. But probably you should start by opening the device as input instead of create and just get reading to work before moving on to writing/formatting.

If you can write it you can zero/format it yourself You are probably right with just using FAT32, but I would just like to be able to get all 8GB from my 8GB card, not 7. 6GB. just something I would like to be able to do. I will take a look at the linux stuff when I get a chance, thanks for the lead. On Windows, I`m pretty sure you need to be in Kernel mode to get the kind of access you`re talking about.

That means having to write a driver for your "User" mode program to go through to access the SD card. That`s a tall order. Oddly enough, using the MCU is easy. You can hook up an SD card to it on the SPI lines and just pass through PC commands/data on the UART Tx/Rx lines.

There are some links to more information HERE. I would still go with FAT and just use a 16/32/64gb card if I needed the extra. 4gb storage. Never know, might want to read that card from your cell phone or whatever some day and they all like file systems. But on the other hand, it is fun to be able to do exactly what you want and I always enjoyed learning/using the less common (and more difficult) pr

🔗 External reference




Warning: include(partials/cookie-banner.php): Failed to open stream: Permission denied in /var/www/html/nextgr/view-circuit.php on line 713

Warning: include(): Failed opening 'partials/cookie-banner.php' for inclusion (include_path='.:/usr/share/php') in /var/www/html/nextgr/view-circuit.php on line 713