Skip to main content

Slave API

Accessed through master.Slaves[n] (0-based index).​

Quick start​

Index access​

DarraEtherCAT.Slave slave = master.Slaves[0];  // 1st slave (0-based)
int count = master.Slaves.Count; // total slave count

Group access​

DarraEtherCAT.Slave servo = master.Groups[1][0];  // group 1 (default group), 1st slave
DarraEtherCAT.Slave io = master.Groups[1][2]; // group 1, 3rd slave
int groupCount = master.Groups[1].Count; // slave count in group 1

Address access​

// Physical configuration address (auto-assigned)
ushort addr = slave.ConfigAddr; // for example 0x1001

// Alias address (configured in the EEPROM, optional)
ushort alias = slave.AliasAddress;

// Look up by address
var target = master.Slaves.FirstOrDefault(s => s.ConfigAddr == 0x1001);

Identity access​

string name = slave.Name;               // device name
string driveName = slave.DriveName; // drive name (SDO 0x1008)
uint vendorId = slave.VendorId; // vendor ID
uint productId = slave.ProductId; // product ID
uint revision = slave.RevId; // revision
uint serial = slave.SerialNumber; // serial number

// Look up by name
var target = master.Slaves.FirstOrDefault(s => s.Name == "EL2008");

Protocol access​

CategoryPropertyRead/writeDescription
PDOslave.PDOread-onlyProcess data object, real-time IO data read/write
CoEslave.CoEread-onlyCANopen over EtherCAT, SDO object dictionary access
CiA 402slave.CoE.CiA402read-onlyServo drive protocol, state machine management and enable control
CiA 401slave.CoE.CiA401read-onlyGeneric I/O module protocol, digital/analog input and output
MDPslave.MDPread-onlyModular device protocol, module detection and automatic configuration
SoEslave.SoEread-onlyServo over EtherCAT, servo drive parameters
FoEslave.FoEread-onlyFile over EtherCAT, file transfer
EoEslave.EoEread-onlyEthernet over EtherCAT, Ethernet tunnel
AoEslave.AoEread-onlyADS over EtherCAT, Beckhoff ADS protocol
VoEslave.VoEread-onlyVendor over EtherCAT, vendor-specific protocol
FSoEslave.FSoEread-onlyFunctional Safety over EtherCAT, functional safety protocol

PDO data read/write​

After entering OP, map a packed struct onto the process image: the input side is read-only, writing a field on the output side sends it on the bus, and any thread can use it:

[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;
}

ref readonly InPdo inputs = ref slave.PDO.InputsMapping<InPdo>();
ref OutPdo outputs = ref slave.PDO.OutputsMapping<OutPdo>();
outputs.ControlWord = 0x000F;
Console.WriteLine($"当前位置: {inputs.ActualPosition}"); // Current position: {inputs.ActualPosition}

EMCY emergency messages​

See CoE emergency messages for reading and clearing the EMCY history (slave.CoE.GetHistory(int maxRecords = 64) / ClearHistory() / EmcyCount).

Topology query​

Get the child slave list through the slave's Children property to walk the topology tree.

// get the child slaves of the slave
var children = slave.Children;

// walk the topology tree
void PrintTree(DarraEtherCAT.Slave s, int depth)
{
var indent = new string(' ', depth * 2);
Console.WriteLine($"{indent}[{s.SlaveNum}] {s.Name}");
foreach (var child in s.Children)
PrintTree(child, depth + 1);
}

// note: ParentStation is really a 1-based slave index, not the station address; 0 means the parent is the master
foreach (var slave in master.Slaves.Where(s => s.Topology == 1 || s.ParentStation == 0))
PrintTree(slave, 0);