Thread: Raw volume access to SD Card (USB adapter)

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    7

    Raw volume access to SD Card (USB adapter)

    Hello,

    I'm new in this Forum and has following Issue:
    I need to read and write to an sd Card at raw level (without FAT). I read and write Data Sectorwise (512Bytes) with a µC to the SD Card. This Data I need on the PC. It's a SD card in a card reader. There are API Funktions to do that, but I never used API Funktions. I found an old Thread in this Forum: Raw volume access (to a usb volume)
    Has anybody solved this?

    Thanks!

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    CreateFile Function (Windows)

    Read the section on Physical Disks and Volumes.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Thanks for the reply!

    I use followinf funktion:
    Code:
    void tmain(void)
    {
        HANDLE hFile; 
        DWORD dwBytesRead = 0;
        char ReadBuffer[BUFFER_SIZE] = {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)\n", GetLastError());
            return; 
        }
    
        // 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)\n", GetLastError());
            CloseHandle(hFile);
            return;
        }
    
        if (dwBytesRead > 0)
        {
            ReadBuffer[dwBytesRead+1]='\0'; // NULL character
    
            _tprintf(TEXT("Text read from %s (%d bytes): \n"), dwBytesRead);
            printf("%s\n", ReadBuffer);
        }
        else
        {
            _tprintf(TEXT("No data read from file %s\n"));
        }
    
        CloseHandle(hFile);
    }
    But it always jumps into this Funktion
    Code:
    if (hFile == INVALID_HANDLE_VALUE)
    because of hFile is 0xffffffff

    What can I do?

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Does nowbody have an idea?
    Now I use following parameters for this function:
    Code:
    hFile = CreateFile("\\.\E:",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_FLAG_NO_BUFFERING,NULL);
    But I'm not able to open the volume??

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    remember that visual studio views characters preceded by a \ as a switch. The proper text is
    Code:
    hFile = CreateFile("\\\\.\\E:",GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Thanks a lot!!
    It worked!

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Hello,

    I have one more question to this issue.
    Following code example works fine to read all data from the SD-Card. Each reading goes one sector forward. But how can I read from different sectors, for example sector 34?

    Code:
    	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
    				if( FALSE == ReadFile(hFile, ReadBuffer, BUFFER_SIZE, &dwBytesRead, NULL) )
    				{
    					CloseHandle(hFile);
    					return;
    				}
    
    				if (dwBytesRead > 0)
    				{
    
    					String ^ str = gcnew String(ReadBuffer);
    					textBox1->AppendText(str);
    
    					while(ReadBuffer[2] != 0x00) // reads until the end
    					{
    						ReadFile(hFile, ReadBuffer, BUFFER_SIZE, &dwBytesRead, NULL);
    						String ^ str = gcnew String(ReadBuffer);
    						textBox1->AppendText(str);
    					}
    				}
    				else
    				{
    					MessageBox::Show("No data read from volume");
    				}
    		 }

  8. #8
    Registered User
    Join Date
    Nov 2009
    Posts
    7
    Hello,
    I figured it out how to read sectorwise, with the SetFilePointer API.
    Now my last problem is, how can I read out the filesystem?
    I tried it with the API GetVolumeInformation, but it only works on c: or my CDROM drive, but not with the SD-Card?
    My goal is, to get back the filesystemname "RAW" from the SD-Card like the information from the properties of windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vector out of range program crash.
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 01-18-2008, 05:37 PM
  2. C functions to access USB port.
    By nethunter in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-10-2006, 02:25 PM
  3. How to Access Ethernet Card From C++
    By DrMario in forum C++ Programming
    Replies: 6
    Last Post: 03-24-2005, 05:38 PM
  4. Onboard video card memory access
    By HermioneFan in forum Game Programming
    Replies: 1
    Last Post: 05-28-2003, 09:53 AM
  5. How to access Network Card using C
    By vadiv in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-17-2001, 01:47 AM