How to read or write data using Python

Description of how to retrieve measurement data from a controller for data analysis, manipulation, or reporting using Python

Introduction

Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn't specialized for any specific problems.

Gantner Instruments provides a C API for reading measurement data from a controller directly read 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 necessary DLL functions. The DLL call within the package is done with the ctypes package. This is a foreign function library for Python. It provides C-compatible data types and allows calling functions in DLLs or shared libraries.

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