Skip to content

icon=”mk-icon-shopping-cart” url=”” align=”center” outline_skin=”light”]Get a HDrive now.[/mk_button][/vc_column][vc_column width=”1/6″][/vc_column][/mk_page_section]

Code Examples

The HDrive Servo can be controlled by various interfaces. Find tutorials and code examples below for a smooth start.
For the Ethernet interface, we suggest using a TCP-Client/Server connection to the motor. Therefore you need to open a TCP socket to the HDrive. The Servo is acting as a server and is able to handle one client connection. The web interface can be used to observe the TCP connection in parallel.

Python example


import socket with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(('', 1000))
s.sendall(b'<control pos=\"1000\" speed=\"500\" current=\"200\" mode=\"129\" acc=\"1000\" decc=\"1000\" />')

C# example

using System;
using System.Net.Sockets;
using System.Text;

namespace ConsoleApplication
    class Program
        static void Main(string[] args)
            //create instance from TCPClient Object
            var client = new TcpClient("", 1000);

            //if a client has found, the program continues
            Console.WriteLine("client found!");

            //create an instance of the C# Stream class and get the Stream to the client
            var networkStream = client.GetStream();
            byte[] encodedBytes = System.Text.Encoding.ASCII.GetBytes("");

            //send the drive order to the HDrive
            networkStream.Write(encodedBytes, 0, encodedBytes.Length);

            //continuously read data comming from the HDrive
            byte[] buffer = new byte[85]; // read in chunks of 85 Byte 
            int bytesRead;
            while ((bytesRead = networkStream.Read(buffer, 0, buffer.Length)) > 0)

Matlab example

Send HDrive ticket

t=tcpip('', 20000, 'NetworkRole', 'client'); %configure TCP Client and Port
fopen(t); % open TCP Connection
fwrite(t, '<control pos=\"1000\" speed=\"500\" current=\"200\" mode=\"129\" acc=\"1000\" decc=\"1000\" />') % send XML drive order
pause( 5 ) % wait 5 seconds ( because the motor would stop if you close the connection before )

Receive HDrive ticket (TCP mode)

Please notice: The HDrive is sending the Tickets with up to 2 kHz. This means you have to read very fast to always get the newest ticket. Therefore it is recommended to read the ticket in a loop or to reduce the send frequency by setting up a pre-scaler with the web interface.

clear all;
t=tcpip('', 1000, 'NetworkRole', 'client'); %configure TCP Client and Port
fopen(t);%open TCP Connection
A = fread(t, 150); %receive one HDrive ticket
% convert received bytes into string
recieved_string = sprintf('
% interpret string
time_ms = str2num(recieved_string(137:146));
position = str2num(recieved_string(23:32));
calibration = str2num(recieved_string(59:62));
speed_RPM = str2num(recieved_string(42:49));
% close connection

Receive HDrive ticket (UDP mode)

In this scenario, the HDrive has to be configured to send UDP tickets to the host. This configuration can be done in the Web interface

hold on;
clear all;
%configure and open UDP Connection
u.ByteOrder = 'littleEndian';
% read UDP data and convert to 32bit integers
data = fread(u, 3, 'int32');
%store data to local variables
time = data(1);
position = data(2);
speed = data(3);
% close connection