Skip to main content

Initialization

Master indexes start at 1

Master indexes are ≥1; 0 is always invalid. The SDK only creates a master context for a valid index starting at 1; an out-of-range index likewise gets no context, and InitializeSpecificMaster(0) returns 0xFFFF. With multiple masters every API call must pass a valid index explicitly, and initialization must already be complete before the call.

Recommended approach

Use the DENI file exported by the Darra configuration tool to initialize the master.
The DENI file carries the NIC, slaves, PDO mapping, DC settings and startup parameters as one configuration source.
In code, read the whole file and pass it to LoadConfigJson instead of writing these parameters by hand.

NIC and scanning​

The public scan surface is organized in three layers: NIC selection → quick scan (batch / single-port) → full scan; cancel can interrupt at any time.

CategoryUseEntry pointRead/writeDescription
ScanNIC selectionGetNetworkInfo(is_redundant, need_slaves_num) + GetNetworkInfoScanStatus(index)read-onlyEnumerates the NICs that can serve as the primary port; use the scan verdict to tell a measured idle port from "not measured"
ScanQuick scan (batch)DarraEcat_QuickScanAdapters(names, count, rows, capacity)read-onlyOne call returns the verdict for every port (public header export, no short-name wrapper)
ScanQuick scan (single-port)QuickSlaveCount(adapter_name)read-onlyDoes not read the EEPROM, reports the count only; DENI / dropdown counts use this path
ScanFull scanScanSlaveInfoEx(primary, secondary, &native_code)read-onlyReads the EEPROM / topology; returns the outcome code (always 0 on success, not the slave count), the third argument receives the native code (>= 0 = slave count)
ScanCancelAbortScan()write-onlyInterrupts a blocking scan when a window closes or an operation is cancelled; can be called from any thread

Public surface differences per language:

  • Batch quick-scan wrappers exist: C# QuickScan(), Rust quick_scan_adapters(), native C public header DarraEcat_QuickScanAdapters().
  • Single-port quick scan only: C++ statics::QuickSlaveCount(), Python quick_slave_count(), Java NetworkInfo.quickSlaveCount() — the per-port verdict is produced by looping over the network ports in the caller.
  • Full scan has an entry point in every language; cancel goes through AbortScan everywhere. Redundant pairing additionally has QuickFindRedundantPairBatch (returns the cumulative WKC count, not the slave count).
/* 1) NIC selection: pick a port that has slaves, based on the scan verdict */
int n = GetNetworkInfo(FALSE, TRUE);
const ec_network_info_t* list = (const ec_network_info_t*)GetNetworksPointer();
for (int i = 0; i < n; i++) {
if (GetNetworkInfoScanStatus(i) != NIC_SCAN_STATUS_FOUND) continue;

/* 2) Quick scan (does not read the EEPROM) */
int quick = QuickSlaveCount(list[i].name);

/* 3) Full scan (reads the EEPROM): return value = outcome code, third argument = native code (>=0 = slave count) */
int native_code = 0;
int outcome = ScanSlaveInfoEx(list[i].name, NULL, &native_code);
if (outcome == DARRA_SCAN_SUCCESS && native_code >= 0)
printf("%s: 快扫 %d 站,完整扫描 %d 站\n", list[i].name, quick, native_code); /* %s: quick scan found %d stations, full scan found %d stations */
else
printf("%s: 快扫 %d 站,完整扫描失败(outcome=%d, native=%d)\n", /* %s: quick scan found %d stations, full scan failed (outcome=%d, native=%d) */
list[i].name, quick, outcome, native_code);
break;
}

/* 4) Cancel: UI cancel button / window close */
AbortScan();

Creating a master​

The C SDK calls functions of the same name directly: they are available after #include "ethercat.h", with no handle and no function-pointer table. The master index master_index is the first parameter of every subsequent API.

Initialize()​

uint16_t Initialize(void);
uint16_t InitializeSpecificMaster(uint16_t master_index);

Initialize() creates a new master instance and returns its index. Indexes are allocated starting at 1 and never return 0: success is always >0, failure returns 0xFFFF (that is, (uint16_t)-1), so test failure with if (master == 0xFFFF). InitializeSpecificMaster() initializes the master with the given index (index 0 is not allowed; an invalid index likewise returns 0xFFFF). Initialization checks the required runtime components automatically; if the check fails it returns an invalid index and the application should terminate its startup sequence.

SetNetwork()​

uint16_t SetNetwork(uint16_t master_index, const char* if_primary, const char* if_secondary);

