Read Flash Serial Number - VB.NET
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 Shared ReadOnly Property FlashID() As String
Get
Dim bytesreturned As Integer = 0
Dim buffer(8) As Byte
Dim nullbuffer(1) As Byte
Dim id As String = ""
Dim hFlash As IntPtr = CreateFile("FSH1:", GENERIC_WRITE, 0, IntPtr.Zero, 0, OPEN_EXISTING, IntPtr.Zero)
If hFlash.ToInt32 = INVALID_HANDLE_VALUE Then
Throw New Exception("Error opening Flash driver")
End If
Dim ret As Integer = DeviceIoControl(hFlash, IOCTL_READ_FLASH_ID, nullbuffer, 0, buffer, 8, bytesreturned, IntPtr.Zero)
If ret = 0 Then
Throw New Exception("DeviceIoControl call failed: " + Marshal.GetLastWin32Error())
End If
Dim i As Integer
For i = 0 To 3
id += [String].Format("{0:X4}", BitConverter.ToUInt16(buffer, i * 2))
If i < 3 Then
id += " "
End If
Next i
Return id
End Get
End Property
Private Shared IOCTL_READ_FLASH_ID As Integer = &H10
Private Shared GENERIC_WRITE As Integer = &H40000000
Private Shared OPEN_EXISTING As Integer = 3
Private Shared INVALID_HANDLE_VALUE As Integer = -1
_
Private Shared Function DeviceIoControl(ByVal hDevice As IntPtr, _
ByVal dwIoControlCode As Integer, _
ByVal lpInBuffer() As Byte, _
ByVal nInBufferSize As Integer, _
ByVal lpOutBuffer() As Byte, _
ByVal nOutBufferSize As Integer, _
ByRef lpBytesReturned As Integer, _
ByVal lpOverlapped As IntPtr) As Integer
End Function
_
Private Shared Function CreateFile(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Integer, _
ByVal dwShareMode As Integer, _
ByVal lpSecurityAttributes As IntPtr, _
ByVal dwCreationDisposition As Integer, _
ByVal dwFlagsAndAttributes As Integer, _
ByVal hTemplateFile As IntPtr) As IntPtr
End Function
_
Private Shared Function CloseHandle(ByVal hObject As IntPtr) As Boolean
End Function
End Class
Sample Usage
Sub Main()
Try
Console.WriteLine(("Flash ID = " + Flash.FlashID))
Catch ex As Exception
Console.WriteLine(("Failed to get Flash ID" + ControlChars.Cr + ControlChars.Lf + ex.Message))
End Try
Console.ReadLine()
End Sub