Skip to main content

C# SDK overview

The .NET class library for building EtherCAT master applications.

Current version

This documentation covers C# SDK v2.13.16. The version that ships with the official installer is authoritative; application code does not need to query the SDK version.

Installation​

Install-Package Darra.EtherCAT.Master

Or with the .NET CLI:

dotnet add package Darra.EtherCAT.Master

The NuGet package contains DarraEtherCAT.dll (.NET Standard 2.0) and the win-x64 SDK runtime libraries (in the runtimes/win-x64/ runtime folder of the package). At build time the SDK runtime libraries are copied to the output directory automatically.

Requirements​

  • Operating system: Windows x64 (the only platform delivered today), Windows 7+ (Windows 10/11 recommended), a standard Ethernet NIC
  • Runtime: .NET Standard 2.0 — verified with .NET Framework 4.6.1+ and .NET 6–8. net9 / net10 require UseAppHost=false and running through dotnet app.dll; otherwise use net8
  • Privileges: Administrator
  • Real-time driver: provided with the installer; microsecond-level real time with no dedicated hardware required

Quick start​

Using the Darra configuration tool
  1. Open the Darra configuration tool and scan the EtherCAT network
  2. Configure slave parameters (PDO mapping, DC settings and so on)
  3. Export the DENI configuration file
  4. Load the DENI file in your code to initialize the master
// ★ Recommended: use a DENI file (exported by the Darra configuration tool)
var result = new DarraEtherCAT()
.SetENI(@"C:\EtherCAT\MyProject.deni")
.Build();

if (result.Success)
{
var master = result.Master;
master.SetState(EcState.OP);
// Start communicating...
}

Alternative: automatic NIC search (no DENI)​

Without a DENI file, use GetNetworkAdapters() to enumerate all NICs and take the one that actually has slaves in the scan; there is no need to hard-code \Device\NPF_{GUID}:

// Enumerate NICs and read the slave count (~500ms-1.5s)
var adapters = DarraEtherCAT.GetNetworkAdapters(includeSlaveCount: true);

// Pick the port that actually has slaves (there is usually only one)
var ec = adapters.FirstOrDefault(a => a.ScanStatus == NicScanStatus.Found);
if (ec == null)
{
Console.WriteLine("未发现 EtherCAT 网络"); // No EtherCAT network found
return;
}
Console.WriteLine($"找到: {ec.Description} (从站 {ec.SlaveCount} 台, MAC={ec.Mac})"); // Found: {ec.Description} ({ec.SlaveCount} slaves, MAC={ec.Mac})

using var master = new DarraEtherCAT()
.SetNetwork(ec.Name)
.Build()
.Master;

master.SetState(EcState.OP, out _);
Which one to use
  • DENI — production; loads the full set of ESI/PDO/DC parameters in one line, recommended.
  • Automatic search — demos / tooling applications / scenarios that do not need preconfigured startup parameters.

Advanced API​

FeatureDescriptionSee
One-step initializationThe fluent configuration plus Build() initializes the master in one step; no step-by-step callsInitialization § Build()
State chain transition (SetState)Runs the multi-step state transition chain automaticallySetState
Startup parameter pipeline (slave.Startup.AddParameter / StartupParameter.Apply)Add startup parameters in bulk and apply them to the slave one by oneStartup parameters
CiA 402 drive state machineOne-step enable / fault reset / state parsingCiA 402
EMCY emergency message bufferRead the emergency message history of the slaveEMCY emergency messages
PDO data read/writeslave.PDO.InputsMapping / OutputsMapping stay in effect after one mapping (I area read-only, O area writable); CopyInputPdo / WriteOutputPdo are snapshotsPDO read/write
Topology queryBuild and query the slave network topology treeTopology navigation
// Enable the servo drive in one step (CiA 402 is reached through slave.CoE.CiA402)
slave.CoE.CiA402.Enable();