Binds a NIC. Passing an empty string "" for if_secondary means single-NIC mode.

Parameters:

  • master_index (uint16_t) — master index
  • if_primary (const char*) — primary NIC name (Windows: \\Device\\NPF_{GUID}, Linux: eth0)
  • if_secondary (const char*) — redundant NIC name ("" means no redundancy)

Return value:

  • uint16_t — number of slaves found; 0 or 0xFFFF means failure

Example:

uint16_t master = Initialize();
uint16_t slave_count = SetNetwork(master, "\\Device\\NPF_{GUID}", "");
printf("发现 %d 个从站\n", slave_count); /* Found %d slaves */

ESI file​

The C SDK has no build-time ESI queue: hand the ESI file to the SDK with esi_dll_load_file() / esi_dll_load_directory() before SetStateSequence() establishes the link, and slave binding completes during link setup. When a DENI file is already in use, a separate load is usually unnecessary.

Resource release​

Dispose()​

BOOL Dispose(uint16_t master_index);
void FreeMemory(void* ptr);
  • Dispose() releases the master instance and returns whether it succeeded. Before releasing it performs one controlled stop on this master (SafeStopMaster), so the stop command is not lost the moment the handle is dropped; after the call master_index must not be used again. Stop() performs the same controlled stop before halting the cycle thread.
  • FreeMemory() frees return values the SDK marks "caller calls FreeMemory" (for example the status struct returned by GetRedundancyStatus()).

Multiple instances​

Each master instance manages its own EtherCAT bus on a different NIC. Call Initialize() several times to create several instances.

Multiple-instance requirements
  • Every instance must use a different NIC (one NIC cannot be used by several instances at the same time)
  • CPU cores are assigned by the SDK automatically; different instances occupy different cores
  • Mailbox Gateway ports are offset automatically by instance index
  • All instances share the same logging system
uint16_t master1 = Initialize();
SetNetwork(master1, "\\Device\\NPF_{GUID_A}", "");

uint16_t master2 = Initialize();
SetNetwork(master2, "\\Device\\NPF_{GUID_B}", "");

SetStateSequence(master1, EC_STATE_OPERATIONAL, 10000);
SetStateSequence(master2, EC_STATE_OPERATIONAL, 10000);

Dispose(master1);
Dispose(master2);

Scan entry points in detail​

Full description of the three layers above; for the order (NIC selection / quick scan / full scan / cancel) see NIC and scanning.

NIC enumeration scope

By default only physical wired ports bound to the real-time driver are listed. Virtual NICs, WiFi and TAP do not appear. USB wired NICs such as ASIX are kept as physical wired ports. Do not treat an unbound port as a primary-port candidate (boundProtocol in the entry).

GetNetworkInfo()​

int   GetNetworkInfo(BOOL is_redundant, BOOL need_slaves_num);
void* GetNetworksPointer(void);

GetNetworkInfo() enumerates NICs and returns the count. The default list contains bound physical wired ports only. With need_slaves_num=TRUE it also fills the slave count for each port; with FALSE it only lists. GetNetworksPointer() returns a pointer to the internal NIC list; it does not need to be freed. Each record contains boundProtocol (a value other than 0 = bound to the real-time driver).

A return of 0 = enumeration completed but no NIC is currently available (a normal empty list); on failure it returns a negative value, which must not be read as "no NICs".

ec_network_info_t.slaveNum semantics (slave count or reason code):

CategoryFieldTypeRead/writeDescription
Slave countslaveNumint16_tread-only>=1: measured slave count; 0 may be a measured idle port, or may mean nothing was measured this round — use the verdict in the next row to tell them apart
Scan verdictGetNetworkInfoScanStatus(index)nic_scan_status_tread-onlyTakes the enum verdict of the NIC at index directly (requires GetNetworkInfo(need_slaves_num=TRUE) first)
Scan verdictnic_scan_status_from_slave_num()nic_scan_status_tread-onlyDecodes slaveNum into a NIC_SCAN_STATUS_* enum
typedef enum {
NIC_SCAN_STATUS_NOT_MEASURED = 0, /* not measured this round (did not take part in the scan / could not open the NIC) — not 0 slaves */
NIC_SCAN_STATUS_IDLE_NO_SLAVE = 1, /* measured idle port: genuinely 0 slaves */
NIC_SCAN_STATUS_FOUND = 2, /* slaves measured */
NIC_SCAN_STATUS_OCCUPIED_BY_MASTER = 3 /* a master on this machine occupies the NIC; the scan skips it by design (a fact, not a failure) */
} nic_scan_status_t;

