Unable to read from FTDI device using calllibrary function

7 views (last 30 days)
I am trying to read values from a FTDI device. I am using loadlibrary and calllib functions.
loadlibrary('ftd2xx64.dll','myheader.h','addheader', 'ftd2xx.h');
libfunctionsview ('ftd2xx64')
%% open handle
handle = uint32(0);
ptr_handle = libpointer('voidPtrPtr',handle);
[err] = calllib('ftd2xx64', 'FT_Open', int32(0), ptr_handle);
%% Write temp command
temp_req_id='*meas:tempe\r';
temp_req = [int8(temp_req_id) 0];
size_temp_req = uint32(strlength(temp_req_id));
bytes_written = libpointer('uint32Ptr',0);
err = calllib('ftd2xx64', 'FT_Write',ptr_handle,temp_req,size_temp_req,bytes_written);
bytes_written.Value
%% Get queue status
queue_size = libpointer('uint32Ptr',99);
[err] = calllib('ftd2xx64', 'FT_GetQueueStatus', ptr_handle, queue_size);
queue_size.Value
Photo from program documentation
When I do write command i expect to receive the temperature value from the device using read command. But after write the status of queue buffer remains 0 so I have nothing to read from the queue. But the device acknowledges that number of bytes written was 13.
To check is the value of queue_size is getting updated i tried to initialize the value of queue_size to 99. but the output was 0.
What could be wrong with the code?
I have attached the header an library for reference. any help is kindly appreciated.
  1 Comment
Tobias Ammann
Tobias Ammann on 29 Jun 2023
Edited: Tobias Ammann on 29 Jun 2023
Hello Kumar,
glad to hear that you solved your problem. I try to load the ftd2xx64 library in matlab also. However i end up with the following warnings:
Warning: The data type 'error' used by function FT_IoCtl does not exist.
> In loadlibrary
Warning: The data type 'error' used by function FT_W32_ReadFile does not exist.
> In loadlibrary
Warning: The data type 'error' used by function FT_W32_WriteFile does not exist.
> In loadlibrary
Warning: The data type 'error' used by function FT_W32_GetOverlappedResult does not exist.
> In loadlibrary
Warning: The data type 'error' used by function FT_W32_ClearCommError does not exist.
> In loadlibrary
Warning: The data type 'error' used by function FT_W32_GetCommState does not exist.
> In loadlibrary
Warning: The data type 'error' used by function FT_W32_SetCommState does not exist.
> In loadlibrary
Warning: The data type 'error' used by function FT_W32_WaitCommEvent does not exist.
> In loadlibrary
Did you see these warnings also?
[notfound,warnings] = loadlibrary('ftd2xx64', 'ftd2xx.h');
Closer examination of the contents in the 'warnings' variable shows a view thousand warnings of this kind:
Function pointer types are unsupported in structures HRESULT ( __stdcall * QueryInterface ) ( IPrintDialogServices * .
Found on line 306997 of input from line 997 of file C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\um\commdlg.h
Function pointer types are unsupported in structures HRESULT ( __stdcall * GetCurrentDevMode ) ( IPrintDialogServices * .
Found on line 306997 of input from line 997 of file C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\um\commdlg.h
Function pointer types are unsupported in structures HRESULT ( __stdcall * GetCurrentPrinterName ) ( IPrintDialogServices * .
Found on line 306997 of input from line 997 of file C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\um\commdlg.h
Function pointer types are unsupported in structures HRESULT ( __stdcall * GetCurrentPortName ) ( IPrintDialogServices * .
Found on line 306997 of input from line 997 of file C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\um\commdlg.h
Type 'intWCHAR__unalignedPtr' was not found. Defaulting to type voidPtr.
Found on line 307589 of input from line 316 of file C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\shared\stralign.h
Type 'LARGE_INTEGER' was not found. Defaulting to type error.
Found on line 308611 of input from line 685 of file C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\um\winsvc.h
Type 'LARGE_INTEGER' was not found. Defaulting to type error.
Found on line 308611 of input from line 685 of file C:\Program Files (x86)\Windows Kits\10\include\10.0.22000.0\um\winsvc.h
Type 'LPOVERLAPPED' was not found. Defaulting to type error.
Any suggestions?
Regards,
Tobias

Sign in to comment.

Accepted Answer

Kishore Kumar
Kishore Kumar on 3 Nov 2021
I got the problem resolved. The problem is at the end of each command in FT_Write it is required to add carriage return(\r). When it is added to a variable the \ and r are treated like character rather than escape sequence. Hence the solution was to include the ASCII equivalent at the end as follows.
temp_req_id='*meas:tempe';
temp_req = libpointer('voidPtr',[int8(temp_req_id),int8(char(13))]);
size_temp_req = uint32(length([int8(temp_req_id),int8(char(13))]));
This resolved the problem.
  1 Comment
Stephane Hausherr
Stephane Hausherr on 6 Oct 2023
I am new to Matlab programming, and was struggling applying the code above. Turned out, if you open the header file in Altium Designer and save it, the tab characters are converted and loading the dll is now working for me :) I believe NotePad++ or other text editors might have the same functionnalities.

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!