Skip to main content

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​

ProtocolPropertyDescription
PDOslave.pdoProcess data objects; real-time IO data read/write
CoEslave.coeCANopen over EtherCAT; SDO object dictionary access
CiA 402slave.cia402 / CiA402AdvancedServo drive protocol; state machine management and enable control
MDPslave.mdpModular device protocol; module detection and automatic configuration
SoEslave.soeServo over EtherCAT; servo drive parameters
FoEslave.foeFile over EtherCAT; file transfer
EoEslave.eoeEthernet over EtherCAT; Ethernet tunnel
AoEslave.aoeADS over EtherCAT; the Beckhoff ADS protocol
VoEslave.voeVendor over EtherCAT; vendor-specific protocol
FSoEslave.fsoeFunctional 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:

PropertyReturnsDescription
slave.stateEcStateBase state (low 4 bits), without the Error bit
slave.state_rawintRaw AL Status byte (register 0x0130) 0-255, bit4 (Error) preserved; returns the 0 sentinel when the read fails
slave.has_errorboolstate_raw & 0x10 — whether the slave is currently faulted
slave.statusEcStateFull 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_codeEcALStateEnum 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_rawintLossless 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
Which entry to use
  • Only need the "base state": slave.state (existing call sites stay unchanged).
  • Judge whether the slave is faulting: slave.has_error (more reliable than state, which does not mistake 0x18 for 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 (no ValueError); unnamed composite values are named like STATE_0x1A, and out-of-table codes such as EcALState(0x0038) are named like AL_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()
Do not use 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)