Slave API
Access via master.getSlave(n) (1-based index), or get the list with master.slaves(). The DENI one-step initialization path does not populate that list (getSlave throws IndexOutOfBoundsException and slaves() is empty); instead construct new Slave(master.MasterIndex, (short) 1, master) directly from the master index + station number. See ENI/DENI configuration loading.
Quick start
Index access
Slave slave = master.getSlave(1); // 1st slave (1-based)
List<Slave> all = master.slaves(); // slave list
int count = master.slaveCount(); // total slave count
Group access
Group membership, group dividers and active groups are covered in Groups and dividers; the slave side only exposes slave.group() / slave.group(byte).
Address access
// Physical configuration address (auto-assigned)
short addr = slave.configAddr();
// Alias address (configured in EEPROM, optional)
short alias = slave.aliasAddress();
// Look up by address
Slave target = master.slaves().stream()
.filter(s -> s.configAddr() == 0x1001)
.findFirst().orElse(null);
Identity access
String name = slave.name(); // Device name (EEPROM)
String driveName = slave.driveName(); // Device name (SDO 0x1008)
int vendorId = slave.vendorId(); // Vendor ID
int productId = slave.productId(); // Product ID
int revision = slave.revId(); // Revision
int serial = slave.serialNumber(); // Serial number
// Look up by name
Slave target = master.slaves().stream()
.filter(s -> "EL2008".equals(s.name()))
.findFirst().orElse(null);
Status and errors
In the slave's AL Status byte the low 4 bits are the state, and bit4 (0x10) is the Error bit. 0x18 = OP + fault, 0x14 = SafeOp + fault — the two most common in the field (watchdog drop-off, DC sync loss, SM watchdog trip).
| Method | Returns | Description |
|---|---|---|
state() | EcState | Only the low 4 bits of the base state; the Error bit is discarded — 0x18 still reports OP |
stateRaw() | int | The raw wire byte (& 0xFF); the Error bit is fully preserved; 0 when it cannot be read |
hasError() | boolean | (stateRaw() & 0x10) != 0 |
status() | EcState.Status | Named composite states: 0x14 → SAFE_OP_FAULT, 0x18 → OP_FAULT; out-of-table values keep the raw value |
stateLive() / stateRawLive() / hasErrorLive() | same as above | Force a frame refresh before reading, bypassing the kernel cache; used for debugging and OP-drop detection |
The return value of state() has not changed: EcState is a closed enum that cannot hold 0x14 / 0x18, and changing the return type would break every slave.state() == EcState.OP call. So never judge whether there is a fault from state() alone:
Slave s = master.getSlave(1);
if (s.state() == EcState.OP && !s.hasError()) {
// Truly healthy OP
} else if (s.hasError()) {
System.out.printf("从站 %d 报错: %s, AL=0x%04X%n", // Slave %d error: %s, AL=0x%04X%n
s.SlaveNum, s.status().pretty(), s.errorCodeRaw());
}
status() is an immutable value object (not an enum). Out-of-table composite values (0x1A, for example) are not folded into NONE: the raw value lives in getValue(), isUnknown() is true, and pretty() prints "0x1A".
| Call | Result |
|---|---|
status().hasFault() | whether the Error bit is set |
status().baseState() | the low 4-bit base state (EcState.OP / EcState.SAFE_OP …) |
status().isRunning() | SAFE_OP or OP, without Error |
status().isOp() | exactly OP, without Error |
status().pretty() | "OP_FAULT (OP+Error)"; out-of-table value "0x1A" |
Compare states with .equals(...), not ==: named constants may use ==, but every fromRaw() on an out-of-table value returns a new instance.
AL Status Code: enum view vs raw value
errorCode() is the enum view and only lists codes inside the ETG.1020 table; out-of-table codes fall to EcALState.UNKNOWN (0xFFFF) — the same value as "not read at all". For the device's real raw value use errorCodeRaw():
int raw = slave.errorCodeRaw(); // Lossless 16-bit: 0xFFFF = could not be read, 0 = the device confirmed no error
EtherCATTypes.EcALState named = slave.errorCode(); // Named view: codes outside the table = UNKNOWN
errorCodeLive() / errorCodeRawLive() are the forced real-time reads with the same semantics.
Protocol access
| Category | Protocol | Method | Type | Read/write | Description |
|---|---|---|---|---|---|
| Process data | PDO | slave.PDO() | PDO | read-only | Process data object, real-time IO data read/write |
| Mailbox protocol | CoE | slave.coe() | CoE | read-only | CANopen over EtherCAT, SDO object dictionary access |
| Drive configuration | CiA 402 | slave.cia402() | CiA402 | read-only | Servo drive protocol, state machine management and enable control |
| CiA 401 | slave.cia401() | CiA401 | read-only | Generic I/O module protocol, digital/analog input and output | |
| MDP | slave.MDP() | MDP | read-only | Modular device protocol, module detection and automatic configuration | |
| Mailbox protocol | SoE | slave.soe() | SoE | read-only | Servo over EtherCAT, servo drive parameters |
| FoE | slave.foe() | FoE | read-only | File over EtherCAT, file transfer | |
| EoE | slave.eoe() | EoE | read-only | Ethernet over EtherCAT, Ethernet tunnel | |
| AoE | slave.aoe() | AoE | read-only | ADS over EtherCAT, Beckhoff ADS protocol | |
| VoE | slave.voe() | VoE | read-only | Vendor over EtherCAT, vendor-specific protocol | |
| FSoE | slave.fsoe() | FSoE | read-only | Functional Safety over EtherCAT, functional safety protocol |
PDO data read/write
After entering OP, map a packed Structure onto the process image and let multiple threads modify fields directly:
@FieldOrder({"statusWord", "actualPosition"})
public static class InPdo extends Structure {
public short statusWord;
public int actualPosition;
public InPdo() { super(ALIGN_NONE); }
}
@FieldOrder({"controlWord", "targetPosition"})
public static class OutPdo extends Structure {
public short controlWord;
public int targetPosition;
public OutPdo() { super(ALIGN_NONE); }
}
PDO pdo = slave.PDO();
InPdo inputs = pdo.inputsMapping(InPdo.class);
OutPdo outputs = pdo.outputsMapping(OutPdo.class);
inputs.read(); // Refresh fields: without read() you get the values from mapping time
System.out.println("当前位置: " + inputs.actualPosition); // Current position: {inputs.actualPosition}
outputs.controlWord = 0x000F;
outputs.write(); // Write back; sent in the next cycle
EMCY emergency messages
An EtherCAT slave sends EMCY (Emergency) messages when it detects an exception. Access them through slave.coe(): getEmergencyHistory() returns all emergency messages recorded for this slave (not truncated by a count limit); on read failure it returns an empty list and leaves a trace in the log. clearEmergencyHistory() clears the records for this slave.
// Get the emergency history
List<CoE.EmergencyMessage> history = slave.coe().getEmergencyHistory();
for (CoE.EmergencyMessage msg : history) {
System.out.printf("错误码: 0x%04X, 寄存器: 0x%02X%n", // Error code: 0x%04X, register: 0x%02X%n
msg.ErrorCode, msg.ErrorRegister);
}
// Clear the emergency history
slave.coe().clearEmergencyHistory();
Topology query
Use master.topologyChildren(...) / master.topologyRoots() to obtain the topology relations and walk the topology tree.
master.slaveTopology().count() returns the number of root nodes (entry port type is MII, or parentStation()==0, directly attached to the master). EBUS modules hanging under a coupler do not count as roots. It is not master.slaveCount() (the number of slaves on the whole network). The slave-level slave.topology().count() has no master list and returns 0.
// Rebuild the topology (usually once after entering OP)
master.rebuildTopology();
int roots = master.slaveTopology().count();
// Walk the topology tree
void printTree(int slaveIdx, int depth) {
String indent = " ".repeat(depth * 2);
Slave slave = master.getSlave(slaveIdx);
System.out.printf("%s[%d] %s%n", indent, slaveIdx, slave.name());
for (int child : master.topologyChildren(slaveIdx)) {
printTree(child, depth + 1);
}
}
for (int root : master.topologyRoots()) {
printTree(root, 0);
}