// Read and write a whole PDO frame (caller buffer + MemoryMarshal)
byte[] inputFrame = new byte[slave.InputsByteCount];
if (slave.CopyInputPdo(inputFrame, out int copied, out int required) == PdoIoStatus.Success
&& copied == required)
{
var input = MemoryMarshal.Read<ServoInput>(inputFrame);
}

// Read emergency messages (the most recent records in the ring, oldest → newest)
var emcyList = slave.CoE.GetHistory();

// Topology query
var children = slave.Children;

Licensing and activation​

Authorization.LicenseStatus​

public static LicenseStatus LicenseStatus { get; }

The current license status. Every read re-confirms whether the license is valid; when confirmation cannot be completed it returns NotVerified, which is handled as unlicensed — the previous conclusion is not reused.

Only Verified means the license is valid; Failed means verification failed (including a revoked license), Expired means the license has expired, and QuotaExceeded means the enterprise license seats are exhausted. Every other value is handled as unverified. For activation and machine binding, see Licensing and activation.

Related structures:

public enum LicenseStatus
{
NotVerified = 0, // Not verified yet / verification cannot be completed (treat as unauthorised)
Verifying = 1, // Verification in progress; for host diagnostics only, not a stable contract value
Verified = 2, // Passed verification, the only valid state
Failed = 3, // Verification failed (including revoked)
Expired = 4, // Licence expired
QuotaExceeded = 7 // Enterprise licence seats exhausted
}

Example:

using DarraEtherCAT_Master;

var status = Authorization.LicenseStatus;
if (status != LicenseStatus.Verified)
Console.WriteLine($"授权不可用: {status}"); // License not available: {status}

Authorization.Activate(string activationCode)​

public static (bool success, string message) Activate(string activationCode)

Completes online activation with the activation code; on success LicenseStatus becomes Verified immediately, with no restart required.

Parameters:

  • activationCode (string) — the activation code issued at purchase; case and separators are ignored on input

Return value:

  • success (bool) — whether activation succeeded
  • message (string) — result message: "activated" / "activation code is empty" / "activation code has an invalid format"; any other failure returns the reason given by the runtime libraries

Administrator privileges and a network connection are required. The license is bound to the local machine; after moving to another machine, activate again on the new machine; before re-activating, clear the existing license on this machine first. See Licensing and activation for details.

Example:

var (success, message) = Authorization.Activate("AAAA-BBBB-CCCC-DDDD");
Console.WriteLine(success ? "授权已生效" : $"激活失败: {message}"); // license activated / activation failed: {message}

Public API layers​

The documentation describes public contracts only; it does not copy every public member of the code into the docs. To decide whether an entry point is usable, first check which layer it belongs to:

LayerCoverageStability
Stable contractInitialization / build, state machine, PDO, SDO, DC, diagnostics, redundancy, hot-plug, ESIAs documented on each page; stable for external use
Advanced / low-levelDiagnostic probes, pointers / handlesMay be documented, but not promised stable; do not use it in new code

Rule for new APIs: decide which layer it belongs to first, then write the docs; do not copy the public surface of the code into the docs automatically.

The SDK also exposes a number of non-public interfaces reserved for Darra's own products; they are not part of the promised public contract, so third parties must not depend on them.

Error code localisation​

ToString() on enums such as EcALState and SDOError returns the member name (in English), for example NoError or SyncManagerWatchdog; when you need a Chinese description or recovery hint, use Print.GetDescriptionChinese(...) / Print.GetRecoveryHintChinese(...). If the application needs another language, map it itself.

// DarraEtherCAT_Master namespace
string desc = ((EcALState)code).GetDescriptionChinese(); // Chinese description
string hint = ((EcALState)code).GetRecoveryHintChinese(); // Chinese recovery hint

The same applies to SDO Abort: ((SDOError)abort).ToString() gives the member name; the Abort Code is a numeric value such as 0x06020000, and the application maps the display text itself.

Version compatibility​

Current version: C# SDK v2.13.16. The SDK, driver and GUI share one version number; a MINOR or MAJOR upgrade requires reinstalling the matching driver.