From: Francois MAURICE on
Thanks Doug for this trick for handling void** using Matlab calllib function. It works well for me with Matlab R2007b.
I use Matlab as a debugging tool for data processing within a dsPIC33. I am used to access DLL using a mex wrapper but I tried your solution which is faster than writing a mex for each individual function.
Instead of rewriting a new header file I just added the following lines to redefine some data types (probably I was missing a header file for ULONG, UCHAR, USHORT, LPDWORD) :

typedef unsigned long int ULONG;
typedef unsigned char UCHAR;
typedef unsigned short USHORT;
typedef unsigned long * LPDWORD;
typedef ULONG FT_STATUS;
typedef ULONG FT_HANDLE;

I want to report the fact that for my application (sending a block of simulated data to the dsPIC, then retrieving the resulting processed block. Using CTS/RTS hardware flow control) the communication between PC and dsPIC is stable using the d2xx DLL at 921600 bauds. Just to mention occasionnaly one or 2 errors every 50 mega bytes of transferred data. On the contrary I experienced instabilities using the Virtual Com Port (VCP) with sometimes obligation to abort Matlab and restart the computer (to reinitialize the com port which 'hangs'). Using VCP involves using java methods used for graphics display and for serial link amongst other. If you move a graphics window during serial communication you can eventually crash the application.
In conclusion my advise is to use D2xx DLL instead of VCP to get a stable application.

Francois

"Doug Olney" <olney_doug(a)keithley.com> wrote in message <eefa4ca.3(a)webx.raydaftYaTP>...
> It is possible to call the ftdi dll directly from Matlab 7.0. The
> trick is to create a reasonable .h file. I'm not good with MEX
> files, so I just grabbed the FTDI documentation and created my own.
> The key routines I use appear like this in the header file:
>
> long FT_Open(
> int deviceNumber,
> unsigned long int *pHandle
> );
>
> long FT_Close(
> unsigned long ftHandle
> );
>
> long FT_Read(
> unsigned long ftHandle,
> unsigned long * lpBuffer,
> unsigned long nBufferSize,
> unsigned long * lpBytesReturned
> );
>
>
> long FT_Write(
> unsigned long ftHandle,
> ussigned long * lpBuffer,
> unsigned long nBufferSize,
> unsigned long * lpBytesWritten
> );
>
> etc.
>
> Notice that I don't call ftHandle a void * - Matlab tends to crash
> when I do that. So, I treat it as a 32 bit number I have to cart
> around.
>
> With a good version of the .h file, the matlab syntax is:
>
> loadlibrary('FTD2XX.dll','myFTD2XXheader.h')
> usbMem = uint32(0);
> handlePtr = libpointer('uint32Ptr',usbMem);
> deviceNum = 0;
> retval = calllib('FTD2XX', 'FT_Open', deviceNum, handlePtr);
>
> handle = get(handlePtr,'Value');
>
> The handle is then the first pass parameter to all the functions.
> You have to FT_Close when you are done. If you are reading short
> messages (one or two words) back, don't forget to use
> FT_SetLatencyTimer to 1 ms, so the FTDI will flush out the partial
> buffer.
>
> This was done on windows.
>
> Doug Olney
>
> Luiz Henrique Kleinmayer wrote:
> >
> >
> > Hi...
> >
> > Jarrett,
> > Did you make an implementation which uses
> > ftd2xx.h / ftd2xx.lib to talk to an ftdi chip?
> > I´m trying to do it, but it´s not working.
> > Can you help me?
> >
> > Thanks
> > Kleinmayer
> >
From: Rebecca on
Hi Doug,

I'm trying to do the same thing it would be great if you could send me a copy of the entire header file.

"Doug Olney" <olney_doug(a)keithley.com> wrote in message <eefa4ca.3(a)webx.raydaftYaTP>...
> It is possible to call the ftdi dll directly from Matlab 7.0. The
> trick is to create a reasonable .h file. I'm not good with MEX
> files, so I just grabbed the FTDI documentation and created my own.
> The key routines I use appear like this in the header file:
>
> long FT_Open(
> int deviceNumber,
> unsigned long int *pHandle
> );
>
> long FT_Close(
> unsigned long ftHandle
> );
>
> long FT_Read(
> unsigned long ftHandle,
> unsigned long * lpBuffer,
> unsigned long nBufferSize,
> unsigned long * lpBytesReturned
> );
>
>
> long FT_Write(
> unsigned long ftHandle,
> ussigned long * lpBuffer,
> unsigned long nBufferSize,
> unsigned long * lpBytesWritten
> );
>
> etc.
>
> Notice that I don't call ftHandle a void * - Matlab tends to crash
> when I do that. So, I treat it as a 32 bit number I have to cart
> around.
>
> With a good version of the .h file, the matlab syntax is:
>
> loadlibrary('FTD2XX.dll','myFTD2XXheader.h')
> usbMem = uint32(0);
> handlePtr = libpointer('uint32Ptr',usbMem);
> deviceNum = 0;
> retval = calllib('FTD2XX', 'FT_Open', deviceNum, handlePtr);
>
> handle = get(handlePtr,'Value');
>
> The handle is then the first pass parameter to all the functions.
> You have to FT_Close when you are done. If you are reading short
> messages (one or two words) back, don't forget to use
> FT_SetLatencyTimer to 1 ms, so the FTDI will flush out the partial
> buffer.
>
> This was done on windows.
>
> Doug Olney
From: Rebecca on
Hi Doug,

I tried to use what you gave in your last post but Matlab crashes on the
retval = calllib('ftd2xx', 'FT_Open', deviceNum, handlePtr);
line.

"Rebecca " <el06rb(a)leeds.ac.uk> wrote in message <hnqg33$71t$1(a)fred.mathworks.com>...
> Hi Doug,
>
> I'm trying to do the same thing it would be great if you could send me a copy of the entire header file.