Unformatierte SD-Card lesen

  • Allgemein

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von UlliS.

    Unformatierte SD-Card lesen

    Hallo zusammen,

    ich möchte gerne den Inhalt einer SD-Card Byte für Byte oder Sektor für Sektor einlesen (RAW daten).
    Wieso ich das muss: Ich schreibe mit einen Mikrocontroller RAW Daten auf die SD-Card (Direktes schreiben) ohne das ich diese zuvor mit einen FAT-System formatiere.
    Ich kann die Daten über den Mikrocontroller wieder auslesen, da ich hier vollen Zugriff auf die Karte habe. Leider habe ich keine Ahnung wie ich das in VB6 oder VB.NET realisieren soll.

    Hat von Euch jemand Erfahrung oder so etwas schon mal gemacht?

    Ich hoffe ich habe die Frage einigermaßen verständlich gestellt.


    Grüße Ulli
    Ich habs jetzt mal in VB6 versucht, aber so ganz will das noch nicht :(
    Hier mal der Code vieleicht hast du einen Tipp was ich falsch mache, "J:" ist meine SD Karte.


    VB.NET-Quellcode

    1. Option Explicit
    2. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    3. Private Declare Function SetFilePointer Lib "kernel32" (ByVal hFile As Long, ByVal lDistanceToMove As Long, lpDistanceToMoveHigh As Long, ByVal dwMoveMethod As Long) As Long
    4. Private Declare Function ReadFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, ByVal lpOverlapped As Long) As Long
    5. Private Declare Function WriteFile Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToWrite As Long, lpNumberOfBytesWritten As Long, ByVal lpOverlapped As Long) As Long
    6. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    7. Private Declare Function FlushFileBuffers Lib "kernel32" (ByVal hFile As Long) As Long
    8. Private Declare Function LockFile Lib "kernel32" (ByVal hFile As Long, ByVal dwFileOffsetLow As Long, ByVal dwFileOffsetHigh As Long, ByVal nNumberOfBytesToLockLow As Long, ByVal nNumberOfBytesToLockHigh As Long) As Long
    9. Private Declare Function UnlockFile Lib "kernel32" (ByVal hFile As Long, ByVal dwFileOffsetLow As Long, ByVal dwFileOffsetHigh As Long, ByVal nNumberOfBytesToUnlockLow As Long, ByVal nNumberOfBytesToUnlockHigh As Long) As Long
    10. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    11. Private Const GENERIC_READ = &H80000000
    12. Private Const GENERIC_WRITE = &H40000000
    13. Private Const FILE_SHARE_READ = &H1
    14. Private Const FILE_SHARE_WRITE = &H2
    15. Private Const OPEN_EXISTING = 3
    16. Private Const INVALID_HANDLE_VALUE = -1&
    17. Private Const BytesPerSector = 512
    18. Private Const FILE_BEGIN = 0
    19. Private Function DirectWriteDriveNT(ByVal sDrive As String, ByVal iStartSec As Long, ByVal iOffset As Long, ByVal sWrite As String) As Boolean
    20. Dim hDevice As Long
    21. Dim abBuff() As Byte
    22. Dim nSectors As Long
    23. Dim nRead As Long
    24. Dim ab() As Byte
    25. nSectors = Int((iOffset + Len(sWrite) - 1) / BytesPerSector) + 1
    26. hDevice = CreateFile(sDrive, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
    27. If hDevice = INVALID_HANDLE_VALUE Then Exit Function
    28. abBuff = DirectReadDriveNT(sDrive, iStartSec, 0, nSectors * BytesPerSector)
    29. ab = StrConv(sWrite, vbFromUnicode)
    30. CopyMemory abBuff(iOffset), ab(0), Len(sWrite)
    31. Call SetFilePointer(hDevice, iStartSec * BytesPerSector, 0, FILE_BEGIN)
    32. Call LockFile(hDevice, LoWord(iStartSec * BytesPerSector), HiWord(iStartSec * BytesPerSector), LoWord(nSectors * BytesPerSector), HiWord(nSectors * BytesPerSector))
    33. DirectWriteDriveNT = WriteFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
    34. Call FlushFileBuffers(hDevice)
    35. Call UnlockFile(hDevice, LoWord(iStartSec * BytesPerSector), HiWord(iStartSec * BytesPerSector), LoWord(nSectors * BytesPerSector), HiWord(nSectors * BytesPerSector))
    36. CloseHandle hDevice
    37. End Function
    38. Private Function DirectReadDriveNT(ByVal sDrive As String, ByVal iStartSec As Long, ByVal iOffset As Long, ByVal cBytes As Long) As Variant
    39. Dim hDevice As Long
    40. Dim nSectors As Long
    41. Dim nRead As Long
    42. Dim abResult() As Byte
    43. nSectors = Int((iOffset + cBytes - 1) / BytesPerSector) + 1
    44. hDevice = CreateFile(sDrive, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, 0&, OPEN_EXISTING, 0&, 0&)
    45. If hDevice = INVALID_HANDLE_VALUE Then Exit Function
    46. Call SetFilePointer(hDevice, iStartSec * BytesPerSector, 0, FILE_BEGIN)
    47. ReDim abResult(cBytes - 1)
    48. ReDim abBuff(nSectors * BytesPerSector - 1)
    49. Call ReadFile(hDevice, abBuff(0), UBound(abBuff) + 1, nRead, 0&)
    50. CloseHandle hDevice
    51. CopyMemory abResult(0), abBuff(iOffset), cBytes
    52. DirectReadDriveNT = abResult '(0)
    53. End Function
    54. Private Function LoWord(ByVal dw As Long) As Integer
    55. If dw And &H8000& Then
    56. LoWord = dw Or &HFFFF0000
    57. Else
    58. LoWord = dw And &HFFFF&
    59. End If
    60. End Function
    61. Private Function HiWord(ByVal dw As Long) As Integer
    62. HiWord = (dw And &HFFFF0000) \ 65536
    63. End Function
    64. Private Function hex2ascii(ByVal hextext As String) As String
    65. Dim y As Integer
    66. Dim num
    67. Dim Value
    68. For y = 1 To Len(hextext)
    69. num = Mid(hextext, y, 2)
    70. Value = Value & Chr(Val("&h" & num))
    71. y = y + 1
    72. Next y
    73. hex2ascii = Value
    74. End Function
    75. Private Sub Command1_Click()
    76. 'DirectReadDriveNT(ByVal sDrive As String, ByVal iStartSec As Long, ByVal iOffset As Long, ByVal cBytes As Long) As Variant
    77. Text1.Text = DirectReadDriveNT("\\?\J:", 0, 0, 1)
    78. End Sub
    79. Private Sub Command2_Click()
    80. ' Write hex "EB" to the first byte of the partition boot sector of volume H:
    81. DirectWriteDriveNT "\\?\J:", 0, 0, hex2ascii("eb")
    82. End Sub



    Oh das wusste ich gar nicht das bei den Longs ein Unterschied besteht, wo liegt der genau?

    Danke nochmals für die Hilfe!


    Grüße Ulli
    Hi
    Longs sind in VB6 Int32 (also Ganzzahlen, die aus 32 Bits bestehen). Diese haben den Maximalwert 2^31 - 1 und den Minimalwert - 2^31 (Somit also 2^32 verschiedene Werte). In VB.Net ist ein Long ein Int64 und somit eine Ganzzahl, die aus 64 Bits besteht. Somit sind halt dann die Werte -2^63 bis 2^63 -1 möglich.

    Gruß
    ~blaze~