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
| Category | Property | Read/write | Description |
|---|---|---|---|
| PDO | slave.PDO | read-only | Process data object, real-time IO data read/write |
| CoE | slave.CoE | read-only | CANopen over EtherCAT, SDO object dictionary access |
| CiA 402 | slave.CoE.CiA402 | read-only | Servo drive protocol, state machine management and enable control |
| CiA 401 | slave.CoE.CiA401 | read-only | Generic I/O module protocol, digital/analog input and output |
| MDP | slave.MDP | read-only | Modular device protocol, module detection and automatic configuration |
| SoE | slave.SoE | read-only | Servo over EtherCAT, servo drive parameters |
| FoE | slave.FoE | read-only | File over EtherCAT, file transfer |
| EoE | slave.EoE | read-only | Ethernet over EtherCAT, Ethernet tunnel |
| AoE | slave.AoE | read-only | ADS over EtherCAT, Beckhoff ADS protocol |
| VoE | slave.VoE | read-only | Vendor over EtherCAT, vendor-specific protocol |
| FSoE | slave.FSoE | read-only | Functional 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);