Hot-plug
Hot-plug means unplugging, replacing and plugging a module back in while the system is running. First separate the two kinds of use: their configuration and "who handles it" are completely different.
| Mode | Typical scenario | What gets swapped | Does the master need reconfiguration |
|---|---|---|---|
| Fixed slot · Spare replacement | Regular motors, remote IO modules; fixed position and fixed order | Same-model spares | No. Plug it back into the original slot or the chain end and the master restores it automatically from the original configuration |
| Tool module change | Robot grippers, various tool modules; swapped at any time | Possibly a different model, different PDO layout | Yes. Declare the tool module in the DENI first; the application layer picks the matching tool module from the event and configures it |
Both of these are called hot-plug, but the responsibility differs. A fixed slot only ever takes a same-model spare, and when the module goes back into the original slot or the chain end the bus layer can handle it entirely on its own; a tool module change swaps the model and the purpose, and because the master does not know how the new tool module is meant to be used, the application layer has to be involved.
| Layer | What the master does automatically | What the application layer must do |
|---|---|---|
| Bus layer | A dropped slave does not stop the bus or lower the expectation, and the reason is reported truthfully; on re-insertion it is discovered automatically, its identity is checked, and it is brought back to OP on the declared configuration | No involvement needed |
| Application layer | Reports the identity expected by the configuration together with the identity actually read | Determine which tool module was swapped in, choose the PDO layout and process parameters, and decide when to allow operation to continue |
If it is all automatic, why does the application still have to take part? Because the master only recognizes identity and declarations, not process requirements. It can make "who is in this slot right now" automatic and truthful; as for "how this tool module should be used", only your program knows. Hot-plug events are the handoff point between the two: as soon as a slot changes, the master tells you the expected identity and the measured identity together, and you make the choice and the configuration in the application layer.
Properties
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| Binding mode | master.HotplugBindMode | HotplugBindMode | Read/write | How a module on site maps to a declared slot in the DENI. Default ByIdentity; only a value set before Build affects this chain build, while a value set afterwards affects only the next re-insertion or rebuild |
| Absence policy | slave.IsOptional | bool | Read/write | This slot may be absent. In a DENI it is written <deni:Optional>true</deni:Optional>; in code, set it before SetState(OP) |
| Recovery observability | master.Diagnostics.HotSwapRescanCount | int | Read-only | Cumulative completed rounds of automatic rebuild. Use it to confirm that the master really tried |
| Recovery observability | master.Diagnostics.LastHotSwapRescanResult | int | Read-only | Result code of the last automatic rebuild. 0 = success or nothing has happened yet, a negative value = the specific failure reason |
Both binding modes identify a module by its identity; neither hard-binds it by slot number:
ByIdentity(1, the default): alias first (the ESC alias in the module's EEPROM), then Vendor + Product matching; wiring order does not matter. Use it when tool modules may change position. When you have several modules of the same model, burn a different alias into each one so that the assignment stays unambiguous when one is absent.ByPosition(0): the declared position carries engineering meaning — each declared slot is one physical workstation. Identification is still alias + Vendor + Product; additionally, the relative physical order of the modules present must match the declared order. Missing slaves are allowed, and later modules do not shift forward. Use it for fixed wiring that only swaps same-model spares: a wiring order that does not match the declared order is called out; same-model modules cannot be told apart from each other, so that has to rely on alias.
Declare an optional slot in the DENI and make the alias match what is burned into the module's EEPROM:
<Slave>
<Info>...</Info>
<deni:SlaveConfig>
<deni:Position>3</deni:Position>
<deni:AliasAddress>0x0103</deni:AliasAddress>
<deni:Optional>true</deni:Optional>
</deni:SlaveConfig>
</Slave>
The binding mode must be set before Build():
var ecat = new DarraEtherCAT().SetENI(@"C:\EtherCAT\line.deni");
// Fixed wiring that only swaps same-model spares: have the master additionally verify the workstation order
ecat.HotplugBindMode = HotplugBindMode.ByPosition;
var master = ecat.Build().Master;
A change takes effect on the next scan, configuration load or re-plug rebuild, and does not rewrite bindings that are already established. Passing a value outside the enum raises a parameter exception on the spot and leaves the current value unchanged; it never quietly runs in the default mode.
Methods
AcknowledgeSlaveReplacement(int slaveNum)
When the identity does not match, the master refuses to let the module through: it does not guess "can the module you swapped in be used", but holds that station outside OP, reports SlaveIdentityMismatch, and then retries on its own backoff schedule. During the backoff it does not read the identity every cycle, and it never applies the old configuration to the new module.
AcknowledgeSlaveReplacement is exactly this signal: "a person has finished handling it, check again right away". After the call the master clears the backoff for that station and re-reads the identity in the very next scan cycle:
- It matches → the module recovers automatically and
SlaveOnlineis raised - Still no match →
SlaveIdentityMismatchis reported again and the backoff continues
It does not change the expected identity in the configuration, and it never forces a module of a different model through. It only makes the master retry earlier; it does not make the "can this module be used" decision for you.
The reason it is exposed is also here: only the site or the application knows when the swap is finished and whether the correct spare was fitted; the SDK cannot make that judgment for you, and it should not make you restart the whole bus just to retry one slot immediately.
public bool AcknowledgeSlaveReplacement(int slaveNum)
Parameters:
slaveNum(int) — slave index (1-based, matches the configuration)
Returns:
bool— returnstrueif the request was accepted and the check restarted immediately;falseif the parameter is invalid, or if that station is not currently in the lost state (there is no replacement waiting to be confirmed)
Calling it on a station that has already recovered, or that never dropped out, returns false.
Example:
if (master.AcknowledgeSlaveReplacement(3))
Console.WriteLine("已让主站重新核对从站 3"); // Told the master to re-verify slave 3
else
Console.WriteLine("从站 3 当前没有待确认的替换,或编号无效"); // Slave 3 currently has no pending replacement to acknowledge, or the index is invalid
Events
Hot-plug events all live on master.Events. The three events have different roles:
SlaveIdentityMismatch— the module in this slot does not match the identity expected by the configuration. The payload carries the expected and measured Vendor / Product / Revision; whether the model changed is judged from Vendor and Product.SlaveOffline— the module no longer answers.SlaveOnline— the module answers again. It only means the station was rediscovered, not that it has reached OP; whether it is usable still depends onslave.Stateandslave.IsLost.
For the full event list see Events.
public event EventHandler<SlaveIdentityMismatchEventArgs>? SlaveIdentityMismatch;
public event SlaveOfflineEventHandler? SlaveOffline;
public event SlaveOnlineEventHandler? SlaveOnline;
master.Events.SlaveIdentityMismatch += (sender, e) =>
{
// Getting here means Vendor or Product does not match the configuration: the model changed, or it was plugged into the wrong position
Console.WriteLine($"从站 {e.SlaveIndex} 实测 0x{e.ActualVendor:X8}/0x{e.ActualProduct:X8}," // Slave {e.SlaveIndex} measured 0x{e.ActualVendor:X8}/0x{e.ActualProduct:X8},
+ $"配置期望 0x{e.ExpectedVendor:X8}/0x{e.ExpectedProduct:X8}"); // configuration expects 0x{e.ExpectedVendor:X8}/0x{e.ExpectedProduct:X8}
};
master.Events.SlaveOffline += slaveIndex =>
Console.WriteLine($"从站 {slaveIndex} 离线"); // Slave {slaveIndex} offline
master.Events.SlaveOnline += slaveIndex =>
Console.WriteLine($"从站 {slaveIndex} 重新应答(是否到 OP 看 slave.State)"); // Slave {slaveIndex} responded again (whether it reached OP: see slave.State)
SlaveIdentityMismatch
Raised when a module does not match. The criterion is that Vendor or Product differs from the configuration (binding by alias checks the alias too). Revision never causes rejection: different revisions of the same model (spares, firmware upgrades) are treated as the same model, and the master only refreshes the record; this is also why a same-model spare plugged back into the original slot or the chain end recovers automatically.
| Property | Type | Description |
|---|---|---|
MasterIndex | ushort | Master index |
SlaveIndex | ushort | Slave index (1-based) |
ExpectedVendor / ExpectedProduct / ExpectedRevision | uint | The identity expected by the configuration |
ActualVendor / ActualProduct / ActualRevision | uint | The identity actually read now |
TimestampUtc | DateTime | Event time (UTC) |
Revision is used only for the record and for troubleshooting, never as grounds for rejection; Vendor and Product are the hard criterion. If the Vendor/Product in the event look identical and the master still reports an identity mismatch, check whether that station's alias matches its DENI declaration (alias binding counts it into the criterion).
The master retries that station on its backoff schedule and does not repeat the event every cycle. Once the identity is corrected, the next retry (or an immediate retry via AcknowledgeSlaveReplacement) recovers it; if it is still not corrected, SlaveIdentityMismatch is reported again.
SlaveOffline
Raised when a module stops answering. The delegate carries only a slave index: SlaveOfflineEventHandler(ushort slaveIndex).
An optional slot that is declared in the DENI but has never been plugged in since the chain was built may also receive an offline notification reported at the declared position. This is not slave loss; it means "this slot never came online" and is handled as an allowed absence. To tell the causes apart, subscribe to SlaveOfflineDetailed; its payload carries Reason and the evidence at the time.
SlaveOnline
Raised when a module answers again. The delegate likewise carries only a slave index: SlaveOnlineEventHandler(ushort slaveIndex).
It only means "this station is visible again"; it does not mean it has reached OP. Only after the master has configured the module automatically and promoted it to OP does slave.State become OP; before writing outputs, confirm both that slave.IsLost == false and that the state is a clean OP.
What happens after plugging in
After a module is plugged back in, the master handles it in the following order:
- Detect — the module is seen answering again in the cyclic communication.
- Find the slot — under the current binding mode, the corresponding slot is found in the configuration.
- Check the identity — see whether the module plugged back in is the declared one: Vendor and Product must match. A Revision difference is recorded only and is not grounds for rejection (same-model spares and firmware upgrades are both allowed). When there are several modules of the same model they are told apart by alias, so one is never mistaken for another.
- Configure automatically — once it is confirmed, the master pushes the module up to OP on that slot's declaration.
- Restore the data — PDOs take effect with the declared layout, inputs and outputs refresh as usual, and the other slaves are unaffected.
Automatic promotion is not unconditional. Only two cases are handled automatically: a module plugged in at the end of the chain, or re-inserted into its original empty slot. The master checks about once per second, and a module already being promoted is not triggered again. Automatic rebuild is the master's default behavior and has no separate switch; what you have to do is declare the slots and identities clearly in the DENI.
If a module is plugged in mid-chain and shifts the order of the modules behind it, the master does not handle it automatically: it does not rewrite the positions of other modules; it stops and prompts you to rescan the bus. This is deliberate — rewriting positions would misconfigure other stations.
Automatic promotion covers only chain-end insertion and empty-slot re-insertion, and expected_wkc does not change because of automatic promotion.
| Situation | Master behavior | What the application layer must do |
|---|---|---|
| Declared, matches the declaration, at the chain end or re-inserted into an empty slot | Configured automatically and brought up to OP | Nothing to do |
| Declared, same model (different Revision), plugged back into the original slot or the chain end | Accepted as the same model, the record is refreshed, and it is promoted normally | Nothing to do; Revision is recorded only and is not grounds for rejection |
| Declared, replaced with a different model (tool module) | Does not apply the old slot's configuration to the new module; if the DENI also declares this tool module and the identity matches, it is configured on that declaration; if it is not found, an identity mismatch or an extra device is reported and it is not promoted | Select the configuration from the tool module catalog; if the DENI does not declare this tool module, add the declaration first, then rebuild |
| Declared, matches the declaration, but inserted mid-chain and shifts the later modules | Not promoted; prompts you to rescan the bus | Stop and rescan the bus, or rebuild |
| Device not in the configuration | Not promoted; reported truthfully as an extra device | Handle it manually, or add the device to the DENI |
| Optional slot absent | Not an error; the other slaves keep running as usual | Nothing to do |
| Mandatory slot absent | OP is not reached; the absence is reported truthfully | Check power, wiring, and configuration |
When troubleshooting, check three things first: slave.IsLost (whether the dropout is confirmed), slave.State (the current state) and slave.ErrorCode (the full AL Status Code). For an identity mismatch, compare the expected and measured values in SlaveIdentityMismatch; if a module reaches OP and then drops again, or never reaches OP, look at the AL code and the startup parameter (SDO) errors. The AL code table is in Diagnostics.
If HotSwapRescanCount never grows, the master has not started an automatic rebuild: it may not be in a stable OP state, the number of physical stations may not exceed the number of registered stations, or a mid-chain insertion may have been blocked by the order gate. LastHotSwapRescanResult gives the result code of the last attempt, and a negative value means the specific failure reason.
Automatic promotion happens only for modules that are declared in the configuration and whose identity check passes. Extra devices that are not declared, and modules that do not match, are never promoted automatically: the master does not pull them into OP just because they were plugged in, and it does not add them to the control outputs by itself. Plugging an extra device into a production line does not make it a station that can be written to.
Failures are reported truthfully; a failure is never reported as success. The station stays un-promoted and the reason is given (identity mismatch / unknown device / missing configuration / promotion failure), and recovery keeps retrying. actual_wkc reflects the missing station truthfully, and expected_wkc is never lowered to make the numbers match.
Tool module change: how the application layer picks and configures
The crux of a tool module change is not the unplugging and the plugging in, but the choice and the configuration the application layer has to make after the module is plugged in. The three steps below are the recommended way.
Step one: declare every tool module you might use in the DENI. Each tool module type takes one declared slot with its own Vendor / Product / PDO layout written out and marked optional (<deni:Optional>true</deni:Optional>). If the workstation is fixed, write a fixed Position; if tool modules may change position, use ByIdentity and burn a unique alias (other than 0) into every tool module. For a tool module that is not declared in the DENI, the master will not guess how to configure it — it only reports "identity mismatch" or "extra device" truthfully.
<!-- Tool module station: both tool modules are declared as optional slots, each with its own PDO layout -->
<Slave>
<Info>
<VendorId>#x00000002</VendorId>
<ProductCode>#x09224052</ProductCode>
</Info>
<deni:SlaveConfig>
<deni:Position>10</deni:Position>
<deni:AliasAddress>0x0110</deni:AliasAddress>
<deni:Optional>true</deni:Optional>
</deni:SlaveConfig>
</Slave>
<Slave>
<Info>
<VendorId>#x00000002</VendorId>
<ProductCode>#x09224053</ProductCode>
</Info>
<deni:SlaveConfig>
<deni:Position>11</deni:Position>
<deni:AliasAddress>0x0111</deni:AliasAddress>
<deni:Optional>true</deni:Optional>
</deni:SlaveConfig>
</Slave>
Step two: use the events to decide whether this is a spare or a model change. Look at the expected identity and the measured identity in SlaveIdentityMismatch:
- Same Vendor and Product → the same model; a Revision difference is only a different version, and when the module is plugged back into the original slot or the chain end the master restores it automatically as a spare, so the application layer does not change its configuration.
- Different Vendor or Product → the model changed; handle it as a tool module change.
AcknowledgeSlaveReplacement is only meaningful for "the correct module was swapped back in"; a module of a different model is not accepted because of it. A tool module change goes through the path of a DENI declaration plus application-layer configuration.
If the new tool module is already declared in the DENI and its identity also matches, the master configures it to OP on that declaration; in that case you usually receive SlaveOnline first. After reading the actual identity from that station, follow the same selection and configuration below.
Step three: pick the configuration from the measured identity and send it down. Take slave.VendorId / ProductId / RevId to the tool module catalog to find the matching configuration; first verify that slave.Ibytes / slave.Obytes agree with this tool module's PDO layout, then send the process parameters (SDO) and the output safe values. If any one of them does not agree, do not write outputs — hold the safe state and raise an alarm.
PDO assignment follows the DENI; at runtime, do not adapt to a new tool module by changing the PDO mapping from the application layer. For a model that is not declared in the DENI, the correct approach is to add the declaration and rebuild the chain, not to force a mapping change at runtime.
// Tool module catalog: find the PDO layout, process parameters and safe outputs by measured identity
void OnModuleChanged(DarraEtherCAT master, int slaveIndex)
{
if (master.Slaves == null) return;
var slave = master.Slaves.Find(s => s.SlaveNum == slaveIndex);
if (slave == null) return;
// Configure and write outputs only on a clean OP; offline and OP+Error both count as unusable
if (slave.IsLost || slave.State != EcState.OP) return;
var tool = toolCatalog.Find(slave.VendorId, slave.ProductId, slave.RevId);
if (tool == null)
{
Console.WriteLine($"从站 {slaveIndex} 插入了未登记工具模块,保持安全态"); // Slave {slaveIndex} has an unregistered tool module inserted, staying in safe state
return;
}
if (slave.Ibytes != tool.InputBytes || slave.Obytes != tool.OutputBytes)
{
Console.WriteLine($"工具模块 {tool.Name} 的 PDO 大小与 DENI 不一致,拒绝配置"); // Tool module {tool.Name} PDO size does not match DENI, configuration rejected
return;
}
if (slave.CoE != null)
{
foreach (var p in tool.Parameters)
{
if (!slave.CoE.SDOWrite(p.Index, p.SubIndex, p.Data, out var detail, false))
Console.WriteLine($"参数 0x{p.Index:X4}:{p.SubIndex:X2} 写入失败:{detail}"); // Parameter 0x{p.Index:X4}:{p.SubIndex:X2} write failed: {detail}
}
}
if (!slave.PDO.TryWriteOutputBatch(new[] { new PdoOutputPatch(0, tool.SafeOutputs) }))
Console.WriteLine($"工具模块 {tool.Name} 的安全输出写入失败"); // Failed to write the safety output of tool module {tool.Name}
}
Complete example
using System;
using DarraEtherCAT_Master;
var ecat = new DarraEtherCAT().SetENI(@"C:\EtherCAT\line.deni");
// Fixed slot + spare replacement: position and order are fixed, use ByPosition
// It must be set before Build: Build binds the slots declared in the DENI to the measured slaves according to it.
ecat.HotplugBindMode = HotplugBindMode.ByPosition;
var result = ecat.Build();
if (!result.Success)
{
Console.WriteLine($"初始化失败:{result.Message}"); // Initialization failed: {result.Message}
return;
}
var master = result.Master;
if (master == null) return;
master.Events.SlaveIdentityMismatch += (sender, e) =>
{
// Vendor or Product does not match: the model changed, or it was plugged into the wrong position.
// A Revision difference on the same model never reaches here; the master restores it automatically as a spare.
Console.WriteLine($"从站 {e.SlaveIndex} 实测 0x{e.ActualVendor:X8}/0x{e.ActualProduct:X8}," // Slave {e.SlaveIndex} measured 0x{e.ActualVendor:X8}/0x{e.ActualProduct:X8},
+ $"配置期望 0x{e.ExpectedVendor:X8}/0x{e.ExpectedProduct:X8}"); // configuration expects 0x{e.ExpectedVendor:X8}/0x{e.ExpectedProduct:X8}
};
master.Events.SlaveOffline += slaveIndex =>
Console.WriteLine($"从站 {slaveIndex} 离线"); // Slave {slaveIndex} offline
master.Events.SlaveOnline += slaveIndex =>
Console.WriteLine($"从站 {slaveIndex} 重新应答(到 OP 后再配置)"); // Slave {slaveIndex} responded again (configure after it reaches OP)
if (!master.SetState(EcState.OP, out var error))
Console.WriteLine($"切换 OP 失败:{error}"); // Failed to switch to OP: {error}
// After the operator swaps the correct module back in, have the master re-check this station:
// master.AcknowledgeSlaveReplacement(3);