DC configuration
Master-level DC configuration. Call it directly on master.
Properties
| Category | Property | Type | R/W | Description |
|---|---|---|---|---|
| DC | has_dc | Optional[bool] | read-only | Whether the network contains a slave that supports DC: True / False are definite answers, a failed read returns None (unknown) |
| master_dc_time | int | read-only | DC system time retrieved in the most recent cycle (ns, epoch 2000-01-01 UTC); 0 when DC is not configured | |
| reference_clock_slave_index | int | read-only | Index of the current reference clock slave (1-based); 0 means the network has no DC slave | |
| max_propagation_delay | int | read-only | Largest propagation delay among all DC slaves (ns) | |
| sync_window_threshold | int | read-only | Sync window threshold (ns, default 1000); write it with set_sync_window_threshold() | |
| max_sync_difference | int | read-only | Largest synchronization deviation across all DC-active slaves (ns); -1 = unreadable (master not ready / stale snapshot) | |
| all_slaves_in_sync | bool | read-only | Whether every slave with SYNC0 armed is inside the sync window (fail-closed); False when there are no participants | |
| dc_armed_slave_count | int | read-only | Number of slaves with SYNC0 armed (taken from the dc_diagnostics 2-second snapshot) | |
| dc_active_slave_count | int | read-only | Number of DC-active slaves (the landed 0x0980 value is non-zero, taken from the dc_diagnostics 2-second snapshot) | |
| dc_diagnostics | DcDiagnosticsSnapshot | read-only | DC diagnostics 2-second snapshot (per-slave + master-level); see Master diagnostics |
has_dc has three statesTrue = the network has DC-capable slaves; False = it definitely has none; None = the read failed (unknown). if master.has_dc: treats unknown as "no DC" and skips configuration; when the distinction matters, test is True / is False / is None explicitly.
master_dc_time
@property
def master_dc_time(self) -> int
The 64-bit DC system time retrieved by the most recent FRMW (Read-Multiple-Write).
- Unit: nanoseconds
- Epoch: 2000-01-01 00:00:00 UTC (EtherCAT DC standard)
- Cases that return 0: DC is not active / DC configuration has not completed
master_dc_time is refreshed automatically by the FRMW sub-frame in the master cycle frame; the sampling cycle equals loop_cycle.
reference_clock_slave_index
@property
def reference_clock_slave_index(self) -> int
Reference clock slave index (1-based). The master selects it when configuring DC: the first slave in the network topology with active DC (real SYNC0) (skipping couplers that are only DC-capable but run in FreeRun); when no slave has active DC, it falls back to the first DC-capable slave in the chain.
Return value:
int— slave index (1-based)0— no DC slave
Example — query the DC time and the reference clock:
t_ns = master.master_dc_time
ref = master.reference_clock_slave_index
if t_ns > 0:
seconds_since_2000 = t_ns / 1_000_000_000
print(f"DC 时间: {t_ns} ns ({seconds_since_2000:.6f} s since 2000-01-01)") # DC time: {t_ns} ns ({seconds_since_2000:.6f} s since 2000-01-01)
print(f"参考从站: #{ref}") # Reference slave: #{ref}
else:
print("DC 未激活") # DC not active
from datetime import datetime, timezone, timedelta
dc_epoch = datetime(2000, 1, 1, tzinfo=timezone.utc)
t_ns = master.master_dc_time
if t_ns > 0:
dc_dt = dc_epoch + timedelta(microseconds=t_ns // 1000)
print(f"DC 对应 UTC 时间: {dc_dt.isoformat()}") # DC as UTC time: {dc_dt.isoformat()}
Methods
configure_dc()
def configure_dc(self, sync0_ns: int, sync1_ns: int = 0) -> int
Configure DC synchronization for all DC slaves.
By default it does the following automatically:
- Propagation delay measurement — read the receive time of every port and compute the delays
- Phase calculation — resolve each slave's phase from the explicit ESI value / explicit user value / default 70% (phase only; the propagation delay is corrected in hardware by the ESC at
0x0928and is not folded into the phase) - DC application — write the SYNC0/SYNC1 cycle and start time to each slave
Parameters:
sync0_ns(int) — SYNC0 cycle (nanoseconds); 0 disables DCsync1_ns(int) — SYNC1 delta time (nanoseconds); 0 means SYNC0 only
Return value:
int— number of DC slaves that were landed and accepted the configuration (armed plus PRE-OP ready, never equivalent to the armed count — the real proof of arming is the re-arm before SAFE-OP / OP and a real read-back, not this return value);0= no slave accepted the configuration, negative = failure
Example:
master.configure_dc(1_000_000) # SYNC0 = 1ms
master.configure_dc(1_000_000, 500_000) # SYNC0 = 1ms, SYNC1 = 500 µs
125 µs high-performance configuration (recommended for the real-time driver):
master.config.loop_cycle = 125_000 # PDO exchange cycle 125 µs
master.configure_dc(125_000) # SYNC0 = 125 µs
1ms standard configuration (general use):
master.config.loop_cycle = 1_000_000 # PDO exchange cycle 1ms
master.configure_dc(1_000_000) # SYNC0 = 1ms
- SYNC0 and loop_cycle — keep them equal or make SYNC0 an integer multiple of
loop_cycle; synchronizing data that has not arrived is meaningless - Automatic phase calculation — the master resolves the phase in priority order through
auto_calculate_dc_shift();update_propagation_delays()measures the propagation delay separately and writes it to0x0928 - Oversampling exception — on oversampling devices SYNC0 < loop_cycle, which needs dedicated support in the device hardware and the PDO mapping
SYNC1 is a delta time relative to SYNC0. For example SYNC0=1,000,000ns and SYNC1=500,000ns mean SYNC1 triggers 500 µs after each SYNC0.
The DC synchronization mode is configured explicitly by the user. After restoring a snapshot, slaves that need DC should be confirmed to have entered DcSynchron / DcSynchron01.
master.configure_dc()— configures all DC slaves in one callDcConfig.configure()— configures a single slave
enable_continuous_measurement()
def enable_continuous_measurement(self, enable: bool = True, interval_sec: int = 10) -> None
Enable or disable continuous propagation delay measurement (ETG.1500 5.13.2).
Example:
master.enable_continuous_measurement(True, 60) # re-measure every 60 seconds
Sync window monitoring
# threshold: default 1000ns
master.diagnostics_info.sync_window_threshold = 500 # set it to 500ns
# events
def on_sync_lost(mi, si, diff_ns):
print(f"从站 {si} DC 同步丢失: 偏差 {diff_ns}ns") # Slave {si} DC sync lost: deviation {diff_ns}ns
master.set_dc_sync_lost_callback(on_sync_lost)
Master-level equivalent: master.set_sync_window_threshold(500) sets the threshold and the read-only property master.sync_window_threshold reads the current value (default 1000ns). At the lower layer an assignment of <= 0 falls back to the default value and logs a WARNING; it is never stored as 0.
max_sync_difference
@property
def max_sync_difference(self) -> int
Largest time deviation among all DC slaves (nanoseconds).
This entry returns
int(an underlying summary) and does not distinguish "read failed" from "no DC-active slave"; a failed read (master not ready / stale snapshot) returns-1. The SDKs of other languages also have anint?three-state entry; this language has no equivalent three-state form.
Example:
print(f"最大同步偏差: {master.max_sync_difference}ns") # Maximum sync deviation: {master.max_sync_difference}ns
all_slaves_in_sync
@property
def all_slaves_in_sync(self) -> bool
Whether every slave with SYNC0 DC enabled is inside the sync window (deviation <= sync_window_threshold). A network where all slaves run FreeRun is not "DC synchronized".
fail-closed: with zero DC-active participants (no DC slave / nothing armed / reference clock only) it returns False; the SDK does not substitute True. To distinguish "no participants" from "participants all inside the window", read the three-state face master.dc_diagnostics.all_in_sync (None = no confirmed participant).
Example:
if not master.all_slaves_in_sync:
print("存在从站同步偏差过大") # Some slaves have excessive sync deviation
reset_all_sync_window_stats()
def reset_all_sync_window_stats(self) -> None
Reset the sync window statistics of every slave (maximum/minimum time difference, out-of-sync count and so on).
Example:
master.reset_all_sync_window_stats()
For the detailed sync window state of a single slave, use the slave.sync_window_status property (returns a dict containing diff_ns, max_diff_ns, min_diff_ns, in_sync and out_of_sync_count), or slave.dc_config.get_sync_window_status() (returns a SyncWindowStatus object).
DC drift compensation
enable_drift_compensation()
def enable_drift_compensation(self, enable: bool = True,
threshold_ns: int = 1000, gain: int = 1) -> None
Enable or disable DC drift compensation. Drift compensation continuously corrects the deviation between the system clock and the DC reference clock on the master side, preventing synchronization accuracy from degrading over long runs.
Parameters:
enable(bool) —Trueenables,Falsedisablesthreshold_ns(int) — deviation threshold that triggers compensation (nanoseconds); no correction is applied below this value, default 1000nsgain(int) — compensation gain (1–1024) that controls the correction rate (a larger value corrects faster but may introduce oscillation), default 1
Example:
# enable drift compensation with the default parameters
master.enable_drift_compensation(True)
# custom parameters: 500ns threshold, gain 256 (1–1024, corrects faster but watch for oscillation)
master.enable_drift_compensation(True, threshold_ns=500, gain=256)
# disable drift compensation
master.enable_drift_compensation(False)
For systems that run for a long time (>24 hours), enable drift compensation. The default parameters suit most scenarios; adjust the threshold and gain only when finer control is needed.
Full example
125 µs high performance (real-time driver)
with EtherCATMaster() as master:
master.set_eni(r"C:\config.deni")
master.set_network(r"\Device\NPF_{GUID}")
# configure the cycle
master.config.loop_cycle = 125_000
# switch to SAFE-OP
master.set_state(EcState.SAFE_OP)
# configure DC
if master.has_dc is True: # None = read failed, treat as unknown
master.configure_dc(125_000)
import time
time.sleep(1)
# switch to OP and start the PDO cycle
master.set_state(EcState.OP)
master.start()
# listen for DC sync lost events
def on_sync_lost(mi, si, diff_ns):
print(f"从站 {si} DC 同步丢失: 偏差 {diff_ns}ns") # Slave {si} DC sync lost: deviation {diff_ns}ns
master.set_dc_sync_lost_callback(on_sync_lost)
Continuous measurement and sync monitoring
# long-running system: enable continuous propagation delay measurement (temperature drift compensation)
master.enable_continuous_measurement(True, 60)
# sync window monitoring
if not master.all_slaves_in_sync:
print(f"同步偏差过大: {master.max_sync_difference}ns") # Sync deviation too large: {master.max_sync_difference}ns
# reset the statistics
master.reset_all_sync_window_stats()
The SM synchronization type is configured automatically through the slave-level slave.dc_config.configure_mode(mode, ...) method; no manual call is needed.