DC configuration
Master-level DC configuration. Call it directly on master.
Properties
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| DC state | HasDC | bool | Read-only | Whether the network contains any slave that supports DC |
Methods
ConfigureDC(uint sync0CycleNs, uint sync1CycleNs = 0, int? shiftNs = null)
public bool ConfigureDC(uint sync0CycleNs, uint sync1CycleNs = 0, int? shiftNs = null)
Configure DC synchronization for all DC slaves. All parameter units are nanoseconds (ns). Propagation delay measurement, phase resolution and DC parameter writes are done automatically.
Parameters:
sync0CycleNs(uint) — SYNC0 cycle (ns); 0 disables DC (removes the SYNC0 arming)sync1CycleNs(uint) — SYNC1 delta time (ns); 0 means SYNC0 onlyshiftNs(int?) — phase shift (ns):null= phase not specified, resolved by precedence ESI explicit > user explicit > default 70%; both0and negative values are an explicit user phase and take effect as given (0is the literal 0 phase and does not fall back to 70%); all slaves use the same value. The only "not specified" sentinel at the lower layer isint.MinValue
Return value:
bool—truewhen at least one DC slave actually landed and was accepted (armed plus PreOp-ready, not equivalent to armed);falsewhen there is no DC slave or the configuration failed
Example:
master.ConfigureDC(1_000_000); // SYNC0 = 1ms, phase resolved by default
master.ConfigureDC(1_000_000, shiftNs: 0); // SYNC0 = 1ms, phase 0 ns (omit shiftNs for the default 70%)
master.ConfigureDC(1_000_000, 500_000); // SYNC0 = 1ms, SYNC1 = 500 µs
125 µs high-performance configuration (recommended for the real-time runtime):
master.Config.LoopCycle = 125_000; // PDO exchange cycle 125 µs
master.ConfigureDC(125_000); // SYNC0 = 125 µs, phase resolved by default
1ms standard configuration (general use):
master.Config.LoopCycle = 1_000_000; // PDO exchange cycle 1ms
master.ConfigureDC(1_000_000); // SYNC0 = 1ms, phase resolved by default
- SYNC0 and LoopCycle — keep them identical or make one an integer multiple of the other; synchronizing to data that has not arrived is meaningless
- Automatic phase (recommended) — omit shiftNs / pass null and the master resolves the phase in the order ESI explicit > user explicit > 70%; propagation delay is measured as usual and written to
0x0928, corrected in ESC hardware - Manual offset — you can also set a higher fixed value, for example 0.8 × sync0CycleNs
- Oversampling exception — on oversampling devices SYNC0 < LoopCycle, which needs dedicated support in the device hardware and the PDO mapping
- Default phase = 1 cycle + 70%:
sync0CycleNs × 70 / 100is used only when the phase is "not specified" (1 ms → 700 µs). The resolution order is ESIShiftTimeSync0other than 0 → explicit usershiftNs(including 0 and negative values, taken as given) → the 70% default. The phase is only a phase shift and adds no software propagation delay compensation; propagation delay is corrected by the ESC in hardware per0x0928. shiftNs = 0is the literal 0 phase (SYNC0 falls on the cycle boundary) and takes effect as given; it does not fall back to 70%. For the default 70%, omitshiftNs(keep itnull); the only "not specified" sentinel at the lower layer isint.MinValue.sync0CycleNs = 0removes the SYNC0 arming (back to FreeRun); it is not a 0 ns cycle. Slaves with PDO then write0x1C32:01/0x1C33:01back to 0 to clear residue from the previous session (otherwise the rise to SafeOp may report AL=0x002D); when SM2/SM3 are not mapped, the write is deferred until after mapping.- Hot-plug recovery: re-arming rewrites
0x09A0and0x0990;0x0990must be written after0x0910is re-read (re-read the slave DC time first, then compute the start instant, and activate with0x0980right after the write), otherwise the start point is stale and the first SYNC0 never appears. The phase comes from the same source as the first arming (the same resolution result, phase only, no propagation compensation) and does not jump on re-arm; when the reference clock fails it is re-selected immediately.
SYNC1 is a delta time relative to SYNC0. For example, SYNC0=1,000,000ns and SYNC1=500,000ns mean SYNC1 fires 500 µs after each SYNC0.
DC synchronization mode is configured explicitly by the user. After restoring a snapshot, check that slaves which need DC have entered DcSynchron / DcSynchron01.
master.ConfigureDC()— configures all DC slaves in one callslave.ConfigureDC()— configures a single slave
Both resolve the phase by the same precedence when none is specified. Use the slave-level method only when individual slaves need different parameters.
Master.ConfigureDC(DcSyncMode mode, uint sync0CycleNs = 0, uint sync1CycleNs = 0, int? shiftNs = null)
public bool ConfigureDC(DcSyncMode mode, uint sync0CycleNs = 0, uint sync1CycleNs = 0, int? shiftNs = null)
Master-level overload: configures DC for all DC slaves according to the synchronization mode (ETG.1020). The synchronization type of each slave is configured automatically. For per-slave configuration use slave.ConfigureDC.
Parameters:
mode(DcSyncMode) — DC synchronization mode (FreeRun/SmSynchron/DcSynchron/DcSynchron01)sync0CycleNs(uint) — SYNC0 cycle (ns), used only in DcSynchron / DcSynchron01 modessync1CycleNs(uint) — SYNC1 cycle (ns), used only in DcSynchron01 modeshiftNs(int?) — phase shift (ns), used only in DC modes
Return value:
bool—truewhen at least one DC slave actually landed and was accepted (armed plus PreOp-ready, not equivalent to armed);falsewhen there is no DC slave or the configuration failed
Example:
master.ConfigureDC(DcSyncMode.DcSynchron, 1_000_000); // SYNC0 = 1ms on all DC slaves
master.ConfigureDC(DcSyncMode.SmSynchron); // SM synchronization mode on all DC slaves
master.ConfigureDC(DcSyncMode.FreeRun); // disable DC synchronization on all DC slaves
EnableContinuousMeasurement(bool enable, uint intervalSec = 0)
public void EnableContinuousMeasurement(bool enable, uint intervalSec = 0)
Enable or disable continuous propagation delay measurement (ETG.1500 5.13.2). When enabled, the master re-measures the propagation delay periodically, which suits cases where the delay changes (temperature drift, for example).
Parameters:
enable(bool) —trueenables,falsedisablesintervalSec(uint) — measurement interval (s); 0 uses the default interval
Example:
master.EnableContinuousMeasurement(true, 60); // re-measure every 60 seconds
master.EnableContinuousMeasurement(false); // disable
By default the DC offset is computed once during ConfigureDC(). Enable this only for long-running systems with large ambient temperature swings.
Sync window monitoring
The master checks the time deviation of every DC slave automatically once per second. When a slave's deviation exceeds the SyncWindowThreshold threshold, the DCSyncLost event fires.
// threshold: 1000ns by default, adjustable
master.Diagnostics.SyncWindowThreshold = 500; // set to 500ns
// event: fires once when a slave goes from in sync to out of sync (no repeat)
master.Events.DCSyncLost += (masterIdx, slaveIdx, diffNs) =>
Console.WriteLine($"从站 {slaveIdx} DC 同步丢失: 偏差 {diffNs}ns"); // Slave {slaveIdx} DC sync lost: deviation {diffNs}ns
GetMaxSyncDifference()
public int? GetMaxSyncDifference()
Get the maximum absolute sync difference (ns) across all DC-active slaves. Returns a tri-state value:
null— read failed / unavailable (the master is closed, not initialized, the SDK read failed, or the slave table could not be read completely enough to decide)-1— confirmed: there is no DC-active slave on the network (this is a conclusion, not an error)>= 0— measured maximum absolute sync difference (ns;0is a genuinely measured 0)
Equivalent entry points:
- Property:
master.MaxSyncDifferenceNs(int?, same implementation asGetMaxSyncDifference()) - Legacy-compatible signature:
master.GetMaxSyncDifferenceLegacy()(int, foldsnullinto-1; under that signature "read failed" and "no DC-active slave" are indistinguishable, so do not use it in new code)
Example:
int? maxDiff = master.GetMaxSyncDifference();
if (maxDiff is null)
Console.WriteLine("最大同步差: 读不到(主站未就绪 / 站表读不全)"); // Max sync diff: cannot be read (master not ready / slave table incomplete)
else if (maxDiff == -1)
Console.WriteLine("最大同步差: 无 DC-active 站"); // Max sync diff: no DC-active slaves
else
Console.WriteLine($"最大同步差: {maxDiff}ns"); // Max sync diff: {maxDiff}ns
IsAllSlavesInSync()
public bool IsAllSlavesInSync()
Check whether all slaves with SYNC0 DC enabled are inside the sync window (deviation ≤ SyncWindowThreshold). All-FreeRun is not "DC synchronized".
Return value:
bool—truewhen all SYNC0 DC slaves are inside the window; FreeRun must not be read as synchronized
Example:
if (!master.IsAllSlavesInSync())
Console.WriteLine("存在从站同步偏差过大"); // Some slaves have excessive sync deviation
ResetAllSyncWindowStats()
public void ResetAllSyncWindowStats()
Reset the sync window statistics of all slaves (maximum/minimum time difference, number of out-of-sync occurrences and so on).
Example:
master.ResetAllSyncWindowStats();
For the detailed sync window status of a single slave use slave.GetSyncWindowStatus(); it covers DiffNs, MaxDiffNs, MinDiffNs, InSync and OutOfSyncCount.
Full example
125 µs high performance (real-time runtime)
var master = new DarraEtherCAT()
.SetENI(@"C:\config.deni");
var (success, message) = master.Build();
if (!success) return;
// 1. configure the cycle — 125 µs (recommended for the real-time runtime)
master.Config.LoopCycle = 125_000;
// 2. switch to SafeOp
master.SetState(EcState.SafeOp);
// 3. configure DC (automatic offset) — keep SYNC0 aligned with LoopCycle
if (master.HasDC)
{
master.ConfigureDC(125_000);
}
Thread.Sleep(1000);
// 4. switch to Op
master.SetState(EcState.OP);
// listen for DC sync loss events
master.Events.DCSyncLost += (masterIdx, slaveIdx, diffNs) =>
Console.WriteLine($"从站 {slaveIdx} DC 同步丢失: 偏差 {diffNs}ns"); // Slave {slaveIdx} DC sync lost: deviation {diffNs}ns
Continuous measurement and sync monitoring
// long-running system: enable continuous propagation delay measurement (temperature drift compensation)
master.EnableContinuousMeasurement(true, 60);
// sync window monitoring
if (!master.IsAllSlavesInSync())
{
int? maxDiff = master.GetMaxSyncDifference();
Console.WriteLine(maxDiff switch
{
null => "同步偏差: 读不到", // sync deviation: cannot be read
-1 => "同步偏差: 无 DC-active 站", // sync deviation: no DC-active slaves
_ => $"同步偏差过大: {maxDiff}ns" // sync deviation too large: {maxDiff}ns
});
}
// reset statistics
master.ResetAllSyncWindowStats();
The SM synchronization type is configured automatically by the slave-level slave.ConfigureDC(DcSyncMode mode, ...) method; no manual call is needed.
DC drift compensation
EnableDriftCompensation(bool enable, int thresholdNs = 1000, int gain = 512)
public void EnableDriftCompensation(bool enable, int thresholdNs = 1000, int gain = 512)
Enable or disable DC drift compensation. Drift compensation continuously corrects the offset between the system clock and the DC reference clock on the master side, so synchronization accuracy does not degrade over long runs.
Parameters:
enable(bool) —trueenables,falsedisablesthresholdNs(int) — deviation threshold (ns) that triggers compensation; below it no correction is made; default 1000nsgain(int) — proportional gain (0-1024, mapping to 0.0-1.0) that controls the correction rate (a larger value corrects faster but may introduce oscillation); default 512 (0.5)
Example:
// enable drift compensation with the default parameters
master.EnableDriftCompensation(true);
// custom parameters: 500ns threshold, lower gain (smoother correction)
master.EnableDriftCompensation(true, thresholdNs: 500, gain: 256);
// disable drift compensation
master.EnableDriftCompensation(false);
For long-running systems (>24 hours), enable drift compensation. The default parameters suit most scenarios; adjust the threshold and gain only when finer control is needed.
CurrentDcSyncMode is a slave-level property, accessed through slave.CurrentDcSyncMode.
Querying the master's current DC time
On every PDO cycle the master reads the 64-bit DC system time back from the reference clock slave with FRMW (Feed-Read-Multiple-Write). The SDK exposes that timestamp to the application for:
- Log time alignment — application logs can be stamped with DC time, so they line up exactly with the timeline of slave-side events (EMCY / Diagnosis History)
- Cross-slave event correlation — when several slaves report events at once, DC time gives their absolute order
- Drift diagnosis — compare against the local wall clock or an external NTP source to watch the drift trend
- Cycle start alignment — when an application action must be triggered at a specific DC instant, use it as the base for computing how long to wait
DC time is in nanoseconds and counts from 2000-01-01 00:00:00 UTC (defined by the ETG specification). It is offset from the UNIX timestamp by 30 years, so take care when converting.
MasterDCTimeNs
public long MasterDCTimeNs { get; }
The 64-bit DC system time (ns, 2000-01-01 epoch) that the master read back from the reference clock slave with FRMW on the most recent PDO cycle.
Return value:
long— DC timestamp (ns); 0 when there is no DC slave or the master is not running
ReferenceClockSlaveIndex
public ushort ReferenceClockSlaveIndex { get; }
Index of the current reference clock slave (1-based). Chosen by the master during DC configuration: the first slave on the link with DC actually active (real SYNC0), skipping couplers that support DC but run in FreeRun; when no slave has DC active it falls back to the first DC-capable slave at the head of the chain.
Return value:
ushort— slave index (1-based); 0 means the network has no DC slave
Example
// print the current DC time and the reference clock slave
long dcNs = master.MasterDCTimeNs;
ushort refIdx = master.ReferenceClockSlaveIndex;
if (dcNs == 0 || refIdx == 0)
{
Console.WriteLine("网络中无 DC 从站"); // No DC slaves on the network
}
else
{
// convert to readable UTC time (DC epoch 2000-01-01)
var dcEpoch = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
var utc = dcEpoch.AddTicks(dcNs / 100);
Console.WriteLine(
$"当前主站 DC 时间 = {dcNs} ns ({utc:yyyy-MM-dd HH:mm:ss.fffffff} UTC), " + // Current master DC time = {dcNs} ns ({utc:yyyy-MM-dd HH:mm:ss.fffffff} UTC),
$"参考从站 = #{refIdx}"); // Reference slave = #{refIdx}
}
Log timestamp scenario:
master.Events.DCSyncLost += (masterIdx, slaveIdx, diffNs) =>
{
long now = master.MasterDCTimeNs;
Console.WriteLine(
$"[DC={now}ns] 从站 {slaveIdx} 同步丢失: 偏差 {diffNs}ns"); // [DC={now}ns] slave {slaveIdx} sync lost: deviation {diffNs}ns
};
MasterDCTimeNs is updated by the master cycle thread, so the value the application reads is an approximation, possibly within one PDO cycle of the actual CPU time. For high-precision work, combine it with master.Diagnostics.BusMaxJitterUs / BusAvgJitterUs to bound the jitter.
DC truth values and recovery observations
Mode truth and queued intent on the slave side (slave.DcModeTruth / slave.PendingDcIntent) follow the hardware read-back; the full fields, three-state semantics and example are in DC synchronization § DC mode truth. This section keeps only the master-level recovery observation entry point.
MasterRecoveryLadder.Available
public bool Available { get; }
false = recovery observation is unavailable (older runtime libraries, or the snapshot cannot be taken), not "there is no recovery"; true only means the snapshot was taken.
Read it through master.Diagnostics.MasterRecoveryLadder.Available. Whether an individual field in the snapshot really has a sample depends on the field itself: an unsampled field returns null rather than 0, and TriggerBlockedTotal is always a measured value. For the per-field definitions see Master diagnostics § Master-level recovery escalation structure; for per-slave recovery observations and events see Master diagnostics.
Example:
var ladder = master.Diagnostics.MasterRecoveryLadder;
if (!ladder.Available)
Console.WriteLine("恢复观测不可用"); // Recovery observation unavailable
else
Console.WriteLine($"触发被压制累计: {ladder.TriggerBlockedTotal}"); // Total suppressed triggers: {ladder.TriggerBlockedTotal}