Reading and Writing to a Controller in Python

How to retrieve measurement data from a controller in Python for analysis, manipulation, reporting, or writing back results.

Introduction

Python is a versatile programming language commonly used for software development, web applications, task automation, and data analysis. As a general-purpose language, it is not limited to a specific domain and can be utilized to develop a wide range of applications.

Gantner Instruments provides a C API for directly reading measurement data from a controller into a Python program. This C API is called eGateHighSpeedPort API and is part of the GInsData Library. This library is available as a 32/64-bit DLL for Windows (giutility.dll) or Linux (libGInsUtility.so).

The latest releases of Python can be downloaded here: www.python.org.

ctypes package

To use the eGateHighSpeedPort API, Gantner Instruments provides an interface to the required DLL functions. The package utilizes the ctypes module, a foreign function library for Python, to perform DLL calls. This module enables interaction with shared libraries by providing C-compatible data types and function calling capabilities.

For example:

from ctypes import*
import ctypes
 
# Load DLL into memory, take care to have the corresponding dll available
try:
GINSDll = ctypes.cdll.LoadLibrary("giutility_x64.dll")
print("64 bit DLL imported")
except OSError:
GINSDll = ctypes.cdll.LoadLibrary("giutility_x86.dll")
print("32 bit DLL imported")
 
...
# function prototypes: specifying the required argument types
GINSDll._CD_eGateHighSpeedPort_Init.argtypes=[c_char_p,c_int,c_int,c_int,POINTER(c_int),POINTER(c_int)]
 
# function call:
ret=GINSDll._CD_eGateHighSpeedPort_Init(self.controllerIP,self.timeout,self.HSP_BUFFER,self.sampleRate,byref(self.HCLIENT),byref(self.HCONNECTION))
...


The DLL functions are simplified for ease of use in Python:

# Load Package
import ginsapy.giutility.connect.PyQStationConnectWin as Qstation #module with communication functions to Gantner Q.Station under windows environment
 
#initialize buffer connection
conn=Qstation.ConnectGIns()
conn.init_connection("192.168.5.24")
 
#Return some information of the controller
conn.read_controller_name()
conn.read_channel_names()
conn.read_channel_count()
 
#Call function to store buffer into the variable buffer
buffer=conn.yield_buffer()

GinsAPy package

Basic GInsData functionality is implemented in different Python modules and classes. All of the interface scripts, with examples and documentation, are delivered in an interface package called GinsAPy.

Documentation

The package documentation is hosted here: GinsAPy documentation.


The following examples are part of the package. Additional libraries need to be installed to run the examples.

  • Read UDBF file (data logger file)

  • Connect to a controller

  • Write an online value to a controller
  • Read a data stream

  • Write a data stream

You can open an interactive menu to run examples from the command prompt (ensure that the PyQtGraph package is installed).

python -m ginsapy.examples


You can also execute the following Python code:

import ginsapy.examples as gins_ex
gins_ex.run()


The following GUI should open:

GinsAPy download

Download link of GinsApy:

Additional libraries

The only external package you need to run efficiently in the GinsAPy package is NumPy

To run the examples the following libraries should be installed:

You can either install the package in your project directory and call the communication methods directly from your project root directory,

import ginsapy.giutility.connect.PyQStationConnectWin as Qstation


or install the package,

pip install ginsapy-xxxxxxxxxxxxx.whl


and call the methods in the same way (now independent from your execution directory).

import ginsapy.giutility.connect.PyQStationConnectWin as Qstation