Answered ReadFile() is not reading the SD card data

  • 2012년 4월 13일 금요일 오전 10:26
     
     

    Hi deall all, Please help me on the the issue of ReadFile(). I have written the program in the vc++ to read the sd card raw data. The code as follows. The program is giving the some junk data. Please help me out by pointing the right mistake. Thanks a lot.

    =========================================================

    #include <windows.h>
    #include <stdio.h>
    #include <stddef.h>
    #include <ntddscsi.h>
    #include <winioctl.h>
    #include <tchar.h>
    #include <strsafe.h>

    #define wszDrive L"\\\\.\\D:"
    #define BUFFERSIZE 256000

    void SendInquiry(HANDLE FileHandle)
    {
    DWORD  dwBytesRead = 0;
    LPVOID   ReadBuffer[BUFFERSIZE];
    //int ReadResult;
    typedef struct
    {
    SCSI_PASS_THROUGH Spt;
    BYTE Sense[18];
    BYTE Data[36];
    } INFO;
    INFO Info = {0};
    DWORD ReturnLen;
    Info.Spt.Length = sizeof(Info.Spt);
    Info.Spt.SenseInfoLength = sizeof(Info.Sense);
    Info.Spt.DataTransferLength = sizeof(Info.Data);
    Info.Spt.SenseInfoOffset = offsetof(INFO, Sense);
    Info.Spt.DataBufferOffset = offsetof(INFO, Data);
    Info.Spt.TimeOutValue = 30;
    Info.Spt.DataIn = SCSI_IOCTL_DATA_IN;
    Info.Spt.CdbLength = 6;
    Info.Spt.Cdb[0] = 0x12; // inquiry opcode
    Info.Spt.Cdb[1] = 0x00;
    Info.Spt.Cdb[2] = 0x00;
    Info.Spt.Cdb[3] = 0x00;
    Info.Spt.Cdb[4] = sizeof(Info.Data);
    Info.Spt.Cdb[5] = 0x00;
    if (DeviceIoControl(FileHandle, IOCTL_SCSI_PASS_THROUGH,&Info, sizeof(Info), &Info, sizeof(Info),&ReturnLen, NULL))
    {
    puts("success\n");

    //ReadResult=ReadFile(FileHandle, ReadBuffer, BUFFERSIZE-1, &dwBytesRead, NULL);

    //printf("The File requested is == %d\n",ReadResult);

    if(ReadFile(FileHandle, ReadBuffer, BUFFERSIZE-1, &dwBytesRead, NULL))
    {
    printf("Terminal failure: Unable to read from file.\n");
    return;
    }
    else
    {
    printf("%s\n", ReadBuffer);
    }

    }
    }
    else
    printf("ioctl failed. Error=%u\n", GetLastError());
    }
    int main( void )
    {
    int a;
    LPWSTR FilePathNameOfSDcard=wszDrive;
    HANDLE FileHandle;
    FileHandle = CreateFile
    (
    FilePathNameOfSDcard, 
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, 
    OPEN_EXISTING, 
    FILE_ATTRIBUTE_NORMAL,
    //FILE_FLAG_OVERLAPPED,
    NULL
    );
    if (FileHandle == INVALID_HANDLE_VALUE)
    printf("Could not open device. Error=%d\n", GetLastError());
    else
    {
    SendInquiry(FileHandle);
    CloseHandle(FileHandle);
    }
    printf("\n");
    scanf_s("%d",&a);
    return(0);
    }

