MATLAB Answers

Work with DLL file

48 views (last 30 days)
Peer Blumido
Peer Blumido on 23 Jan 2020 at 13:41
Commented: Mohammad Sami on 30 Jan 2020 at 15:28
im trying to work with a DMD from Vialux. All i know is that i have to use the functions from the DLL File. I loaded it and all functions are in the workspace.
Now i want to use at frist one function "AlpDev Alloc". In the Description it says "long AlpDevAlloc (long DeviceNum, long InitFlag, ALP_ID *DeviceIdPtr)". I get three ouputs but i have to give three inputs. First two of them are given as "ALP_DEFAULT" for DeviceNum and InitFlag (4 byte integer value). For "DeviveIdPtr" pointer to a writeable 4-byte integer. Its completly new for me with DLLs, could anyone help with this question what i have to do with "ALP_ID *DevideIdPtr" and is the rest correct?
Thanks a lot!
loadlibrary('alp4395', 'alp.h');
libfunc = libfunctions('alp4395');
% long AlpDevAlloc (long DeviceNum, long InitFlag, ALP_ID *DeviceIdPtr) in documentation
[res1, res2, res3] = calllib('alp4395',libfunc{1,1},'ALP_Default', 'ALP_Default',?);


Show 2 older comments
Mohammad Sami
Mohammad Sami on 28 Jan 2020 at 7:28
Is ALP_DEFAULT a constant defined in the dll ? I think you will need to pass in the actual numeric value of the constant as the signature asks for long. ulongPtr would be uint32 pointer.
Also do note the pointer is initialised to a null value.
Peer Blumido
Peer Blumido on 28 Jan 2020 at 13:15
I want to show the information i have.
From the Description ( AlpDevAlloc Function):
From the h. - File:
Should i set DeviceNum and InitFlag to 0 or is this value i get as return? I thought i have to write it as a string in the calllib function.
For ALP_Default = 0:
ID = 0;
ALP_ID = libpointer('uint32',ID); % e.g. DataType = 'int32'
DeviceNum = 0;
InitFlag = 0;
[res1, res2] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
This gives me an error in Calllib "Array must be numeric or logical or a pointer to one.".
Something happend i tried this:
ID = 0;
ALP_ID = libpointer; % e.g. DataType = 'int32'
DeviceNum = 0;
InitFlag = 0;
[res1, res2] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
I get no error and in the workspace for res1 = 1001 and res2 = [ ]. At the moment the device is not connected. Maybe if its connected res2 should show me ALP_ID.
I'm in the laboratory now and i used this:
deviceid = uint32(0);
ALP_ID = libpointer('uint32Ptr', deviceid);
DeviceNum = uint32(0);
InitFlag = uint32(0);
[x1, x2] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
My two ouputs [long, longPtr] are x1 = 1001 and x2 = 0. x1 should be alp_returnvalue and x2 the ALP_ID. But 0 seems not like an ID.
Guillaume on 28 Jan 2020 at 15:13
Can you check in the dll documentation what a return value of 1001 mean?

Sign in to comment.

Answers (2)

Guillaume on 28 Jan 2020 at 14:29
Your dll function takes 3 inputs, the first two are scalar numeric indeed. The snippet of documentation you show is not very clear but indeed it looks like you want 0 for the default.
The 3rd input is a pointer to some memory where the function will write something.From your header file snippet it looks like it is indeed a 4-bit unsigned integer (assuming that the dll defines long as a 32-bit integer, which is fairly standard but not guaranteed).
The function only has one output, which is probably an error code or success. I'm unclear why you say you get 3 outputs.
In theory,
deviceptr = libpointer('uint32'); %No need to assign a value, it's overwritten by AlpDevAlloc
res = calllib('alp4395', 'AlpDevAlloc', 0, 0, deviceptr);
ALP_ID = deviceptr.Value;
should work according to the information you've provided. However, do check the value of res against the dll documentation as it may indicate an error of some kind.
If you get a matlab error with the above, then we've not properly understood the signature.
Note that when you did:
ALP_ID = libpointer;
res1 = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
you passed a NULL pointer to the library. Two things can happen in this case: the library tries to write to the NULL address and crashes, or the dll checks that the pointer is not NULL and returns an error. Since it didn't crash, it must have returned an error code which I assumes is that 1001 value.