nic_scan_status_t nic_scan_status_from_slave_num(int16_t slave_num);
Master occupancy is not a failure

While a master is linked, the NIC it is using may not be bound again by a scan. The SDK skips that port by design and marks it NIC_SCAN_STATUS_OCCUPIED_BY_MASTER; such a port has no freshly measured slave count, so it cannot be used to conclude that the bus is empty. When slaveNum == 0, use nic_scan_status_from_slave_num() to tell a measured idle port from "not measured".

0 slaves does not mean an empty bus

The slave count alone cannot distinguish "measured: no slaves" from "nothing measured on this port this round". When a verdict matters, read the classification from GetNetworkInfoScanStatus(); 0 means the bus is empty only when the verdict says the port was measured as idle.

QuickSlaveCount()​

int QuickSlaveCount(const char* adapter_name);

QuickSlaveCount() — DENI / drop-down count: it does not read the EEPROM, only performs a fast probe (reads broadcast frames and does not change the slave configuration) and returns the number of slaves.

Return valueMeaning
>= 1Measured slave count
0The scan ran to completion and there really are no slaves on the bus — measured on an idle port, a normal verdict, not a failure
-1Failed to open the NIC / invalid parameter (adapter_name is empty) / this round was cancelled by AbortScan()
-2The topology write transaction was refused; this round of scanning is incomplete (NIC and slaves are fine)
-3Scanning is winding down (the previous fast scan / wind-down has not drained yet); retry shortly
Other negative valuesUnclassified native negative codes, returned as-is

A negative code is not a slave count — a negative value only says "this round did not measure / did not measure fully". It can neither be read as 0 slaves nor take part in a business judgement about "how many slaves are on the bus". -2 / -3 carry the established semantics of the scan code family (their values match the same-named members of darra_scan_outcome_t in ScanSlaveInfo()); the fast scan only reads broadcast frames and writes no topology, so in practice -1 and unclassified native negative codes are what you will see — report them at their original value.

A negative code is a native return code, not the outcome enum

QuickSlaveCount() returns an int native return code, not a darra_scan_outcome_t. The latter is the outcome classification of ScanSlaveInfoEx(): DARRA_SCAN_SUCCESS = 0, DARRA_SCAN_OPEN_FAILED = -1, DARRA_SCAN_TOPOLOGY_XACT_REFUSED = -2, DARRA_SCAN_CLOSING = -3, plus the -1201..-1206 codes dedicated to DARRA_SCAN_TOPOLOGY_LOOP = -1208 NIC-open failures. The two sets of numbers share the same origin but are different types — do not store a fast-scan return value as the outcome enum, and do not use the outcome enum as a slave count. For a human-readable description, hand the native code to DarraEcatScanOutcomeText(): for an unclassified negative code it faithfully reports "unclassified native error code" and does not invent a reason for the low-level layer.

Unclassified negative codes are returned as-is

Negative codes outside -1 / -2 / -3 and -1201..-1206 have no sentinel value, and the SDK never squashes them into -1 or 0 — the return value is itself the low-level native code. Business logic only tests < 0; when you need finer detail, record and report the real value instead of collapsing it to "unknown failure".

A NIC already held by a local master never reports "occupied"

Before opening the NIC, the fast scan first checks whether the port is already held by a master on this machine: on a hit it only reads the slave count that master already knows and returns a non-negative value (including 0), without opening the NIC again or sending frames. So "occupied" never appears as a negative code in the return value of QuickSlaveCount(). For a per-port named verdict (including NIC_SCAN_STATUS_OCCUPIED_BY_MASTER) use GetNetworkInfoScanStatus() above.

Do not mix the two scan paths
  • Quick count: QuickSlaveCount() (DENI; does not read the EEPROM).
  • Full scan (used to select slaves): ScanSlaveInfoEx(). The return value is the outcome code (always 0 on success, independent of the slave count); read the count from the third out-parameter: *out_native_code >= 0 = slave count (0 = empty bus, a normal verdict), < 0 is the raw failure code. ScanSlaveInfo() has no outcome channel and returns the native code directly (>= 0 = slave count).

ScanSlaveInfo()​

int ScanSlaveInfo(const char* primary, const char* secondary);
int ScanSlaveInfoEx(const char* primary, const char* secondary, int* out_native_code);

