Connecting to an FT232R using Matlab

Hello,
I am using a motorized Piëzo stage that is connected to a COM port driven by an FT232R chip. I have to write ASCI commands to the chip. (For details, see the programming guide: https://we.tl/t-VFpX081G3Y)
I've been trying to connect to this port via Matlab, but no luck so far. First of all, I have tried using ftd2xx.dll. The device does open, but whenever I try to write something to it using FT_Write, a handle error is returned. I have no idea what to do here. My script is below.
%% load d2xx library and open channel #0
lib = 'd2xx';
if ~libisloaded( lib )
loadlibrary( lib, 'ftd2xx.dll', 'ftd2xx.h', 'alias', 'd2xx');
end
deviceNr = uint32(0);
ftHandle = libpointer('uint32Ptr',deviceNr);
statusCode = calllib(lib, 'FT_Open', deviceNr, ftHandle);
FT_Errors(statusCode)
%% write to ftd2xx device using CF30 command
BytesTransferredPtr = libpointer('uint32Ptr',255);
writingBuffer = [80 77 44 49 44 49 48 44 49 48 13]; % ASCI code for velocity (see CF30 programmer guide)
ftHandleVal = get(ftHandle,'Value');
statusCode = calllib(lib,'FT_Write',ftHandleVal,writingBuffer,length(writingBuffer),BytesTransferredPtr);
FT_Errors(statusCode)
FT_Errors is simply a function that returns the status name coupled to a status code (not important).
It must have something to do with the handle, but I have no idea what.
Alternatively, there is also a VCP driver provided, and I can connect directly to the COM port via Matlab, and it is recognized. However, I have no experience with this and it doesn't work directly (I am not getting any error codes, but the stage does not move so apparently it's not working), so I don't know whether this is even possible.
device = serialport("COM5",60000);
writeline(device,"PV,1,100000\r") % see CF30 programmer guide
Does anyone have any idea what I can try?
Thanks!

Answers (0)

Categories

Products

Release

R2019b

Asked:

on 17 Nov 2021

Community Treasure Hunt

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

Start Hunting!