How to stream data to Matlab

How to stream data from a Gantner controller over Ethernet to Matlab software

There are 2 different ways to read buffered data from a Gantner Controller (for e.g. Q.station) or a PostProcess Buffer (GI.bench) into Matlab:

Call Gantner-DLL functions (C-API) directly in Matlab → see Use shared libraries
Use a pre-compiled MEX file (mexw64 for 64-Bit, mexw32 for 32-Bit versions) → see MEX file
This works for both Matlab on Windows and Linux.

The mentioned functions (loadlibrary() and calllib()) were tested with Matlab R2013a through Matlab R2021b (64bit). The installed compiler was “Microsoft Visual C++ 2010”.

In Matlab it is necessary to have an installed and supported compiler to run these functions!! If this is not possible, you need to use the compiled MEX-file (in 32/64 bit).

You can find more infos on Mathworks.com

You can check/install available compilers in matlab using

mex -setup

To read Gantner-Data directly into Matlab, it is possible to call Gantner-DLLs directly in Matlab code. For example using:
eGateUtility.dll (only 32-bit!!)
giutility.dll (GInsData → C-API: eGateHighSpeedPort API): available in 32/64-bit

Load C/C++ Shared Library

The syntax to load a library is for example

loadlibrary('giutility','eGateHighSpeedPort.h','alias','HighSpeedPortDLL');

See for detailed info for loadlibrary: https://www.mathworks.com/help/matlab/ref/loadlibrary.html

Call Function in Shared Library

A DLL Function can be called from a loaded library for example

calllib('HighSpeedPortDLL','CD_eGateHighSpeedPort_Init',host,timeout,conType,10,hclient,hconnection);

See for detailled info for calllib: https://www.mathworks.com/help/matlab/ref/calllib.html

Pointer

Pointer objects are needed for use with shared libraries.

Examples:

  • Int32: Ptr = libpointer('int32Ptr',-1);
  • Double: Ptr = libpointer('doublePtr',0);
  • cstring: Ptr = libpointer('cstring','000000000000000000000000000000000000000000000000000000');
  • cstring null pointer: Ptr = libpointer('cstring');

Get Values from pointers using:

  • Ptr.Value

See for detailed info of pointer objects: https://www.mathworks.com/help/matlab/ref/libpointer.html

MEX File

Using a MEX file, it is possible to provide the GInsData functionality (via C-API) directly in Matlab. With simple C-code the interface between Matlab and the DLL functions (written in C/C++) can be realized, and then be called as function in Matlab

Gantner Instruments provides MEX files for 64/32 bit to load DLL functionality. The files are called:

  • “GInsMex.mexw64” for 64-bit Matlab (generated in Matlab R2013a 64bit)
  • “GInsMex.mexw32” for 32-bit Matlab

Also be sure to have a “giutility.dll” in 32/64 bit in your working directory beside the GInsMex.mexw32/64 file. (or in Windows System32/SysWOW64 directory)

The MEX-files are generated using Microsoft Visual C++ 2010 compiler. To use the MEX-files, be sure that Microsoft Visual Studio 2010 run-time libraries are available on the computer they are run on.

Workmodes

The MEX-file function is called “GInsMex” and can be executed with different “Workmodes”. It wraps the most important functions of the DLL interface (giutility.dll) to make them easily accessible in Matlab. The workmodes covers for example reading buffer data from a device or from post process buffers (GI.bench) and also file import (UDBF file format).

Usage:

 [ret(, Additional Ret Values) ] = GInsMex(workmode[, Additional Parameters]) 

To get the functions online-help, just type GInsMex into your Matlab Command Window. Most of the functions only make sense in a combination of multiple function calls with different workmodes. An example is provided to demonstrate the usage (HSP_ReadBuffer.m).

The workmodes are:

1 - connect to hardware with specified ip address
2 - set the buffer index to specify which buffer should be used (0-3)
3 - get number of channels of the selected buffer
4 - get channel name
5 - read data (handle this function with care! on high buffer data rates, your Matlab application needs to be able
to process the received amount of data fast enough. Otherwise a buffer-overrun will occur and connection will be closed!
In this case, think about reducing the buffer sample rate or only "post"-proccess the high speed data in Matlab
using .DAT files (avoid "online" highspeed data processing in Matlab).
6 - close connection
7 - get samplerate
8 - sleep (ms)
10 - Identify first - Use this Fuction (+ workmode 11) to scan the network for devices...
11 - Identify next - Use this in addition to workmode 10 to scan the network for devices...
12 - Get Number of available PostProcess buffers. Use this function in combination with workmode 13
13 - Read PostProcess Buffer information by index
14 - Initialize connection to a PostProcess Buffer (GI.bench)
15 - Set Backtime - use this function only in combination with workmode 14.
101 - Open .DAT file
102 - Read channel data from selected file

Examples

HSP_ReadBuffer.m

This example demonstrates the usage of the GInsMex functions to connect to different buffer streams with a simple UI to set some parameters (HSP = HighSpeedPort Protocol). To run the example, just type

 >> HSP_ReadBuffer 

in your Matlab Command Window.

The example allows you to connect to different data sources:

  • Connected Network Device (e.g. Q.station) with a specified IP-Address
  • PostProcess buffers, as local system data buffers, provided for example by GI.bench, specified by ID.

To identify devices or PostProcess buffers it is possible to use a network scan or a local PostProcess buffer scan, which is also available as a button on the UI. The example code can be found in the HSP_ReadBuffer.m file.

Be aware: this is only an example on how to use the functions and get data into Matlab! Any data visualization or processing is up to the user.

Import .DAT Files

With the MEX-function, it is possible to import binary Gantner .DAT file data into Matlab. The import loads single channel-data by channel-index from a file into a 1×1 struct including 2 fields with Nx1 vectors (N = number of samples in the file)

  • XData (relative time values in [s])
  • YData (channel measurement values)

Use the MEX-function with the following work-modes:

example_filedata_import.m
%% load file data -> workmode = 101
GInsMex(101, 'backup___0_2018-10-02_05-11-00_000000.dat')
 
%% read channel data of channelindex 5 to "data" -> workmode = 102
[ret, data] = GInsMex(102,5);
 
%% close file -> workmode = 6
GInsMex(6)
 
%% plot channel data
plot(data.XData, data.YData)

To load a complete file with data of all channels, a matlab “struct” can be used:

example_filedata_import2.m
%% load file data -> workmode = 101
GInsMex(101, 'backup___0_2018-10-02_05-11-00_000000.dat')
 
%% read number of channels -> workmode = 3
[ret, numChannels] = GInsMex(3);
 
%% create struct for all channels
channelstruct = struct();
 
for i=1:numChannels
%read channel name -> workmode = 4
[ret, strChannel] = GInsMex(4, i-1);
 
%read channel data -> workmode = 102
[ret, data] = GInsMex(102,i-1);
 
%write to struct -> remove invalid characters from string!
channelstruct.(regexprep(strChannel, {'#' '/' ' '}, '_')) = data;
end
 
%% close file -> workmode = 6
GInsMex(6)
 
%% plot channel data of a single channel
plot(channelstruct.sine3.XData,channelstruct.sine3.YData)

Download

64-bit

Download the file + example here: mexw64.zip

32-bit

Download the file + example here: mexw32.zip