Full scan (reads the EEPROM / topology). ScanSlaveInfo() returns the native code directly (>= 0 = slave count); ScanSlaveInfoEx() returns the outcome code, and the native code comes from the third out-parameter. secondary = NULL means single-port scanning. Scan time grows with the number of slaves: if the UI needs immediate feedback, call it on a background thread instead of waiting on the UI thread.

ScanSlaveInfoEx()'s return value is this scan's outcome classification (darra_scan_outcome_t), which distinguishes "the scan completed normally but there really are no slaves on the bus" from "the scan failed"; the third argument out_native_code receives the native return code (>= 0 = slave count, < 0 = raw error code) and may be NULL.

OutcomeDescription
DARRA_SCAN_SUCCESSThe scan completed normally (0 = there are genuinely no slaves on the bus) — a normal verdict, not a failure
DARRA_SCAN_OPEN_FAILEDFailed to open the NIC / invalid parameter, and it does not fall into any of the named reasons below
DARRA_SCAN_TOPOLOGY_XACT_REFUSEDThe topology write transaction was refused; this round of scanning is incomplete
DARRA_SCAN_CLOSINGThe previous scan is still winding down; retry shortly
DARRA_SCAN_OPEN_ACCESS_DENIEDNIC open failed: the driver safety gate refused (insufficient permission)
DARRA_SCAN_OPEN_OCCUPIEDNIC open failed: the port is occupied (another master instance is using it)
DARRA_SCAN_OPEN_NOT_READYNIC open failed: the driver or the NIC is not ready
DARRA_SCAN_OPEN_NIC_NOT_FOUNDNIC open failed: that NIC is not in the driver's NIC pool
DARRA_SCAN_OPEN_LINK_DOWNNIC open failed: the port link is down
DARRA_SCAN_OPEN_BAD_MEDIANIC open failed: non-wired NIC, cannot serve as the EtherCAT master port
DARRA_SCAN_TOPOLOGY_LOOPZero replies (0 slaves) and the linear bus is judged to be wired as a ring (-1208) — a wiring verdict, not a slave/driver fault; disconnect the OUT port of the last slave
Other negative codesUnclassified native codes, returned as-is ((int)outcome is the native code)
typedef enum {
DARRA_SCAN_SUCCESS = 0, /* native 0: scan complete; there really are no slaves on the bus */
DARRA_SCAN_OPEN_FAILED = -1, /* native -1: could not open the NIC / invalid parameter */
DARRA_SCAN_TOPOLOGY_XACT_REFUSED = -2, /* native -2: topology write transaction refused, this round's scan is incomplete */
DARRA_SCAN_CLOSING = -3, /* native -3: scan winding down; retry shortly */

/* Named reason for the NIC open failure (native -1201..-1206); the numeric value always equals the native code*/
DARRA_SCAN_OPEN_ACCESS_DENIED = -1201, /* The safety gate refused (insufficient permission)*/
DARRA_SCAN_OPEN_OCCUPIED = -1202, /* The NIC is occupied (another master instance is using it)*/
DARRA_SCAN_OPEN_NOT_READY = -1203, /* The drive or the NIC is not ready*/
DARRA_SCAN_OPEN_NIC_NOT_FOUND = -1204, /* That NIC is not in the driver's NIC pool*/
DARRA_SCAN_OPEN_LINK_DOWN = -1205, /* NIC link down*/
DARRA_SCAN_OPEN_BAD_MEDIA = -1206, /* Non-wired NIC (wireless/Bluetooth/loopback/tunnel)*/

/* topology verdict (native -1208): zero replies (really 0 slaves) and a linear bus wired as a ring */
DARRA_SCAN_TOPOLOGY_LOOP = -1208 /* restore linear wiring: set the last slave's OUT port back to unconnected */
} darra_scan_outcome_t;

const char* DarraEcatScanOutcomeText(int outcome); /* human-readable outcome text */
int DarraEcatScanOutcomeFromCode(int native_code); /* raw code -> outcome (>=0 normalised to SUCCESS)*/
int DarraEcatScanOutcomeIsUnknown(int outcome); /* 1 = unclassified raw negative code, the value is the raw code*/
  • Scan completed with 0 slaves: the port may be idle, or nothing may have been measured on it this round — decide with the verdict from GetNetworkInfoScanStatus(); never assert an empty bus from 0 alone.
  • Failure: use the outcome enum to tell the reason apart first; detect unclassified negative codes with DarraEcatScanOutcomeIsUnknown(), where (int)outcome is the native code — display it faithfully and check the log instead of inventing a reason.

