From: Guido on
Hello,

I want to use Matlab to write the measured data of my digital multimeter compact flash card to a file. The communication between Matlab and the DMM Card can be done via an external library (*.dll). I'm already able to load the library and call functions of this library. As you can see in the following excerpt form the header file of the external library, some functions return wide characters strings (wchar_t).

C Header file (excerpt):
CFD200Drvr_API int CFD200Drvr_OpenCard(int *,int *,wchar_t *,wchar_t *,wchar_t *,wchar_t *,int *,int *);

Although Matlab uses 2 bytes for each character of a string. Matlab uses 'void pointer' to replace 'wchar_t pointer'. Please note the following information shown by libfunctionsview.

Information shown by 'libfunctionsview'.
[int32, int32Ptr, int32Ptr, voidPtr, voidPtr, voidPtr, voidPtr, int32Ptr, int32Ptr] CFD200Drvr_OpenCard(int32Ptr, int32Ptr, voidPtr, voidPtr, voidPtr, voidPtr, int32Ptr, int32Ptr)

Currently I use the following Matlab code to call the just mentioned function:
CardHandle = int32(0);
Socket = int32(0);
Manufacturer = char(zeros(1,200)); % max 100 wchar (wide character)
CardName = char(zeros(1,200)); % max 100 wchar (wide character)
HWRevision = char(zeros(1,200)); % max 100 wchar (wide character)
SerialNumber = char(zeros(1,200)); % max 100 wchar (wide character)
IOBaseAddress = int32(0);
IRQ = int32(0);
[Result, CardHandle, Socket, Manufacturer, CardName, HWRevision, ...
SerialNumber, pIOBaseAddress, pIRQ] = ...
calllib('ElanPCCard_CFD200', 'CFD200Drvr_OpenCard', ...
CardHandle, Socket, uint8(Manufacturer), uint8(CardName), ...
uint8(HWRevision), uint8(SerialNumber), IOBaseAddress, IRQ);

All 'strings' that are returned by the library function 'CFD200Drvr_OpenCard' are from type 'uint8'. See e.g. the value of the variable 'CardName'.

CardName =
Columns 1 through 15
68 0 77 0 77 0 32 0 67 0 97 0 114 0 100
Columns 16 through 30
0 58 0 32 0 67 0 70 0 68 0 50 0 48 0
Columns 31 through 45
48 0 0 0 0 0 0 0 0 0 ...

As the 'strings' are from type 'uint8' I first have to typecast these 'strings' and convert them to character arrays.

Manufacturer = char(typecast(Manufacturer, 'uint16'));
CardName = char(typecast(CardName, 'uint16'));
HWRevision = char(typecast(HWRevision, 'uint16'));
SerialNumber = char(typecast(SerialNumber, 'uint16'));

After that the value for the variable 'CardName' is

CardName =
DMM Card: CFD200

and the size of 'CardName' is

>> size(CardName)
ans =
1.0000e+000 100.0000e+000

I don't like the above code very much. Isn't there any other way to receive strings from the matlab function 'callib'?

Best Regards
Guido
From: James Tursa on
"Guido " <girgu(a)gmx.de> wrote in message <i0u1ss$kaj$1(a)fred.mathworks.com>...
>
> I want to use Matlab to write the measured data of my digital multimeter compact flash card to a file. The communication between Matlab and the DMM Card can be done via an external library (*.dll). I'm already able to load the library and call functions of this library. As you can see in the following excerpt form the header file of the external library, some functions return wide characters strings (wchar_t).
>
> C Header file (excerpt):
> CFD200Drvr_API int CFD200Drvr_OpenCard(int *,int *,wchar_t *,wchar_t *,wchar_t *,wchar_t *,int *,int *);
>
> Although Matlab uses 2 bytes for each character of a string. Matlab uses 'void pointer' to replace 'wchar_t pointer'. Please note the following information shown by libfunctionsview.
>
> Information shown by 'libfunctionsview'.
> [int32, int32Ptr, int32Ptr, voidPtr, voidPtr, voidPtr, voidPtr, int32Ptr, int32Ptr] CFD200Drvr_OpenCard(int32Ptr, int32Ptr, voidPtr, voidPtr, voidPtr, voidPtr, int32Ptr, int32Ptr)
>
(snip)
>
> I don't like the above code very much. Isn't there any other way to receive strings from the matlab function 'callib'?


You could also alter the header file and change all the wchar_t * to unsigned short *. Then on the MATLAB side you would have a uint16 array directly that you could typecast into char.

James Tursa