All Forums
 Microsoft Windows CE
 CE Sample Applications and Utilities
 C# - ReadFlashID (Flash Serial Number)
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Author Topic  

ctacke

877 Posts

Posted - 11 Nov 2003 :  10:55:34  Show Profile  Email Poster
Read Flash Serial Number - C#

Below is a code snippet showing how to read the 64-bit serial number of the on-board flash of some ADS systems (see Topic 1242 for support info).


Declaration

public class Flash
{
public static string FlashID
{
get
{
uint bytesreturned = 0;
byte[] buffer = new byte[ 8 ];
byte[] nullbuffer = new byte[ 1 ];

string id = "";
IntPtr hFlash = CreateFile("FSH1:", GENERIC_WRITE, 0, IntPtr.Zero, 0, OPEN_EXISTING, IntPtr.Zero);

if(hFlash.ToInt32() == INVALID_HANDLE_VALUE)
{
throw new Exception("Error opening Flash driver");
}

int ret = DeviceIoControl(hFlash, IOCTL_READ_FLASH_ID, nullbuffer, 0, buffer, 8, ref bytesreturned, IntPtr.Zero);

if((ret == 0))
{
throw new Exception("DeviceIoControl call failed: " + Marshal.GetLastWin32Error());
}

for(int i = 0 ; i < 4 ; i++)
{
id += String.Format("{0:X4}", BitConverter.ToUInt16(buffer, i * 2));
if(i < 3)
{
id += " ";
}
}

return id;
}
}

private const uint IOCTL_READ_FLASH_ID = 0x10;
private const uint GENERIC_WRITE = 0x40000000;
private const int OPEN_EXISTING = 3;
private const int INVALID_HANDLE_VALUE = -1;

[DllImport("coredll.Dll")]
private static extern int DeviceIoControl(IntPtr hDevice,
uint dwIoControlCode,
byte[] lpInBuffer,
uint nInBufferSize,
byte[] lpOutBuffer,
uint nOutBufferSize,
ref uint lpBytesReturned,
IntPtr lpOverlapped);

[DllImport("coredll.dll")]
private static extern IntPtr CreateFile(string lpFileName,
uint dwDesiredAccess,
int dwShareMode,
IntPtr lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
IntPtr hTemplateFile);

[DllImport("coredll.dll")]
private static extern bool CloseHandle(IntPtr hObject);
}



Sample Usage

static void Main(string[] args)
{
try
{
Console.WriteLine("Flash ID = " + Flash.FlashID);
}
catch(Exception ex)
{
Console.WriteLine("Failed to get Flash ID\r\n" + ex.Message);
}
Console.ReadLine();
}

janetjek

2 Posts

Posted - 23 Apr 2007 :  14:10:07  Show Profile
I tried this on a BitsyXB and bytesreturned is always 0.
I found a reference in the ADSmartIO that uses a different IOCTL code but it also always had bytesreturned=0. Any suggestions?

Thanks.
Go to Top of Page

jnosek

3 Posts

Posted - 23 Apr 2007 :  15:02:39  Show Profile  Email Poster
What BitsyXb build are you using?

Try using the latest build.
Go to Top of Page
  Topic  
 Forum Locked
 Send Topic to a Friend
 Printer Friendly
Jump To:
Eurotech Support Forums © Eurotech Inc. Go To Top Of Page
This page was generated in 0.03 seconds. Snitz Forums 2000