AbortScan()​

void AbortScan(void);

Aborts an ongoing network scan. Use it to interrupt a blocking scan quickly when a UI window closes or an operation is cancelled; it can be called from any thread. AbortNetwork() / ResetAbortNetwork() are network-layer cancels, see Properties and state machine § Runtime emergency control.

Async scan: darra_scan_async(adapter, done_cb, user_data, &token) runs through a process-wide serial gate (only one scan runs at a time), and darra_scan_async_cancel(token) cancels one specific run (token = NULL broadcasts the cancel). Before the process exits, call darra_scan_async_shutdown() to drain and release the static worker.

One-step initialization (EcInit)​

EcInit()​

uint16_t EcInit(const char* json_config);
uint16_t EcInitFromFile(const char* json_file_path);
void EcClose(uint16_t master_index);

EcInit() performs Initialize + SetNetwork + LoadConfig + SetStateSequence + Start in one step from a JSON string. EcInitFromFile() reads the JSON from a file. EcClose() closes and releases in one step; the two are used as a pair. Return value: success = the master index (>0), failure = 0 — unlike Initialize(), which returns 0xFFFF on failure, so test with == 0.

When you need to build in the background, use darra_build_async_json(gate, json, ...) / darra_build_async_file(gate, path, ...): they queue on the same master gate, return as soon as the work is submitted, and report progress and result through callbacks; cancel with darra_async_token_cancel(token) and release afterwards with darra_async_token_release(token).

JSON configuration:

{
"adapter": "\\Device\\NPF_{GUID}",
"redundant_adapter": "",
"cycle_time_us": 1000,
"dc_sync0_ns": 1000000,
"target_state": "OP",
"slaves": []
}

Field description:

  • adapter (required) — primary NIC name
  • redundant_adapter (optional) — redundant NIC name (empty string = single NIC)
  • cycle_time_us (optional) — PDO cycle, default 1000
  • dc_sync0_ns (optional) — DC SYNC0 cycle, 0 = do not configure DC
  • target_state (optional) — target state ("OP"/"SAFEOP"/"PREOP")
  • slaves (optional) — array of per-slave startup parameters

ENI/DENI configuration​

ethercat.h includes utils/xml.h automatically. To load product DENI/ENI (exported by the Darra configuration tool) use LoadConfigJson: call Initialize first, then pass the whole file contents in; loading completes NIC selection, live slave scanning and applying the whole configuration; afterwards SetNetwork(master, "", "") uses the NIC selected by the DENI.

deni_load / eni_load / xml_apply_config / deni_save / eni_save are offline structure reads and writes: deni_load reads only the legacy offline structure and does not read product DENI; eni_load reads standard ENI. Saving a configuration is provided by Darra products; the SDK does not expose it.

Not a user API

SaveXML / save_xml_configuration / load_and_apply_xml_configuration are not user APIs of the C SDK: the ethercat.h umbrella header does not export them, and utils/xml.h no longer declares them.

deni_load()​

int deni_load(const char* path,
DarraMasterXmlConfig* master_out,
DarraSlaveXmlConfig* slaves_out,
int max_slaves);

Loads the configuration from a legacy offline structure format file. This is the offline format used for struct import / export, not a product DENI exported by the Darra configuration tool; this function returns -4 for a product DENI. To load a product DENI use LoadConfigJson().

Parameters:

  • path (const char*) — legacy offline format XML file path
  • master_out (DarraMasterXmlConfig*) — master configuration output (zero it before the call)
  • slaves_out (DarraSlaveXmlConfig*) — array of slave configurations
  • max_slaves (int) — array capacity

Return value:

  • int — ≥0: number of slaves; -1: invalid parameter; -2: could not open the file; -3: XML parse failure; -4: invalid root element

eni_load()​

int eni_load(const char* path,
DarraMasterXmlConfig* master_out,
DarraSlaveXmlConfig* slaves_out,
int max_slaves);

Parses structures offline from a standard ENI file. Signature and return values are the same as deni_load. Loading an ENI / product DENI at runtime uses LoadConfigJson, so this function is not needed.

xml_apply_config()​

int xml_apply_config(const DarraMasterXmlConfig* master_config,
uint16_t master_index);

Applies the offline structure parsed by deni_load / eni_load to the master. Must be called before SetNetwork (in INIT). When a product DENI goes through LoadConfigJson, the runtime has already applied the whole configuration, so there is no need to call this function again.

