Sign in to follow this  
Followers 0
KenE

DLLCall w/FTDI USB -> Serial Converter?

4 posts in this topic

Can anyone who is more familiar with DLLCall help me out with this one? I'm writing an app to interface with a FTDI USB to serial converter, which uses a well documented DLL. I have example code for C++ & VB, but I don't know how to get this to work with AI3.

Here is the code from the docs that I'm having trouble converting:

FT_Open

Open the device and return a handle which will be used for subsequent accesses.

FT_STATUS FT_Open (int iDevice, FT_HANDLE *ftHandle)

Parameters:

iDevice Must be 0 if only one device is attached. For multiple devices 1,

2 etc.

ftHandle Pointer to a variable of type FT_HANDLE where the handle will

be stored. This handle must be used to access the device.

Return Value

FT_OK if successful, otherwise the return value is an FT error code.

Remarks

Although this function can be used to open multiple devices by setting iDevice to 0, 1, 2 etc. there

is no ability to open a specific device. To open named devices, use the function FT_OpenEx .

Example

This sample shows how to open a device.

FT_HANDLE ftHandle;

FT_STATUS ftStatus;

ftStatus = FT_Open(0,&ftHandle);

if (ftStatus == FT_OK) {

// FT_Open OK, use ftHandle to access device

}

else {

// FT_Open failed

}

The above was taken from:

http://www.ftdichip.com/Documents/ProgramGuides/D2XXPG30.pdf

So here's what I do understand:

$dll = DllOpen("FTD2XX.dll")

$result = DllCall($dll, "int", "FT_OPEN", "int", 0, ????)

-What I have thus far returns a status of FT_OK, so it is able to open the device, but I don't know how to get the handle that is returned in order to use any of the DLL functions that require the handle. Any ideas?

Share this post


Link to post
Share on other sites



Can anyone who is more familiar with DLLCall help me out with this one? I'm writing an app to interface with a FTDI USB to serial converter, which uses a well documented DLL. I have example code for C++ & VB, but I don't know how to get this to work with AI3.

Here is the code from the docs that I'm having trouble converting:

FT_Open

Open the device and return a handle which will be used for subsequent accesses.

FT_STATUS FT_Open (int iDevice, FT_HANDLE *ftHandle)

Parameters:

iDevice Must be 0 if only one device is attached. For multiple devices 1,

2 etc.

ftHandle Pointer to a variable of type FT_HANDLE where the handle will

be stored. This handle must be used to access the device.

Return Value

FT_OK if successful, otherwise the return value is an FT error code.

Remarks

Although this function can be used to open multiple devices by setting iDevice to 0, 1, 2 etc. there

is no ability to open a specific device. To open named devices, use the function FT_OpenEx .

Example

This sample shows how to open a device.

FT_HANDLE ftHandle;

FT_STATUS ftStatus;

ftStatus = FT_Open(0,&ftHandle);

if (ftStatus == FT_OK) {

// FT_Open OK, use ftHandle to access device

}

else {

// FT_Open failed

}

The above was taken from:

http://www.ftdichip.com/Documents/ProgramGuides/D2XXPG30.pdf

So here's what I do understand:

$dll = DllOpen("FTD2XX.dll")

$result = DllCall($dll, "int", "FT_OPEN", "int", 0, ????)

-What I have thus far returns a status of FT_OK, so it is able to open the device, but I don't know how to get the handle that is returned in order to use any of the DLL functions that require the handle. Any ideas?

i'm very new to DLL's myself, but have you looked at DLL structures? check out DLLStructCreate() to start... there are some examples in this forum of it's application, one you could search for was for setting the system time with a dll call.

1100111 00001011101111 00011101101111 00010111100100 00001111110100 00110111110010 00101101111001 0011100i didn't make up this form of encryption, but i like it.credit to the lvl 6 challenge on arcanum.co.nz

Share this post


Link to post
Share on other sites

try this

$result = DllCall($dll, "int", "FT_OPEN", "int", 0)
If Not @error Then
 If IsArray($result) Then
  MsgBox(0,"FT_HANDLE",$result[1])
 EndIf
EndIf

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Hmm... I seem to have found the answer to this one, DLL Structures were the answer. (Thanks to Lazycat for an example post I found elsewhere in the forums!) Simply using the return array didn't work, so I created a structure to hold just the FT_HANDLE as described in the FTDI docs. This seems to work, so when I am finished implementing all of the FTDI commands in AI3, I'll post all of my code.

Here's what I came up with:

$dll = DllOpen(@ScriptDir & "\FTD2XX.dll") ;Duh, open dll!

$p = DllStructCreate ("dword") ;Create structure to hold pointer returned from DLL

$result = DllCall($dll, "int", "FT_Open", "int", 0, "ptr", DllStructGetPtr ($p,1)) ;Call DLL to open device

$data = DllStructGetData($p, 1) ;Store pointer (FT_HANDLE) here

$result1 = DllCall($dll, "int", "FT_ResetDevice", "long", $data) ;Reset FTDI chip using pointer

$result2 = DllCall($dll, "int", "FT_SetDivisor", "long", $data, "int", 12) ;Set baud rate using pointer

msgbox(0, "", $dll) ;Display DLL Open result

msgbox(0, "", $result[0]) ;Display FT_Open result, 0=FT_OK

msgbox(0, "", $data) ;Display FT_HANDLE

msgbox(0, "", $result1[0]) ;Display FT_ResetDevice result, 0=FT_OK

msgbox(0, "", $result2[0]) ;Display FT_SetDivisor result, 0=FT_OK

This is really my first attempt to use DLLCall in AI (Or using a DLL at all, for that matter!), so I'm having a hard time with some of the concepts behind its use. I've been stuck at this point for two days, so this is a good day for me! ;) Thanks to everyone that replied, I'll post my results here later.

K

Edited by KenE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0