Accessing the HF2 using Visual Basic .NET

This article briefly describes, how to access an HF2LI or HF2IS using Visual Basic .NET (VBA.NET, VB.NET).


Please consider that VB is not a supported programming language and it does not appear in the official list. A software update might lead to misbehaviour of the setup due to changes in the text-based interface protocol. Furthermore, the achievable readout rates are lower than what can be achieved using the fully supported APIs, such as MATLAB and LabVIEW. Nevertheless, a way to interface the HF2 Instrument using Visual Basic could be just what you have been missing.

As no Visual Basic API is available for the HF2 Instruments, one has to do a work around by using the Telnet text interface. There is a lot of information about text based programming in the HF2 user manual, which I recommend to have a look at first. Furthermore, if you want to adapt this example to other application cases (reading or writing other settings and values), be referred to the programming chapter where you can find a list of available nodes.



In the described example, functionality is limited to reading values from the two auxiliary input channels. Data acquisition can be started by pressing the “Subscribe” button and stopped by pressing “Unsubscribe”. Make sure, you adapt the code to suit your device number.


Description of the Code

The most fundamental code snippets will be highlighted here. If you need the complete source code, please download the project file at the bottom of this post.


To connect to the HF2 Instrument, a TCP connection has to be established between the Visual Basic application and the ziServer. The server always silently runs on the computer that is connected to the HF2 Instrument using a USB cable.

Public client As TcpClient client = New TcpClient("localhost", "8005") 

Sending data

Once a connection is established, data can be written to the device by writing to a stream.

Public stream As NetworkStream stream = client.GetStream() 
stream.Write(data, 0, data.Length) 

Receiving and parsing data

Reading data can be done in a timer loop using the StreamReader class and its ReadLine method.

Public sr As StreamReader 
sr = New StreamReader(stream) 
s = sr.ReadLine() 

Once a line is read, it has to be parsed to retrieve the information that you are interested in. When receiving a line of auxiliary input samples, one might have to replace the decimal point by a decimal comma or specify a number style for the Double.Parse method. Using the Substring method, the part of the line containing the input voltage information is extracted.

s = s.Replace(".", ",") 
toconvert = s.Substring(s.IndexOf("Ch0=") + 4, 9) 
d = Double.Parse(toconvert) 

Starting data acquisition

Now, data acquisition can be started by subscribing to the auxiliary input nodes. This is done using the subs and unsubs command of the text based programming interface.

sendCommand("subs auxins/0/sample") 
sendCommand("unsubs auxins/0/sample") 

In order to reduce the amount of data that is transmitted to the VB application, averaging of the auxiliary inputs is set to a high value. This, furthermore, reduces noise at the input.

sendCommand("auxins/0/averaging 32768") 

This is it. Attached you will find the source code and the executable: