Reading a raw data from SD Card
-
Tuesday, April 10, 2012 6:56 AM
Dear all, I am developing the project to read raw data from the 2GB SD Card in C#. My Task is to read raw data from sd-card then put it in the structure and upload it to the data base and then generate the excel files from the data base based on the user inputs. I am trying to read the raw data from sd card on PC side. I have a code where I am trying to read the sd card.
namespace USBSharp
{
public partial class Form1 : Form
{
public int HidHandle;
public int hDevInfo = -1;
public string DevicePathName = "";
public const int DIGCF_PRESENT = 0x00000002;
public const int DIGCF_DEVICEINTERFACE = 0x00000010;
public const int DIGCF_INTERFACEDEVICE = 0x00000010;
public const uint GENERIC_READ = 0x80000000;
public const uint GENERIC_WRITE = 0x40000000;
public const uint FILE_SHARE_READ = 0x00000001;
public const uint FILE_SHARE_WRITE = 0x00000002;
public const int OPEN_EXISTING = 3;
public const int EV_RXFLAG = 0x0002;
public string DeviceName = "\\\\.\\D:";
public int InputReportByteLength = 512 * 500;
// specified in DCB
public int INVALID_HANDLE_VALUE;
public const int ERROR_INVALID_HANDLE = 6;
public const int FILE_FLAG_OVERLAPPED = 0x40000000;
[StructLayout(LayoutKind.Sequential)]
public struct OVERLAPPED
{ public int Internal;
public int InternalHigh;
public int Offset;
public int OffsetHigh;
public int hEvent;
}
public OVERLAPPED ovl = new OVERLAPPED();
// DLL Calls
[DllImport("kernel32.dll", SetLastError = true)]
private static extern int CreateFile
(string lpFileName, // file name
uint dwDesiredAccess, // access mode
uint dwShareMode, // share mode
uint lpSecurityAttributes, // SD
uint dwCreationDisposition, // how to create
uint dwFlagsAndAttributes, // file attributes
uint hTemplateFile // handle to template file
);
[DllImport("kernel32.dll", SetLastError = true)]
public unsafe static extern bool ReadFile
(
int hFile, // handle to file
byte[] lpBuffer, // data buffer
int nNumberOfBytesToRead, // number of bytes to read
ref int lpNumberOfBytesRead, // number of bytes read
ref OVERLAPPED lpOverlapped // overlapped buffer
);
private void btnUsbData(object sender, EventArgs e)
{
HidHandle = CreateFile( DeviceName,
GENERIC_READ,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0
);
int BytesRead = 0;
byte[] BufBytes = new byte[InputReportByteLength];
if (true == ReadFile(HidHandle, BufBytes, InputReportByteLength, ref BytesRead, ref ovl))
{ byte[] OutBytes = new byte[BytesRead];
Array.Copy(BufBytes, OutBytes, BytesRead);
string FileReadHexDump = HexDump(OutBytes, 16);
rtbUsbDataShow.Text = FileReadHexDump;
}
else
rtbUsbDataShow.Text = "File Read Failed";
}
public Form1()
{
InitializeComponent();
}
public static string HexDump(byte[] bytes, int bytesPerLine)
{
if (bytes == null) return "<null>";
int bytesLength = bytes.Length;
char[] HexChars = "0123456789ABCDEF".ToCharArray();
int firstHexColumn =
8 // 8 characters for the address
+ 3; // 3 spaces
int firstCharColumn = firstHexColumn
+ bytesPerLine * 4 // - 2 digit for the hexadecimal value and 2 space
+ (bytesPerLine - 1) / 8 // - 1 extra space every 8 characters from the 9th
+ 2; // 2 spaces
int lineLength = firstCharColumn
+ bytesPerLine // - characters to show the ascii value
+ Environment.NewLine.Length; // Carriage return and line feed (should normally be 2)
char[] line = (new String(' ', lineLength - 2) + Environment.NewLine).ToCharArray();
int expectedLines = (bytesLength + bytesPerLine - 1) / bytesPerLine;
StringBuilder result = new StringBuilder(expectedLines * lineLength);
for (long i = 512*217; i < bytesLength; i += bytesPerLine)
{
line[0] = HexChars[(i >> 28) & 0xF];
line[1] = HexChars[(i >> 24) & 0xF];
line[2] = HexChars[(i >> 20) & 0xF];
line[3] = HexChars[(i >> 16) & 0xF];
line[4] = HexChars[(i >> 12) & 0xF];
line[5] = HexChars[(i >> 8) & 0xF];
line[6] = HexChars[(i >> 4) & 0xF];
line[7] = HexChars[(i >> 0) & 0xF];
int hexColumn = firstHexColumn;
int charColumn = firstCharColumn;
for (int j = 0; j < bytesPerLine; j++)
{
if (j > 0 && (j & 7) == 0) hexColumn++;
if (i + j >= bytesLength)
{
line[hexColumn] = ' ';
line[hexColumn + 1] = ' ';
line[charColumn] = ' ';
}
else
{
byte b = bytes[i + j];
line[hexColumn] = HexChars[(b >> 4) & 0xF];
line[hexColumn + 1] = HexChars[b & 0xF];
line[charColumn] = (b < 32 ? '·' : (char)b);
}
hexColumn += 3;
charColumn++;
}
result.Append(line);
}
return result.ToString();
}
}
}Please let me know where i am mistake...... I am always getting the message "File Read Failed".
Thanks a lot.
regards
wep1
All Replies
-
Tuesday, April 10, 2012 10:09 AM
you have open the device handle for the asynchronous operation and checking the return value of ReadFile against true. In case of pending io operation ReadFile returns false even though function does not fail. So it is better to call the GetLastError() for the error operation and do accordingly. I'll suggest you take a look on asynchronous file operation sample.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365690(v=vs.85).aspx


