Skip to main content

Continue from the SDK

After the official installer (driver included) is installed, and you have verified the setup in the configuration tool and exported the code, use this page to connect the SDK to your own project.

Feature overview​

ItemContents
Exported projectComplete and buildable, containing the code, the configuration and the run script
SDK folderMaintained automatically by the tool; contains the SDK files for six languages
Six languagesC# / C / C++ / Python / Java / Rust
Protocol coverageCoE / FoE / FSoE / EoE / SoE / AoE / VoE / MDP

Exported project structure​

After you pick the C# export, the tool generates a complete, buildable project:

EtherCATRestore/
├── EtherCATRestore.csproj # project file (local references already configured)
├── Program.cs # C# usage example (initialization, PDO loop, and the selected protocol code)
├── config.deni # master configuration file
├── run.bat # build and run script
└── lib/
├── DarraEtherCAT.dll # C# class library
└── DarraEtherCAT.xml # IntelliSense documentation
Runs as exported

The exported project already contains the .csproj and the local references, so there is no NuGet package to install. Double-click run.bat or use dotnet run to build and run.

SDK folder​

The official installer extracts the SDKs for all six languages into the SDK/ folder of the installation directory (C:\DARRA\EtherCAT_Master\SDK\ by default); you can also take the latest version from there:

Darra_EtherCAT_Master.exe
SDK/
├── SDK_CSharp/
├── SDK_C/
├── SDK_Cpp/
├── SDK_Python/
├── SDK_Java/
└── SDK_Rust/

Detailed documentation for each language: C# SDK · C SDK · C++ SDK · Python SDK · Java SDK · Rust SDK

Quick start​

Pick the tab for your language and add the SDK as shown:

Option A: NuGet package (recommended)

dotnet new console -n MyEtherCATApp
cd MyEtherCATApp
dotnet add package Darra.EtherCAT.Master

Or search for Darra.EtherCAT.Master in the NuGet package manager of Visual Studio, then copy the exported Program.cs and config.deni into the project directory.

Option B: Local reference

Copy DarraEtherCAT.dll and DarraEtherCAT.xml from the exported lib/ directory or from SDK/SDK_CSharp/ in the configuration tool into the lib/ directory of the project, then:

<Reference Include="DarraEtherCAT">
<HintPath>lib\DarraEtherCAT.dll</HintPath>
</Reference>

With DarraEtherCAT.xml next to the assembly you get full IntelliSense.

Complete example​

Below is the same Hello-World in all six languages, fully equivalent: enumerate NICs → load the DENI configuration → build the master → enter OP → read the slave identity object 0x1018:01 (VendorID). The steps are identical in all six SDKs; only the syntax differs.

using System;
using System.Linq;
using DarraEtherCAT_Master;

class Program
{
static void Main()
{
// 1. Enumerate the NICs and pick the interface that has slaves
var nic = DarraEtherCAT.GetNetworkInfo(needSlavesNum: true)
.FirstOrDefault(n => n.SlaveNum > 0);
if (nic == null) { Console.WriteLine("未发现从站"); return; } // No slaves found

// 2. Build the master from the DENI configuration + NIC
var result = new DarraEtherCAT()
.SetENI(@"config.deni")
.SetNetwork(nic)
.EnableAutoStartup()
.Build();
if (!result.Success) { Console.WriteLine(result.Message); return; }

DarraEtherCAT master = result;
Console.WriteLine($"发现 {master.SlaveCount} 个从站"); // Found {master.SlaveCount} slaves

// 3. Enter OP (OP = fully started)
if (!master.SetState(EcState.OP, out string msg)) { Console.WriteLine(msg); master.Dispose(); return; }

// 4. Read VendorID of slave 1 (0x1018:01)
byte[] data = master.Slaves[0].CoE.SDORead(0x1018, 0x01);
uint vendorId = BitConverter.ToUInt32(data, 0);
Console.WriteLine($"VendorID = 0x{vendorId:X8}");

master.Close();
master.Dispose();
}
}
Identical steps
  1. Enumerate NICs → find the interface with slaves attached
  2. Load the DENI configuration → exported by the configuration tool
  3. Build and start the master → INIT → PRE_OP → SAFE_OP → OP are completed automatically
  4. Read the object dictionary → 0x1018:01 (VendorID) is a standard object that every slave must have

Once one language works, extending to the PDO cycle and to protocol reads and writes uses the same model.

Read and write PDOs from the cyclic thread​

Zero-copy means the structs are mapped onto the process image, so any thread can change fields directly. After entering OP, hook the reads and writes onto the PDO cyclic callback of the SDK and let the bus cycle set the cadence; do not start a thread of your own that busy-waits in while (true).

This is a raw PDO write demo, not a complete CiA 402 motion recipe

