Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

USB & FTDI & Compiler

355 views
Skip to first unread message

LJ Malone

unread,
Jan 31, 2005, 4:53:21 PM1/31/05
to
Hello,
I've create a mex file which uses
ftd2xx.h / ftd2xx.lib to talk to an ftdi chip (down a usb port).

This mex file works great / seemlessly from an m file run from the
command window.

However, when I compile the m file, I get the error message:

MATLAB ERROR: USB Device Interface Undefined

Any suggestions would be helpful.

Thanks -- Jarrett

Peter Webb

unread,
Feb 2, 2005, 4:44:54 PM2/2/05
to
Does your MEX file use a DLL to communicate with the USB port? If so, you
may need to add the directory containing that DLL to your path.

"LJ Malone" <ljma...@yahoo.com> wrote in message
news:eefa4...@webx.raydaftYaTP...

Samuel Zahnd

unread,
Apr 6, 2005, 8:27:47 AM4/6/05
to
Hi Jarrett,

does your implementaion work on several plattforms (linux, win, mac)?
Can you handle several FTDI's even without EEPROM product
information. If yes, i might be interested in you solution.
Thanks for your feedback. Best,
-Sam

Luiz Henrique Kleinmayer

unread,
May 13, 2005, 8:46:53 AM5/13/05
to
Hi...

Jarrett,
Did you make an implementation which uses
ftd2xx.h / ftd2xx.lib to talk to an ftdi chip?
I惴 trying to do it, but it愀 not working.
Can you help me?

Thanks
Kleinmayer

Doug Olney

unread,
May 18, 2005, 7:00:37 PM5/18/05
to
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

unread,
May 18, 2005, 7:20:15 PM5/18/05
to
Hi...

Thank愀 Doug...
But, only one question: isn愒 your .h file the .h from FTDI chip? Is
it a new .h file whit only the functions that you want to use?

Thank愀
Kleinmayer

Doug Olney

unread,
May 18, 2005, 7:27:33 PM5/18/05
to
Luiz Henrique Kleinmayer wrote:
>
>
> Hi...
>
> Thank愀 Doug...
> But, only one question: isn愒 your .h file the .h from FTDI chip?
> Is
> it a new .h file whit only the functions that you want to use?
>
> Thank愀
> Kleinmayer

There are two issues with the FTDI provided .h file. One is that
they don't use fundamental types in their declarations: they use
types like uchar8 that matlab doesn't always know what to do with.
Somehow you have to get the right typecasts for all those types. I
only use about 7 functions, so I just typed it.

Second is the use of the void * for ftHandle. For whatever reason,
matlab crashes immediatly as soon as you try to get( ,'Value') of the
pointer. It is easier to lie and call it a unsigned long.

Doug

Luiz Henrique Kleinmayer

unread,
Jun 6, 2005, 3:08:18 PM6/6/05
to
Hi Doug....

I惴 open my usb device sucessfuly, but when I try to write data in
the buffer it crashes.
How can you make ir??

Thank愀 agains...
Kleinmater

Luiz Henrique M. Kleinmayer

unread,
Jun 9, 2005, 8:02:21 AM6/9/05
to
Other question: Anybody implements the communication whit Simulink
and FTDI Chip?

Luiz Henrique Kleinmayer

unread,
Jun 15, 2005, 10:34:07 AM6/15/05
to
Hi Doug...

I have questioned about using FTDI Chip with Matlab in the Matlab
Newsgroup and you helped me. But now I have a problem and I want your
help.
Do you use the function FT_OpenEx in your application? I´m trying to
use this function and Matlab chashes.

Thank´s for your help,
Kleinmayer

Stev

unread,
Sep 11, 2009, 4:01:03 AM9/11/09
to
Hi Doug,
I am trying to implement your header file, but it is not working. What other code did you have to have in the file besides the key routines you had?
Thanks in advance!
-Steve


"Doug Olney" <olney...@keithley.com> wrote in message <eefa...@webx.raydaftYaTP>...

> > I?m trying to do it, but it?s not working.

Francois MAURICE

unread,
Mar 11, 2010, 4:20:09 AM3/11/10
to
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...@keithley.com> wrote in message <eefa...@webx.raydaftYaTP>...

> > I´m trying to do it, but it´s not working.

Rebecca

unread,
Mar 17, 2010, 7:57:23 AM3/17/10
to
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...@keithley.com> wrote in message <eefa...@webx.raydaftYaTP>...

Rebecca

unread,
Mar 17, 2010, 8:05:23 AM3/17/10
to
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 " <el0...@leeds.ac.uk> wrote in message <hnqg33$71t$1...@fred.mathworks.com>...

John

unread,
Aug 13, 2010, 7:44:07 PM8/13/10
to
"Doug Olney" <olney...@keithley.com> wrote in message <eefa...@webx.raydaftYaTP>...
> > I´m trying to do it, but it´s not working.

> > Can you help me?
> >
> > Thanks
> > Kleinmayer
> >
Stellar information. Do you have anymore code to share? I'm having a heck of a time setting the bit mode and writing to the device. Sadly, the VCP will not work for me.. I need to toggle individual pins. thanks.

John

unread,
Aug 23, 2010, 4:50:21 PM8/23/10
to
"John " <johnn...@hotmail.com> wrote in message <i44lc7$hej$1...@fred.mathworks.com>...


Thought I would post successful Matlab code for any others working on this:
Device UM245R . Uses the modified .h file as suggested above to deal with datatypes.

code writes 0xff to bits :

[a b]=loadlibrary('ftd2xx.dll','myftd2xx.h');


usbMem = uint32(0);
handlePtr = libpointer('uint32Ptr',usbMem);
deviceNum = 0;

retval = calllib('ftd2xx', 'FT_Open', deviceNum, handlePtr);


handle = get(handlePtr,'Value');

calllib('ftd2xx', 'FT_SetBitMode',handle,uint8(255),uint8(1));
calllib('ftd2xx', 'FT_SetBaudRate',handle,38400);
buffsize = uint32(1);
on = uint8(255); onPtr = libpointer('uint8Ptr',on);
bytes = uint32(0); bytePtr = libpointer('uint32Ptr',bytes);
calllib('ftd2xx','FT_Write',handle,onPtr,buffsize,bytePtr);

John

unread,
Aug 24, 2010, 8:19:04 PM8/24/10
to
"John " <johnn...@hotmail.com> wrote in message <i4umud$ah$1...@fred.mathworks.com>...

Also, should ad:
calllib('ftd2xx','FT_Close',handle);
unloadlibrary('ftd2xx');

Another note for creating .exe......
http://www.mathworks.com/support/solutions/en/data/1-1AJI4/index.html?product=CO

0 new messages