Sign in to comment.

Peer Blumido
Peer Blumido on 28 Jan 2020 at 16:33
Edited: Peer Blumido on 28 Jan 2020 at 16:55
Hello Guillaume,
thanks for your answer! Thats what ive got about the Function AlpDevAlloc:
I know it wasnt correct that i receive 3 outputs. Im so sorry if i did many dumb mistakes but this is completly new for me. Never worked with DLLs before.
I would suggest this, because it worked without any Error.
loadlibrary('alp4395.dll', 'alp.h');
libfunc = libfunctions('alp4395');
deviceid = uint32(0);
ALP_ID = libpointer('uint32Ptr', deviceid);
DeviceNum = int32(0);
InitFlag = int32(0);
[alp_returnvalue, ALP_ID] = calllib('alp4395','AlpDevAlloc',DeviceNum,InitFlag,ALP_ID);
i hope this can help more find the issue.
i dont know what happend but i wrote at the end
and matlab crashed. After removing this and new run i had this in workspace:
After a second run it turned to the values in workspace above.


Show 5 older comments
Peer Blumido
Peer Blumido on 30 Jan 2020 at 14:24
yes i did this. The values need to set as int32().
%% AlpDevControl
ControlType = int32(2021);
ControlValue = int32(10);
AlpDevStatus = calllib('alp4395', 'AlpDevControl', ALP_ID, ControlType, ControlValue);
I tried some functions to get more experience and to find out how they work or what i have to write for parameters etc.
%% AlpDevInquire
UserVar = int32(0);
UserVarPtr = libpointer('int32Ptr',UserVar);
InquireType1 = int32(2000);
InquireType2 = int32(2057);
InquireType3 = int32(2058);
InquireType4 = int32(2016);
InquireType5 = int32(2050);
InquireType6 = int32(2052);
[~, ALP_SerialNum] = calllib('alp4395','AlpDevInquire',ALP_ID,InquireType1,UserVarPtr); % DMD Serial Nummer
[~, Alp_Height] = calllib('alp4395','AlpDevInquire',ALP_ID,InquireType2,UserVarPtr); % DMD Höhe
[~, ALP_Width] = calllib('alp4395','AlpDevInquire',ALP_ID,InquireType3,UserVarPtr); % DMD Breite
[~, ALP_USB] = calllib('alp4395','AlpDevInquire',ALP_ID,InquireType4,UserVarPtr); % USB Connection
[~, ALP_Temp] = calllib('alp4395','AlpDevInquire',ALP_ID,InquireType5,UserVarPtr); % DLPC FPGA Temp
ALP_Temp = ALP_Temp/256;
[~, ALP_Temp_PCB] = calllib('alp4395','AlpDevInquire',ALP_ID,InquireType6,UserVarPtr); % DLPC FPGA Temp
ALP_Temp_PCB = ALP_Temp_PCB/256;
And these are my results :)
Mohammad Sami
Mohammad Sami on 30 Jan 2020 at 15:08
Looks like you have manage to get it to work. You can add these as functions in your wrapper class.
Mohammad Sami
Mohammad Sami on 30 Jan 2020 at 15:28
Also I would recommend that your class manage all the pointers internally. If any of the c functions return pointers those may need to be freed once you don't need them or when you are deleting this object.
What I did was to create a private property to store pointers in a cell array. Once the pointer is cleared in C (your dll my have a clear or free function), just set it to libpointer (null).
If a function in c returns a pointer, its stored in the cell array, and i return the cell array index rather then the pointer.
% release pointers
obj.pointers{i} = libpointer; % set to null

Sign in to comment.

Sign in to answer this question.