Hot-plug
Hot-plug means unplugging a module, swapping it and plugging it back in while the system is running. Tell the two uses apart first, because their configuration and who handles what are completely different.
| Mode | Typical use | What gets swapped | Does the master need reconfiguring |
|---|---|---|---|
| Fixed slot · spare replacement | Routine motors, remote IO modules; fixed position, fixed order | A spare of the same model | No. Plug it back into the original slot or the chain end and the master recovers automatically with the original configuration |
| Tool module swap | Robot fixtures, all kinds of tool modules; swap 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 takes a spare of the same model, and when it is plugged back into its original slot or at the chain end the bus layer can handle it fully automatically; a tool module swap changes the model and the purpose, the master does not know how to use the new tool module, so the application layer has to take part.
| Layer | What the master does automatically | What the application layer must do |
|---|---|---|
| Bus layer | A dropped slave does not stop the bus, does not lower the expected count, and reports the reason truthfully; on re-insertion it rediscovers the module, checks the identity and pushes the module back to OP as declared | Nothing |
| Application layer | Reports the identity expected by the configuration together with the identity actually read | Decide which tool module was swapped in, choose the PDO layout and the process parameters, and decide when further motion is allowed |
If it is fully automatic, why does the application still have to take part? Because the master only understands identity and declarations, not processes. It can make "who occupies this slot now" automatic and truthful; as for "how this tool module is used", only your program knows. Hot-plug events are the handover point between the two: as soon as a slot changes, the master reports the expected identity and the measured identity together, and you make the choice and the configuration at the application layer.
Properties
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| Bind mode | SetHotplugBindMode / GetHotplugBindMode | ec_hotplug_bind_mode_t | Read/write | Which declared slot in the DENI a field module maps to. Default EC_HOTPLUG_BIND_BY_IDENTITY; a value set before the DENI is loaded affects this link-up, one set afterwards only affects the next re-insertion or rebuild |
| Absence policy | SetSlaveOptional / GetSlaveOptional | BOOL | Read/write | Whether this slot may be absent. In the DENI it is written <deni:Optional>true</deni:Optional>; in code it must be set before going to OP |
| Recovery observability | GetHotSwapRescanCount | uint32_t | Read-only | Completed rounds of automatic rebuild so far. Use it to confirm that the master really tried |
| Recovery observability | GetHotSwapRescanLastResult | 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 by identity; neither hard-binds by index:
EC_HOTPLUG_BIND_BY_IDENTITY(1, the default): match by alias (the ESC alias burned into the module EEPROM) first, then by Vendor + Product; the wiring order has no effect. Use it when a tool module may change position. When there are several modules of the same model, burn a different alias into each one so ownership stays unambiguous when one is absent.EC_HOTPLUG_BIND_BY_POSITION(0): a declared position carries engineering meaning — every declared slot is one physical workstation. Identification still goes by alias + Vendor + Product; the extra requirement is that the relative physical order of the modules present matches the declared order — it is not a positional hard-bind by index. Missing slaves are allowed, empty workstations take no part in the order check, and the modules behind them neither shift forward nor get displaced. Use it for fixed installations such as fixtures and tooling, with fixed wiring where only same-model spares are swapped: a mismatch between the wiring order and the declared order is called out; same-model swaps cannot be told apart, so alias has to do that work.
Declare an optional slot in the DENI, with the alias matching what is burned into the module EEPROM:
<Slave>
<Info>...</Info>
<deni:SlaveConfig>
<deni:Position>3</deni:Position>
<deni:AliasAddress>0x0103</deni:AliasAddress>
<deni:Optional>true</deni:Optional>
</deni:SlaveConfig>
</Slave>
Set the binding mode before the DENI is loaded:
uint16_t master = Initialize();
/* Fixed wiring, spares of the same model only: have the master check the station order too */
SetHotplugBindMode(master, EC_HOTPLUG_BIND_BY_POSITION);
/* Then load the DENI (the whole file): link-up binds the declared slots to the modules actually present, using the mode set above */
LoadConfigJson(master, deni_text);
A change takes effect on the next scan / configuration load / re-plug rebuild, and does not rewrite bindings that are already established. Passing a value other than 0/1 returns -4 and leaves the current value unchanged; it never silently runs in the default mode.
A master index of 0 means Initialize has not run yet: in that case the mode is only remembered on the calling thread and is dispatched automatically once Initialize has obtained a master. If that dispatch fails the pending value is kept and the next Initialize retries it, again without ever starting silently in the default mode; to confirm that the dispatch succeeded, read GetHotplugBindMode back once after Initialize.
Methods
AcknowledgeSlaveReplacement(uint16_t master_index, uint16_t slave_index)
On an identity mismatch the master refuses to let the module through: it does not guess whether the module you fitted will work, it keeps that station out of OP, reports SlaveIdentityMismatch, and retries on its own schedule. Between retries it does not read the identity every cycle, and it does not apply the old configuration to the new module; on entering the protected state it reports the event only once, and it does not probe again until someone confirms.
AcknowledgeSlaveReplacement is the signal meaning "the person has finished handling it, check again right away". Once called, the master resets the identification flow for that station and re-reads the identity in the next retry beat:
- It matches → the module is recovered automatically and an online notice is raised
- It still does not match →
SlaveIdentityMismatchis reported again and the station returns to the protected state
It does not change the expected identity in the configuration, and it does not force 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.
That is also why it is exposed: only the site or the application knows when the swap is finished and whether the right spare was fitted. The SDK cannot make that judgement for you, and it should not make you restart the whole bus just to retry one slot immediately.
BOOL AcknowledgeSlaveReplacement(uint16_t master_index, uint16_t slave_index);
Parameters:
master_index(uint16_t) — master indexslave_index(uint16_t) — slave index (1-based, matches the configuration)
Returns:
BOOL—TRUEwhen the request is accepted and the identity is re-checked immediately;FALSEwhen the parameter is invalid, or when this station is not currently in the lost state (there is no replacement to confirm)
Calling it on a station that has already recovered, or that never dropped out in the first place, returns FALSE. Calling it before a SlaveIdentityMismatch event has been received returns FALSE as well: that station has not entered the protected state yet, the master is probing normally and does not need your confirmation.
Example:
if (AcknowledgeSlaveReplacement(master, 3))
printf("已让主站重新核对从站 3\n"); /* The master has been asked to re-check slave 3 */
else
printf("从站 3 当前没有待确认的替换,或编号无效\n"); /* Slave 3 has no pending replacement, or the number is invalid */
Events
All hot-plug events are registered in master/events.h. The three events have different jobs:
SlaveIdentityMismatch— the module in this slot does not match the identity expected by the configuration. The payload carries the expected and the 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 onGetSlaveStateandGetSlaveIsLost.
The full event list is in Events.
In C the offline and the online events share one discovery callback, told apart by is_found; the identity mismatch is a separate callback:
void RegisterSlaveIdentityMismatchCallback(identity_mismatch_callback_t callback);
void RegisterSlaveDiscoveryCallbackSync(discovery_callback_t callback);
static void on_identity_mismatch(uint16_t master_index, uint16_t slave_index,
uint32_t expected_vendor, uint32_t expected_product,
uint32_t expected_revision,
uint32_t actual_vendor, uint32_t actual_product,
uint32_t actual_revision)
{
/* Reaching here means Vendor or Product does not match the configuration: a different model, or plugged into the wrong position */
printf("从站 %u 实测 0x%08X/0x%08X,配置期望 0x%08X/0x%08X\n", /* Slave %u measured 0x%08X/0x%08X, the configuration expects 0x%08X/0x%08X */
slave_index, actual_vendor, actual_product,
expected_vendor, expected_product);
}
static void on_discovery(uint16_t master_index, uint16_t slave_index, BOOL is_found)
{
if (is_found) printf("从站 %u 重新应答(是否到 OP 看从站状态)\n", slave_index); /* Slave %u is answering again (whether it reaches OP depends on the slave state) */
else printf("从站 %u 离线\n", slave_index); /* Slave %u is offline */
}
RegisterSlaveIdentityMismatchCallback(on_identity_mismatch);
RegisterSlaveDiscoveryCallbackSync(on_discovery);
SlaveIdentityMismatch
Raised when a module does not match. The criterion is that Vendor or Product differs from the configuration (in alias-bound scenarios the alias is checked as well). Revision is ignored by default: different versions of the same model (spares, firmware upgrades) are treated as the same model and the master only refreshes the record. That is also why a same-model spare plugged back into its original slot or at the chain end can recover automatically. The C SDK offers no switch for this match mode.
Callback payload:
| Parameter | Type | Description |
|---|---|---|
master_index | uint16_t | Master index |
slave_index | uint16_t | Slave index (1-based) |
expected_vendor / expected_product / expected_revision | uint32_t | The identity expected by the configuration |
actual_vendor / actual_product / actual_revision | uint32_t | The identity actually read right now |
The callback carries no event timestamp; if you need one, record it in your own callback.
Revision is only recorded and used for troubleshooting, never as a reason to reject; Vendor and Product are the hard criteria. If the Vendor/Product in the event looks consistent but the master still reports an identity mismatch, check that this station's alias matches the DENI declaration (alias-bound scenarios count it into the criteria).
The master handles this station on its own retry schedule and does not repeat the event every cycle: it is reported once on entering the protected state, and it is probed again — and may be reported again — only after AcknowledgeSlaveReplacement is called. Once the identity is corrected, the next retry (or an immediate retry through AcknowledgeSlaveReplacement) recovers it; if it is still wrong, SlaveIdentityMismatch is reported again.
SlaveOffline
Raised when a module no longer answers. The callback carries only the master index and the slave index: discovery_callback_t(master_index, slave_index, is_found), with is_found FALSE for offline.
An optional slot that is declared in the DENI but has never been plugged in since link-up may also receive an offline notice reported at its declared position. That is not a slave loss — it means the slot never came up — and it is simply handled as an optional absence.
To tell the reasons apart, subscribe to the rich-payload callback with darra_register_slave_offline_detailed. Its ec_slave_offline_event_t carries the evidence at the moment the offline notice is delivered, in that same beat: reason (EC_SLV_OFFLINE_REASON_UNKNOWN / EC_SLV_OFFLINE_REASON_REAL_OFFLINE / EC_SLV_OFFLINE_REASON_UNREACHABLE), the per-slave probe measurement probe, the process data contribution contribution, and the delivery time timestamp_unix_ms. Of these, EC_SLV_OFFLINE_REASON_UNREACHABLE means "the slave-loss flag contradicts the evidence at the delivery moment" (the process data is confirmed to still be contributing) and must not be taken as a confirmed offline.
SlaveOnline
Raised when a module answers again: in the same discovery callback is_found is TRUE. It likewise carries only the master index and the slave index.
It only means the station has been seen again; it does not mean it has reached OP. Only after the master configures the module automatically and promotes it to OP does GetSlaveState become EC_STATE_OPERATIONAL. Before writing outputs, confirm both that GetSlaveIsLost is 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 one you declared: Vendor and Product must match. A difference in Revision is only recorded and is not a reason to reject (same-model spares and firmware upgrades are both allowed). Several modules of the same model are told apart by alias, so one is never mistaken for another.
- Configure automatically — once the module is confirmed, the master applies that slot's configuration and drives the module to OP.
- Restore the data — PDOs take effect with the layout from the configuration and inputs and outputs refresh as usual; the other slaves are not affected.
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 behaviour with 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 its original slot or at the chain end | Accepted as the same model, the record is refreshed, and it is promoted normally | Nothing to do; Revision is only recorded and is not a reason to reject |
| Declared, swapped for a different model (tool module) | The old slot configuration is not applied to the new module; if the DENI declares this tool module separately and the identity matches, it is configured from that declaration; if no declaration is found, an identity mismatch or an extra device is reported and the module is not promoted | Pick the configuration from the tool module catalog; when the DENI does not declare this tool module, add the declaration first and 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, 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 the power supply, the wiring and the configuration |
When troubleshooting, look at three things first: GetSlaveIsLost (whether the loss is confirmed), GetSlaveState (the current state) and GetSlaveALStatusCode (the full AL Status Code). For an identity mismatch, look at the expected and the measured identity in SlaveIdentityMismatch; if it reaches OP and then drops, or never reaches OP at all, look at the AL code and the startup parameter (SDO) errors. The AL code table is in Diagnostics.
If GetHotSwapRescanCount never grows, the master has not started an automatic rebuild: it may not be in a steady OP, the physical slave count may not exceed the registered count, or a mid-chain insertion may have been stopped by the order gate. GetHotSwapRescanLastResult gives the last result code, where a negative value is the specific failure reason.
Automatic promotion happens only for modules that are declared in the configuration and match their declaration. 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 turn it into 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 (does not match / 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 point of a tool module swap is not the unplugging and the plugging — it is the choice and the configuration the application layer has to make after the module is plugged in. The three steps below are recommended.
Step one: declare every tool module you might use in the DENI. Each tool module takes one declared slot with its own Vendor / Product / PDO layout, marked optional (<deni:Optional>true</deni:Optional>). If the workstation is fixed, write a fixed Position; if a tool module may change position, use EC_HOTPLUG_BIND_BY_IDENTITY and burn a unique alias other than 0 into each tool module — the wiring order is not looked at, so the same module is still recognized when it moves from position 3 to position 7, and leaving a few modules out does not stop the rest from working. 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 positions: 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 event to tell a spare apart from a model change. Look at the expected and the measured identity in SlaveIdentityMismatch:
- Vendor and Product the same → the same model; a difference in Revision is only a version difference, and when it is plugged back into its original slot or at the chain end the master recovers it as a spare, so the application layer does not change the configuration.
- Vendor or Product different → the model changed, so handle it as a tool module swap.
AcknowledgeSlaveReplacement only means anything for "the correct module was swapped back in"; a module of a different model is not accepted because of it. A tool module swap goes through a DENI declaration plus application-layer configuration.
If the new tool module is already declared in the DENI and the identity matches, the master configures it to OP from that declaration; at that point you usually receive an online notice first. Once you have read the actual identity from that station, run the same selection and configuration below.
Step three: pick the configuration from the measured identity and send it down. Use the VendorId / ProductCode / RevisionNo read by GetSlaveIdentity to look up the matching configuration in the tool module catalog, first verify that this station's input and output byte counts match the PDO layout of this tool module, then send the process parameters (SDO writes) and the safe output values. If any one of them does not match, do not write outputs: hold the safe state and raise an alarm.
PDO assignment follows the DENI; do not adapt a new tool module by changing the PDO mapping from the application layer at runtime. Swapping in a model that the DENI does not declare is handled by adding the declaration and rebuilding the link, not by hard-rewriting the mapping at runtime.
/* Tool module catalog: find the PDO layout, process parameters and safe outputs from the measured identity */
static void on_module_changed(uint16_t master, uint16_t slave_index)
{
ec_slave_identity_t id;
ec_slavet* slave;
const tool_entry_t* tool;
int i;
if (!GetSlaveIdentity(master, slave_index, &id)) return;
/* Configure and write outputs only on a clean OP; offline and an OP carrying the Error bit both count as unusable */
if (GetSlaveIsLost(master, slave_index)) return;
if (GetSlaveState(master, slave_index) != EC_STATE_OPERATIONAL) return;
tool = tool_catalog_find(id.VendorId, id.ProductCode, id.RevisionNo);
if (tool == NULL)
{
printf("从站 %u 插入了未登记工具模块,保持安全态\n", slave_index); /* Slave %u has an unregistered tool module inserted and stays in the safe state */
return;
}
slave = (ec_slavet*)GetSlave(master, slave_index);
if (slave == NULL) return;
if (slave->input.bytes != tool->input_bytes ||
slave->output.bytes != tool->output_bytes)
{
printf("工具模块 %s 的 PDO 大小与 DENI 不一致,拒绝配置\n", tool->name); /* PDO size of tool module %s does not match DENI, configuration refused */
return;
}
for (i = 0; i < tool->param_count; i++)
{
const tool_param_t* p = &tool->params[i];
if (!dx_sdo_write(master, slave_index, p->index, p->subindex,
FALSE, p->data, p->length))
printf("参数 0x%04X:%02X 写入失败\n", p->index, p->subindex); /* Parameter 0x%04X:%02X write failed */
}
/* Safe output values: write the whole frame exactly */
{
uint32_t written = 0, required = 0;
if (!WriteSlaveOutputPdo(master, slave_index,
tool->safe_outputs, tool->output_bytes,
&written, &required))
printf("工具模块 %s 的安全输出写入失败\n", tool->name); /* Writing the safety output of tool module %s failed */
}
}
Complete example
#include "ethercat.h"
#include <stdio.h>
#include <stdlib.h>
/* Read the whole DENI file (returns a NUL-terminated buffer, NULL on failure) */
static char* read_text_file(const char* path)
{
FILE* f = fopen(path, "rb");
if (!f) return NULL;
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return NULL; }
long size = ftell(f);
if (size < 0) { fclose(f); return NULL; }
rewind(f);
char* text = (char*)malloc((size_t)size + 1);
if (!text) { fclose(f); return NULL; }
size_t got = fread(text, 1, (size_t)size, f);
fclose(f);
text[got] = '\0';
return text;
}
static void on_identity_mismatch(uint16_t master_index, uint16_t slave_index,
uint32_t expected_vendor, uint32_t expected_product,
uint32_t expected_revision,
uint32_t actual_vendor, uint32_t actual_product,
uint32_t actual_revision)
{
/* Vendor or Product does not match: a different model, or plugged into the wrong position.
A Revision difference between same-model modules never reaches here; the master recovers it as a spare. */
printf("从站 %u 实测 0x%08X/0x%08X,配置期望 0x%08X/0x%08X\n", /* Slave %u measured 0x%08X/0x%08X, the configuration expects 0x%08X/0x%08X */
slave_index, actual_vendor, actual_product,
expected_vendor, expected_product);
}
static void on_discovery(uint16_t master_index, uint16_t slave_index, BOOL is_found)
{
if (is_found) printf("从站 %u 重新应答(到 OP 后再配置)\n", slave_index); /* Slave %u is answering again (configure after it reaches OP) */
else printf("从站 %u 离线\n", slave_index); /* Slave %u is offline */
}
int main(void)
{
char* deni_text = read_text_file("C:\\EtherCAT\\line.deni");
uint16_t master;
int rc;
if (deni_text == NULL) return 1;
master = Initialize();
if (master == 0xFFFF) { free(deni_text); return 1; }
/* Fixed slot + spare replacement: position and order are fixed, so use ByPosition.
It must be set before the DENI is loaded: loading binds the slots declared in the DENI to the modules actually present, using it. */
SetHotplugBindMode(master, EC_HOTPLUG_BIND_BY_POSITION);
RegisterSlaveIdentityMismatchCallback(on_identity_mismatch);
RegisterSlaveDiscoveryCallbackSync(on_discovery);
rc = LoadConfigJson(master, deni_text);
free(deni_text);
if (rc != 0)
{
char why[128];
describe_load_config_error(rc, why, (int)sizeof(why));
fprintf(stderr, "初始化失败:%s\n", why); /* Initialization failed: %s */
Dispose(master);
return 1;
}
SetNetwork(master, "", ""); /* empty string = take the NIC selected by the DENI */
if (!SetStateSequence(master, EC_STATE_OPERATIONAL, 10000))
printf("切换 OP 失败\n"); /* Switch to OP failed */
Start(master);
/* After the operator has swapped the correct module back in, let the master re-check this station:
AcknowledgeSlaveReplacement(master, 3); */
Stop(master);
Dispose(master);
return 0;
}