Slave API
Accessed through master[n] or master.slave(n) (1-based index).
Quick start
Index access
slave = master[1] # the 1st slave (1-based index)
slave = master.slave(1) # equivalent form
count = master.slave_count # total number of slaves
Group access
servo = master.groups[1][0] # the 1st slave of group 1 (the default group)
io = master.groups[1][2] # the 3rd slave of group 1
group_count = len(master.groups[1]) # number of slaves in group 1
Address access
# physical configuration address (assigned automatically)
addr = slave.config_addr # e.g. 0x1001
# alias address (configured in the EEPROM, optional)
alias = slave.alias_address
# look up by address
target = next((s for s in master.slaves if s.config_addr == 0x1001), None)
Identity access
name = slave.name # slave group name (the ESI GroupType name)
drive_name = slave.drive_name # device name (SDO 0x1008 Device Name)
vendor_id = slave.vendor_id # vendor ID
product_id = slave.product_id # product ID
revision = slave.rev_id # revision
serial = slave.serial_number # serial number
# look up by group name
target = next((s for s in master.slaves if s.name == "EL2008"), None)
Protocol access
| Protocol | Property | Description |
|---|---|---|
| PDO | slave.pdo | Process data objects; real-time IO data read/write |
| CoE | slave.coe | CANopen over EtherCAT; SDO object dictionary access |
| CiA 402 | slave.cia402 / CiA402Advanced | Servo drive protocol; state machine management and enable control |
| MDP | slave.mdp | Modular device protocol; module detection and automatic configuration |
| SoE | slave.soe | Servo over EtherCAT; servo drive parameters |
| FoE | slave.foe | File over EtherCAT; file transfer |
| EoE | slave.eoe | Ethernet over EtherCAT; Ethernet tunnel |
| AoE | slave.aoe | ADS over EtherCAT; the Beckhoff ADS protocol |
| VoE | slave.voe | Vendor over EtherCAT; vendor-specific protocol |
| FSoE | slave.fsoe | Functional Safety over EtherCAT; functional safety protocol |
Status and error codes
slave.state only gives the base state (the low 4 bits of AL Status) and drops the Error bit: when a slave reports 0x14 (SafeOp+Error) or 0x18 (OP+Error) it shows up as SAFE_OP / OP, which looks healthy. That is the long-standing semantics of the old entry point (a large number of call sites compare slave.state == EcState.OP, so the return type cannot change). To judge "is it faulting", use these entries:
| Property | Returns | Description |
|---|---|---|
slave.state | EcState | Base state (low 4 bits), without the Error bit |
slave.state_raw | int | Raw AL Status byte (register 0x0130) 0-255, bit4 (Error) preserved; returns the 0 sentinel when the read fails |
slave.has_error | bool | state_raw & 0x10 — whether the slave is currently faulted |
slave.status | EcState | Full status view (same semantics as C# (EcState)raw): can carry 0x14 / 0x18; status.base_state takes the low 4 bits, status.format() produces SafeOp+Error / OP+Error |
slave.al_status_code | EcALState | Enum view: in-table codes return the named member; out-of-table codes return an instance that carries the raw code (int(...) gives it), no longer folded into UNKNOWN |
slave.al_status_code_raw | int | Lossless raw code 0-65535; returns 0xFFFF when the read fails (the EcALState.UNKNOWN sentinel, not a code the device reported) |
if slave.has_error:
print(f"从站 {slave.slave_num} 报错: {slave.status.format()}") # e.g. "OP+Error"
print(f" AL=0x{slave.al_status_code_raw:04X} ({slave.al_status_code.describe()})")
# Codes outside the table keep their raw value too
code = slave.al_status_code
print(f"int={int(code):#06x}, 名字={code.name}") # e.g. 0x0038 -> AL_CODE_0x0038
- Only need the "base state":
slave.state(existing call sites stay unchanged). - Judge whether the slave is faulting:
slave.has_error(more reliable thanstate, which does not mistake0x18for a healthy OP). - Raw value for logs / reporting:
slave.state_raw/slave.al_status_code_raw. - A readable state that carries the Error bit:
slave.status.format(). - Composite values such as
EcState(0x14)/EcState(0x18)can be constructed directly (noValueError); unnamed composite values are named likeSTATE_0x1A, and out-of-table codes such asEcALState(0x0038)are named likeAL_CODE_0x0038.EcALState.UNKNOWN(0xFFFF) only means "not read / not sampled".
PDO data read/write
After entering OP, map fixed-width packed structures onto the process image and change fields directly from multiple threads:
import ctypes
class InPdo(ctypes.Structure):
_pack_ = 1
_fields_ = [
("status_word", ctypes.c_uint16),
("actual_position", ctypes.c_int32),
]
class OutPdo(ctypes.Structure):
_pack_ = 1
_fields_ = [
("control_word", ctypes.c_uint16),
("target_position", ctypes.c_int32),
]
inputs = slave.pdo.inputs_mapping(InPdo)
outputs = slave.pdo.outputs_mapping(OutPdo)
outputs.control_word = 0x000F
print(f"当前位置: {inputs.actual_position}") # Current position: {inputs.actual_position}
EMCY emergency
An EtherCAT slave sends an EMCY (Emergency) message when it detects an exception. Each slave exposes it through the slave.emcy_history / slave.emcy_count properties and the slave.clear_emcy() method.
# get the number of emergency messages
count = slave.emcy_count
# get the emergency message history (List[EmergencyMessage], in chronological order, oldest → newest)
history = slave.emcy_history
for msg in history:
print(f"错误码: 0x{msg.error_code:04X}, 寄存器: 0x{msg.error_register:02X}") # Error code: 0x{msg.error_code:04X}, register: 0x{msg.error_register:02X}
# clear the emergency message history
slave.clear_emcy()
slave.coe.get_emergency_history()EMCY history is a capability of the Slave object (slave.emcy_history / slave.clear_emcy()). The CoE instance (slave.coe) has no get_emergency_history() / clear_emergency_history() methods; the single authority for the history is the ring buffer on the slave side (at most 256 entries per slave), and the Python side keeps no second copy of the history.
Topology queries
Get the list of child slaves through the slave's children property to traverse the topology tree.
# get the child slaves of a slave
children = slave.children
# traverse the topology tree
def print_tree(s, depth):
indent = " " * depth
print(f"{indent}[{s.slave_num}] {s.name}")
for child in s.children:
print_tree(child, depth + 1)
for s in master.slaves:
if s.topology == 1 or s.parent_station == 0:
print_tree(s, 0)