// LEARN · Host Link Guide

Host Link by hand vs. KcpX vs. KV COM+ Library in C#

The same task — reading DM100–DM102 from a KEYENCE KV series PLC — written three ways: sending Host Link commands yourself, the .NET library KcpX, and KEYENCE's KV COM+ Library. We compare code size, setup, supported environments and when each one fits.

Updated

There are three main ways to read and write a KEYENCE KV series PLC from C#: send the Host Link commands yourself, use a library such as KcpX, or use KEYENCE’s KV COM+ Library.

In this article we write the same task all three ways: “read the three words DM100–DM102 as signed 16-bit values from a PLC connected over Ethernet.” Writing them side by side shows clearly what each one takes care of and what it leaves to you.

1. Writing Host Link by hand

We use nothing but .NET’s built-in TcpClient. The RDS DM100.S 3 command from the command format article goes out as a plain ASCII string.

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

using var client = new TcpClient();
client.Connect("192.168.0.10", 8501);
using var stream = client.GetStream();

// Continuous read of 3 words from DM100 as signed 16-bit (.S)
stream.Write(Encoding.ASCII.GetBytes("RDS DM100.S 3\r"));

// The response ends with CR LF, so read up to there
var buffer = new List<byte>();
while (buffer.Count < 2 || buffer[^2] != '\r' || buffer[^1] != '\n')
{
    int b = stream.ReadByte();
    if (b < 0)
        throw new IOException("Connection closed");
    buffer.Add((byte)b);
}
string response = Encoding.ASCII.GetString(buffer.ToArray()).TrimEnd('\r', '\n');

// E0–E6 are error responses
if (response.Length == 2 && response[0] == 'E')
    throw new Exception($"The PLC returned an error: {response}");

// Values come back separated by spaces, e.g. "+15025 -25400 +00000"
short[] values = response.Split(' ').Select(short.Parse).ToArray();

Unlike the MC protocol, where you assemble a binary frame, Host Link commands are readable strings, so the code stays short. Even so, this code only handles “read three DM words as .S”. To use it for real, you end up adding things like:

  • Splitting commands when you exceed the per-command point limit (1000 points for most devices, 256 on KV Nano, and so on)
  • Assembling values that span four words, such as double or long, from individual words
  • Handling “channel + 2-digit bit number” devices such as R and MR, and hexadecimal devices such as B and W
  • Splitting the “contact,current,set” response of timers and counters
  • Timeouts when no response arrives, and re-establishing the connection afterwards

Writing it by hand is the best way to understand what the commands contain. But if you build it into an application, you have to test all of the above yourself.

2. Using KcpX

Add KcpX from NuGet and write the same task.

using KcpXLib;
using KcpXLib.Enums;

using var kcpx = new KcpX("192.168.0.10", 8501);
short[] values = kcpx.BatchRead<short>(Prefix.DM, "100", 3);

The single BatchRead<short> line takes care of the command building and response parsing from section 1. Change the type to int or double, and KcpX also picks the data format (.L or .D) and assembles the words. Reads beyond the point limit are split automatically, following the limits of the PLC series you pass as series to the constructor.

When the PLC returns an error, a HostLinkException is thrown, and its ErrorCode property gives you the code (E0–E6). When no response arrives, a TimeoutException is thrown, and over TCP the connection is closed so that a late, stale response is never mistaken for the next one.

KcpX is written purely in .NET, so it runs on Linux and macOS as well as Windows. In exchange, the only paths it supports are Host Link over Ethernet (TCP/UDP) and the KV STUDIO simulator. The target PLCs are the KV-8000 / 7000 / 5000 families and KV Nano (via KV-NC1EP).

3. Using KV COM+ Library

KV COM+ Library is KEYENCE’s communication library for Windows. It is provided as ActiveX controls, and the product page lists Visual Basic, Visual C++, Excel and Access as supported environments. To use it, you buy and install the product, then add the controls to the Visual Studio toolbox or references.

using AxDATABUILDERAXLibLB;
using DATABUILDERAXLibLB;