It only shows which mapped fields change in each cycle. Copy it as it stands to drive a servo and the axis will judder, because three things are still missing:

  1. Enable handshake — 0x06 → 0x07 → 0x0F; you cannot write 0x000F straight away;
  2. State check — (statusWord & 0x6F) == 0x27 is OperationEnabled; (statusWord & 0x4F) == 0x08 is Fault;
  3. Target = actual while not enabled — write the target position back to the actual position and drive the control word from the state machine (0x0006 while not enabled, then 0x0007 and 0x000F as the handshake advances); the target must not jump from 0 to 10000.

Inside the callback, only read and write mapped fields: no Thread.Sleep / Sleep, and no SDOs (a mailbox transaction takes about 2 cycles and drags the real-time cadence down). In the example, 0x000F is held only when the status word already reads OperationEnabled; the enable handshake (0x06 → 0x07 → 0x0F) has to be advanced step by step from the status word on the not-enabled branch — you cannot rely on writing 0x000F unconditionally in the callback. These are single-axis commissioning examples, not synchronized-axis usage; multi-axis synchronization must be issued per axis from the same cyclic callback — see CiA 402.

using System.Runtime.InteropServices;
using DarraEtherCAT_Master;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct InPdo
{
public ushort StatusWord;
public int ActualPosition;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct OutPdo
{
public ushort ControlWord;
public int TargetPosition;
}

var slave = master.Slaves[0];
int nextPosition = 0; // Trajectory state; the first tick aligns to the actual position inside the callback
bool aligned = false;

// C# forbids lambdas capturing ref locals (CS8175): the mapping must be fetched once per callback body (still zero-copy)
master.Events.ProcessDataCyclicSync += (masterIndex) =>
{
ref readonly InPdo inputs = ref slave.PDO.InputsMapping<InPdo>();
ref OutPdo outputs = ref slave.PDO.OutputsMapping<OutPdo>();

if ((inputs.StatusWord & 0x6F) == 0x27) // OperationEnabled: only issue motion targets once enabled
{
if (!aligned) { nextPosition = inputs.ActualPosition; aligned = true; } // First tick aligns, no jump
outputs.ControlWord = 0x000F; // Stay enabled (do not write 0x001F in CSP)
outputs.TargetPosition = nextPosition; // One target per cycle, advanced by your trajectory
}
else
{
outputs.ControlWord = 0x0006; // Not enabled: stop command (also the first step of the 0x06->0x07->0x0F handshake)
outputs.TargetPosition = inputs.ActualPosition; // Target = actual while disabled
aligned = false;
}
};

Define the PDO structs​

If you are not using the code exported by the tool, define the PDO structs by hand. Three rules apply everywhere:

  1. Tight order — declare the fields in PDO mapping order; do not let the compiler insert padding bytes
  2. Little-endian — the EtherCAT protocol specifies PDO data as little-endian
  3. Size match — the total byte count of the struct must equal the Ibytes / Obytes of the slave

How each language declares the packed layout:

  • C# — [StructLayout(LayoutKind.Sequential, Pack = 1)] struct ...
  • C++ — #pragma pack(push, 1) struct ... #pragma pack(pop)
  • C — __attribute__((packed)) (GCC/Clang) or #pragma pack(1) (MSVC)
  • Python — fixed-width struct classes (_pack_ = 1, field order matching the PDO mapping)
  • Java — class X extends Structure { ... }
  • Rust — #[repr(C, packed)] struct ...

Type cross-reference:

  • 1 bit — bool
  • 8 bit — sbyte / byte / int8 / uint8 / i8 / u8
  • 16 bit — short / ushort / int16 / uint16 / i16 / u16
  • 32 bit — int / uint / float / int32 / uint32 / i32 / u32 / f32
  • 64 bit — long / ulong / double / int64 / uint64 / i64 / u64 / f64

Note: bool occupies only 1 bit, and any value read that is not 0 counts as true; the standard defines only 0x00 / 0x01, and only those two values are written out.

Always compare the struct size with the Ibytes / Obytes the slave reports before running; if they do not match, do not start the PDO.

Release resources​

How each language releases resources (works best with end of scope / with / try-with-resources / RAII):

  • C# — master.Close(); master.Dispose();
  • C++ — automatic destruction when leaving scope (RAII)
  • C — Stop(master); Dispose(master);
  • Python — with EtherCATMaster() as master: ... (leaving with releases automatically)
  • Java — try (var master = ...) { ... } (try-with-resources)
  • Rust — master drops automatically when it leaves scope

Next steps​

ProtocolPurposeDocumentation
FSoEFunctional safety communicationUsing FSoE
CoECANopen over EtherCAT, SDO reads and writes against the object dictionaryUsing CoE
FoEFile transfer (firmware update)Using FoE
EoEEthernet pass-throughUsing EoE
SoEServo parameter accessUsing SoE
AoEADS over EtherCATUsing AoE
DCDistributed clock synchronizationUsing DC