모든 응답

  • 2012년 4월 16일 월요일 오전 8:28
    중재자
     
     답변됨

    Hi,

    You can check the file path in SD cards.

    The file path should be "Store Card\<folder>\file.txt", do not use driver letter.

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

  • 2012년 4월 18일 수요일 오후 4:44
     
     답변됨

    On Mon, 16 Apr 2012 08:28:15 +0000, Jesse Jiang wrote:

    Hi,

    You can check the file path in SD cards.

    The file path should be "Store Card\<folder>\file.txt", do not use driver letter.

    Actually, the root name for a storage card can vary quite a bit. I've encountered
    Storage Card
    Storage_Card
    CF Card
    SD Card
    Memory Card

    And each can have a trailing number it a card is removed and replaced (by same or another
    card).

    Definitely no drive letters.


    Best regards,
    Jesse

    -----------------------------------------
    To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

    Robert E. Zaret
    PenFact, Inc.
    20 Park Plaza, Suite 400
    Boston, MA 02116
    www.penfact.com
    r_z_aret@pen_fact.com
    Useful reading (be sure to read its disclaimer first):
       http://catb.org/~esr/faqs/smart-questions.html

  • 2012년 4월 18일 수요일 오후 4:44
     
     

    On Fri, 13 Apr 2012 10:26:35 +0000, wep1 wrote:

    Hi deall all, Please help me on the the issue of ReadFile(). I have written the program in the vc++ to read the sd card raw data. The code as follows. The program is giving the some junk data.

    ASCII/Unicode may be a problem. If you're reading an ASCII file, you'll need to convert
    all it's content to UNICODE for most string functions in CE/Windows Mobile; see the
    MultiByteToWideChar and WideCharToMultiByte functions. If you're reading a UNICODE file,
    make sure it starts with a BOM (Byte Order Marker).

    Please help me out by pointing the right mistake. Thanks a lot.

    =========================================================

    #include <windows.h>
    #include <stdio.h>
    #include <stddef.h>
    #include <ntddscsi.h>
    #include <winioctl.h>
    #include <tchar.h>
    #include <strsafe.h>

    #define wszDrive L"\\\\.\\D:"
    #define BUFFERSIZE 256000

    void SendInquiry(HANDLE FileHandle)
    {
    DWORD  dwBytesRead = 0;
    LPVOID   ReadBuffer[BUFFERSIZE];
    /int ReadResult;
    typedef struct
    {
    SCSI_PASS_THROUGH Spt;
    BYTE Sense[18];
    BYTE Data[36];
    } INFO; INFO Info = {0};
    DWORD ReturnLen;
    Info.Spt.Length = sizeof(Info.Spt);
    Info.Spt.SenseInfoLength = sizeof(Info.Sense);
    Info.Spt.DataTransferLength = sizeof(Info.Data);
    Info.Spt.SenseInfoOffset = offsetof(INFO, Sense);
    Info.Spt.DataBufferOffset = offsetof(INFO, Data);
    Info.Spt.TimeOutValue = 30;
    Info.Spt.DataIn = SCSI_IOCTL_DATA_IN;
    Info.Spt.CdbLength = 6;
    Info.Spt.Cdb[0] = 0x12; /
    inquiry opcode
    Info.Spt.Cdb[1] = 0x00;
    Info.Spt.Cdb[2] = 0x00;
    Info.Spt.Cdb[3] = 0x00;
    Info.Spt.Cdb[4] = sizeof(Info.Data);
    Info.Spt.Cdb[5] = 0x00;
    if (DeviceIoControl(FileHandle, IOCTL_SCSI_PASS_THROUGH,&Info, sizeof(Info), &Info, sizeof(Info),&ReturnLen, NULL))
    {
    puts("success\n");

    //ReadResult=ReadFile(FileHandle, ReadBuffer, BUFFERSIZE-1, &dwBytesRead, NULL);

    //printf("The File requested is == %d\n",ReadResult);

    if(ReadFile(FileHandle, ReadBuffer, BUFFERSIZE-1, &dwBytesRead, NULL))
    {
    printf("Terminal failure: Unable to read from file.\n");
    return;
    }
    else
    {
    printf("%s\n", ReadBuffer);
    }

    }
    } else
    printf("ioctl failed. Error=%u\n", GetLastError());
    }
    int main( void )
    {
    int a;
    LPWSTR FilePathNameOfSDcard=wszDrive;
    HANDLE FileHandle;
    FileHandle = CreateFile
    (
    FilePathNameOfSDcard, 
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, 
    OPEN_EXISTING, 
    FILE_ATTRIBUTE_NORMAL,
    //FILE_FLAG_OVERLAPPED,
    NULL
    );
    if (FileHandle == INVALID_HANDLE_VALUE)
    printf("Could not open device. Error=%d\n", GetLastError());
    else
    {
    SendInquiry(FileHandle);
    CloseHandle(FileHandle);
    }
    printf("\n");
    scanf_s("%d",&a);
    return(0);
    }

    -----------------------------------------
    To reply to me, remove the underscores (_) from my email address (and please indicate which newsgroup and message).

    Robert E. Zaret
    PenFact, Inc.
    20 Park Plaza, Suite 400
    Boston, MA 02116
    www.penfact.com
    r_z_aret@pen_fact.com
    Useful reading (be sure to read its disclaimer first):
       http://catb.org/~esr/faqs/smart-questions.html

  • 2012년 4월 19일 목요일 오전 7:35
     
     
    Since the original post code uses 'D:' for the storage card maybe the question is about Windows desktop development?

    Luca Calligaris lucaDOTcalligarisATeurotechDOTcom www.eurotech.com Check my blog: http://lcalligaris.wordpress.com