How to use powershell to get device info using FT_ListDevices of FTDI D2XX drivers library

901 views Asked by At

I am using an FT245R USB to Parallel FIFO Interface Device. The chip uses the FTDI D2XX library. I am using powershell and DllImport to load and access the library functions. I am having trouble using the FT_ListDevices Function. The Reference and examples to the Device can be found at link below. See Pages 11, 12, and part of 13

http://www.ftdichip.com/Support/Documents/ProgramGuides/D2XX_Programmer's_Guide(FT_000071).pdf

A co-worker and I were able to get the Second example to work, but we were unsuccessful to get the Third. Below is the code that works. Third example is similar to the example below, however it uses an array of arrays instead of a Byte array as in the working code below.

#2. Get serial number of first device
$FT_LIST_BY_INDEX = '0x40000000'
$FT_OPEN_BY_SERIAL_NUMBER = 1

$numDevs = 0

$SerialNumber = New-Object byte[] 64

$run=@'
[DllImport("FTD2XX.dll")] static public extern UInt32 FT_ListDevices(System.IntPtr pvArg1, byte[] pvArg2, System.UInt32 dwFlags);
'@
$typef = Add-Type -MemberDefinition $run -Name "ListDevices" -PassThru
$typef::FT_ListDevices($numDevs, $SerialNumber, $FT_LIST_BY_INDEX -bor $FT_OPEN_BY_SERIAL_NUMBER)
[System.Text.Encoding]::ASCII.GetString($SerialNumber)

The best attempt to get example 3 to work is below.

#3. Get device descriptions of all devices currently connected
$FT_LIST_ALL = '0x20000000'
$FT_OPEN_BY_SERIAL_NUMBER = 1
$FT_OPEN_BY_DESCRIPTION = 2
$numDevs = 0
$TotalnumDevs = 2

[Byte[][]]$BufferPtrs = (,([Byte[]] (,"" * 64)))*$TotalnumDevs
[Byte[][]]$BufferPtrs += ,""

$run=@'
[DllImport("FTD2XX.dll")] static public extern UInt32 FT_ListDevices(Array BufferPtrs, ref UInt32 pvArg2, UInt32 dwFlags);
'@
$typef = Add-Type -MemberDefinition $run -Name "ListDevices1" -PassThru
$ftStatus = $typef::FT_ListDevices($BufferPtrs, [ref] $numDevs, $FT_LIST_ALL -bor $FT_OPEN_BY_DESCRIPTION)
'(FT_OK = 0) ftStatus = ' + [string]$ftStatus
'Number of Devices = ' + [string]$numDevs
[ref] $BufferPtrs    #Display the Pointers Array
$BufferPtrs.GetType()

So the goal here is to change Array to Byte[][] in the DLLimport statement, but
this will result in an error . . .

ERROR: "There is no marshaling support for nested arrays"

I did some research and tried to use custom marshaling but this resulted in another error.

ERROR: "The type or namespace name 'ICuztomMarshaler' could not be found"

I need to be able to send a Jagged/Nested array to the library to get this
to work.

Please help and thank you in advance. J. Wade

1

There are 1 answers

2
Negorath On

Are you using 64 or x86 version of PowerShell to run the code? I've had similar issue before because the driver couldn't find devices when running the "wrong" bit version of PowerShell.