Unable to read from FTDI device using calllibrary function

18 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 = [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);
%% Get queue status
queue_size = libpointer('uint32Ptr',99);
[err] = calllib('ftd2xx64', 'FT_GetQueueStatus', ptr_handle, queue_size);
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.

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 = 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.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!