C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-02-2009, 09:11 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 6
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!
hans75 is offline   Reply With Quote
Old 11-02-2009, 02:48 PM   #2
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
CreateFile Function (Windows)

Read the section on Physical Disks and Volumes.
abachler is offline   Reply With Quote
Old 11-03-2009, 01:24 PM   #3
Registered User
 
Join Date: Nov 2009
Posts: 6
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?
hans75 is offline   Reply With Quote
Old 11-08-2009, 09:30 AM   #4
Registered User
 
Join Date: Nov 2009
Posts: 6
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??
hans75 is offline   Reply With Quote
Old 11-09-2009, 01:32 AM   #5
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 2,927
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);
__________________
He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet
abachler is offline   Reply With Quote
Old 11-09-2009, 11:30 AM   #6
Registered User
 
Join Date: Nov 2009
Posts: 6
Thanks a lot!!
It worked!
hans75 is offline   Reply With Quote
Old 11-11-2009, 01:16 PM   #7
Registered User
 
Join Date: Nov 2009
Posts: 6
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");
				}
		 }
hans75 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22