Using FTDI .dll

I know that you can use the SerialPort with FTDI devices, but I need to use their .DLL as I make function calls to change parameters of the USB device on the fly. Are there any examples of setting up the DLL and making function calls with Xojo?

Also - you guys spelled FTDI wrong in your FAQ (under “Can I used USB with Xojo”)… its FTID there. :slight_smile:

Thanks!

For example… the setup in VB6 for the DLL is:

Public Declare Function FT_Open Lib “FTD2XX.DLL” (ByVal intDeviceNumber As Integer, ByRef lngHandle As Long) As Long
Public Declare Function FT_OpenEx Lib “FTD2XX.DLL” (ByVal arg1 As String, ByVal arg2 As Long, ByRef lngHandle As Long) As Long
Public Declare Function FT_Close Lib “FTD2XX.DLL” (ByVal lngHandle As Long) As Long
Public Declare Function FT_SetUSBParameters Lib “FTD2XX.DLL” (ByVal lngHandle As Long, ByVal dwInTransferSize As Long, ByVal dwOutTransferSize As Long) As Long

Using the DLL:

’ This routine opens the FTDI device

Public Sub OpenFTDI()
strDescription = “Laser Control XS1” + Chr$(0) ’ null terminated name string
ftStatus = FT_OpenEx(strDescription, FT_OPEN_BY_DESCRIPTION, lngHandle) ’ open device with specified name and store handle
If ftStatus = FT_OK Then
If FT_SetTimeouts(lngHandle, 100, 0) <> FT_OK Then GoTo BadFTDI ’ set timeout to 100ms

    ftStatus = FT_SetLatencyTimer(lngHandle, 2)     ' set lowest latency possible
    If ftStatus <> FT_OK Then GoTo BadFTDI
    
    ftStatus = FT_SetUSBParameters(lngHandle, &H10000, &H10000) ' set USB receive/transmit buffer to 64K * 2
    If ftStatus <> FT_OK Then GoTo BadFTDI

End If
Exit Sub

BadFTDI:
Call CloseFTDI
End Sub

If we can do the same in Xojo, we will switch in a second! We need Mac support.

Those look like standard C-style declares, not OO or .Net, so they should translate almost verbatim to Xojo.

That is the exact VB6 code from my program cut n paste into my post.

Are there any example programs that show DLL usage?

Examples are under your Xojo install directory in `Example Projects\Advanced\Declares\…

My very quick translate, of what you supplied is :-

  const FT_OK = 0 //=PUT-CORRECT-VALUE-HERE
  const FT_OPEN_BY_DESCRIPTION = 0 //=PUT-CORRECT-VALUE-HERE
  
  dim strDescription as string
  dim ftStatus as int32
  dim lngHandle as int32
  
  Soft Declare Function FT_Open Lib "FTD2XX.DLL" (ByVal intDeviceNumber As Int32, ByRef lngHandle As Int32) As Int32
  
  Soft Declare Function FT_OpenEx Lib "FTD2XX.DLL" (ByVal arg1 As CString, ByVal arg2 As Int32, ByRef lngHandle As Int32) As Int32
  
  Soft Declare Function FT_Close Lib "FTD2XX.DLL" (ByVal lngHandle As Int32) As Int32
  
  Soft Declare Function FT_SetUSBParameters Lib "FTD2XX.DLL" (ByVal lngHandle As Int32, ByVal dwInTransferSize As Int32, ByVal dwOutTransferSize As Int32) As Int32
  
  ' Using the DLL:
  
  ' This routine opens the FTDI device
  
  strDescription = "Laser Control XS1" + Chr(0) ' null terminated name string
  
  ftStatus = FT_OpenEx(strDescription, FT_OPEN_BY_DESCRIPTION, lngHandle) ' open device with specified name and store handle
  If ftStatus = FT_OK Then
    
    //x Do not have declare for these...
    //x If FT_SetTimeouts(lngHandle, 100, 0) <> FT_OK Then GoTo BadFTDI ' set timeout to 100ms
    
    //x ftStatus = FT_SetLatencyTimer(lngHandle, 2) ' set lowest latency possible
    //x If ftStatus <> FT_OK Then GoTo BadFTDI
    
    ftStatus = FT_SetUSBParameters(lngHandle, &H10000, &H10000) ' set USB receive/transmit buffer to 64K * 2
    If ftStatus <> FT_OK Then GoTo BadFTDI
    
  End If
  
  RETURN
  
  BadFTDI:
  //x Call CloseFTDI
  // Should this be...
  Call FT_Close()
  // ?
  
  RETURN

I’ve commented out the bits I have no declares for and put in dummy constants as I don’t know the values,
but this does compile.

I’ve also resisted temptation to re-code to remove Gotos :slight_smile:

Thanks… I will try this. I have declarations for all of the DLL functions. The CloseFTDI is my function as that performs a bunch of hardware poking before calling the FT_Close() function. The BadFTDI is goto is needed, because there is code there to look for other hardware.

I will give this a try. If it works, then we will be able to get away from VB6.

I looked at the examples and what I do not understand about Xojo is WHERE to set the declarations. In VB6 we do it at the start of a module or code that occurs prior to any of the functions. I don’t see anything like that. It seems from the examples that the declarations are inside of the function (like a button press). That doesn’t make much sense.

That is correct. You could also add them as an external function by adding an “external method” to a class, but in general you can leave them at the top of a function or directly before you call them.