var plc = new AxDBCommManager();
plc.CreateControl();
plc.PLC = DBPlcId.DBPLC_DKV5000;  // PLC model to connect to
plc.Peer = "192.168.0.10:8500";   // Destination (IP address:port)
plc.Connect();
try
{
    int[] values = new int[3];
    for (int i = 0; i < values.Length; i++)
        values[i] = Convert.ToInt32(plc.ReadDevice(DBPlcDevice.KV5000_DM, (100 + i).ToString()));
}
finally
{
    plc.Disconnect();
}

This code uses only what we could confirm from published usage examples of the DBCommManager control: the PLC and Peer properties and the Connect, Disconnect and ReadDevice methods. That is why it reads one point at a time. For methods that read several points at once, the return type and data format options, and how errors are reported, check the KV COM+ Library user’s manual.

You specify the PLC model with the DBPlcId enum and the device with the DBPlcDevice enum. The published examples use port 8500 rather than the Host Link port 8501.

KV COM+ Library’s strength is the breadth of its target models and extras. The product page lists generations from KV-8000 back to KV-1000 and KV-700, plus the KV Nano series. It also includes DBDeviceManager for periodic device reads and display controls such as meters. KV COM+ for Excel, which brings PLC data into Excel, belongs to the same product series.

On the other hand, it runs only on Windows, and you have to buy the product to use it.

The three side by side

Host Link by handKcpXKV COM+ Library
What you addNothing (built into .NET)NuGet package KcpXProduct installation and ActiveX control references
Specifying the targetIP and port in codeIP and port in the constructorPLC (model) and Peer (destination) properties
Operating systemsAnywhere .NET runsWindows, Linux, macOSWindows
Development environment.NET.NET 7–9, .NET Framework 4.6.1+, .NET Core 2.0+Visual Basic, Visual C++, Excel, Access
Target PLCsModels with Host LinkKV-8000 / 7000 / 5000 families, KV Nano, KV STUDIO simulatorKV-8000 through KV-700 generations, KV Nano
Value typesConvert from strings yourselfSpecify bool, short, int, float, double and more as the typeDevices chosen by enum (see the manual for details)
Error handlingCheck E0–E6 yourselfExceptions (HostLinkException)See the manual
Points per callSplit yourself above the limitSplit automatically above the limitSee the manual
License—MIT (free, commercial use allowed)Paid (sold per license)

Which one to choose

When choosing, ask the following three questions. Answer them from the top and you will usually land on one.

1. Do you connect to older PLCs such as KV-1000 or KV-700?

KcpX targets the KV-8000 / 7000 / 5000 families and KV Nano. For PLCs from earlier generations, choose KV COM+ Library, whose target models include them.

2. Will it run on anything other than Windows?

Next, consider where it runs. On a Linux server, in a Docker container, on a Raspberry Pi or on a macOS development machine, choose KcpX. KV COM+ Library runs only on Windows.

3. How will you distribute it, and under what license?

On Windows, both KcpX and KV COM+ Library can do the job. What decides it is how you build and ship the app.

  • KcpX fits new C# apps, apps deployed to many PCs, and apps you ship outside your company. It is free under the MIT license, and a NuGet reference bundles it into the app, so no per-PC installation is needed.
  • KV COM+ Library fits apps with existing VB or Excel assets built on KV COM+, and in-house tools that want to use its display controls such as meters as-is.

Common scenarios

ScenarioChoice
Collect factory operating data on a Linux server and show it on a web dashboardKcpX
Run a gateway that sends PLC data to the cloud in DockerKcpX
Automatically test ladder behavior against the KV STUDIO simulator in CIKcpX
Read KV-1000 data from existing Excel or VB toolsKV COM+ Library
Talk to the PLC from a language other than C# or from a microcontrollerBy hand (the command knowledge from this guide)

Knowing the raw commands helps whichever you choose

You will rarely build a real system from raw commands. Still, knowing the command format lets you track down problems even while using a library: when you look at traffic in Wireshark you know what each character means, and you know what an E0 or E1 from the PLC refers to. The command format and error response articles give you that knowledge.

Sources

  • KEYENCE KV-EP21V/KV-8000(A)/KV-7500/KV-5500/KV-NC1EP User's Manual, EtherNet/IP Functions (Japanese edition) chapter 8 pp.8-2–8-7, 8-17–8-22, 8-36
  • [object Object]