Parameters:

  • master_config (const DarraMasterXmlConfig*) — master XML configuration
  • master_index (uint16_t) — master index

Return value:

  • int — 0 on success; -1 invalid parameter; -2 master state unreadable / SDK entry point unavailable

The applied items include cycle, CPU affinity, redundancy, UDP, IOmap lock, VLAN/frame priority, PDO optimization, state timeouts and the watchdog.

Example:

/* offline structure path (standard ENI): parse into structs -> apply.
A product DENI does not need this step: LoadConfigJson already applies the whole configuration. */
DarraMasterXmlConfig cfg = {0};
DarraSlaveXmlConfig slaves[XML_MAX_SLAVES];
int n = eni_load("C:\\EtherCAT\\network.eni", &cfg, slaves, XML_MAX_SLAVES);
if (n < 0) { fprintf(stderr, "ENI 解析失败 %d\n", n); return 1; } /* ENI parse failed %d */

uint16_t master = Initialize();
if (xml_apply_config(&cfg, master) != 0) {
fprintf(stderr, "配置下发失败\n"); /* Failed to send the configuration */
Dispose(master);
return 1;
}
SetNetwork(master, adapter, "");

deni_save() / eni_save()​

int deni_save(const char* path,
const DarraMasterXmlConfig* master,
const DarraSlaveXmlConfig* slaves,
int slave_count);
int eni_save(const char* path,
const DarraMasterXmlConfig* master,
const DarraSlaveXmlConfig* slaves,
int slave_count);

Writes the parsed configuration structs back to a DENI / ENI file. This is offline struct serialization, not dumping a live master to XML.

Return value:

  • int — 0 on success, negative on failure

Full examples​

Using ENI/DENI files (recommended)​

#include "ethercat.h"
#include <stdio.h>
#include <stdlib.h>

/* read the whole configuration 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;
}

int main(void)
{
char* deni = read_text_file("C:\\EtherCAT\\MyProject.deni");
if (!deni) { return 1; }

uint16_t master = Initialize();
if (master == 0xFFFF) { free(deni); return 1; } /* failure sentinel = (uint16_t)-1; indexes start at 1, 0 is never returned */

/* product DENI: a leading '<' -> parsed as XML; NIC selection + live scan + the whole configuration */
int rc = LoadConfigJson(master, deni);
free(deni);
if (rc != 0) {
char why[128];
describe_load_config_error(rc, why, (int)sizeof(why));
fprintf(stderr, "DENI 加载失败: %s\n", why); /* DENI load failed: %s */
Dispose(master);
return 1;
}

uint16_t ret = SetNetwork(master, "", ""); /* empty strings = use the NIC selected by the DENI */
if (ret == 0 || ret == 0xFFFF) {
Dispose(master); return 1;
}
printf("发现 %d 个从站\n", ret); /* Found %d slaves */

if (!SetStateSequence(master, EC_STATE_OPERATIONAL, 10000)) {
Dispose(master); return 1;
}
Start(master);

getchar();

Stop(master);
Dispose(master);
return 0;
}

Scanning NICs dynamically​

int adapter_count = GetNetworkInfo(FALSE, TRUE);
printf("发现 %d 个网络适配器\n", adapter_count); /* Found %d network adapters */

const ec_network_info_t* list = (const ec_network_info_t*)GetNetworksPointer();
for (int i = 0; i < adapter_count; i++) {
if (GetNetworkInfoScanStatus(i) != NIC_SCAN_STATUS_FOUND) continue;

/* full scan: read the EEPROM slave by slave; return value = outcome code, third argument = native code (>=0 = slave count) */
int native_code = 0;
int outcome = ScanSlaveInfoEx(list[i].name, NULL, &native_code);
if (outcome == DARRA_SCAN_SUCCESS && native_code >= 0)
printf("%s 完整扫描 %d 个从站\n", list[i].name, native_code); /* %s full scan found %d slaves */
else
printf("%s 完整扫描失败(outcome=%d, native=%d)\n", list[i].name, outcome, native_code); /* %s full scan failed (outcome=%d, native=%d) */
}

uint16_t master = Initialize();
SetNetwork(master, "\\Device\\NPF_{GUID}", "");
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);

One-step initialization​

uint16_t master = EcInitFromFile("config.json");
if (master == 0) { return 1; } /* EcInit / EcInitFromFile return 0 on failure (unlike Initialize(), which returns 0xFFFF); success returns the master index >0 */

/* ... application logic ... */

EcClose(master);