FSoE functional safety
Accessed through slave/fsoe.h — the master index and slave index locate one safety slave.
FSoE adds a functional safety protocol layer on top of the standard PDO channel (ETG.5100/5120): the master maintains the safety connection state machine, the slave runs the safety logic, and safety data carries CRC and sequence number checks. This is not a certified safety product and not a PLC capability; the safety level actually reached depends on the safety device and the overall safety concept. Before establishing a connection, confirm with dx_fsoe_is_slave_capable that the slave supports FSoE; do not bind a slave that does not.
FSoE devices are detected automatically when the slave is initialized: the slave must support the CoE mailbox protocol and implement the FSoE safety objects (the device-level safety address object 0xF980:01, or the connection parameter object 0x9001:02 on modular devices). A plain CoE-only device (an ordinary servo drive, for example) is not recognized as an FSoE slave, and dx_fsoe_is_slave_capable returns false.
Function overview
| Category | Function | Type | Read/write | Description |
|---|---|---|---|---|
| Capability | dx_fsoe_is_slave_capable | BOOL | Read | Whether the slave supports FSoE; do not establish a connection if it does not |
| Capability | dx_fsoe_get_connection_count | uint16_t | Read | Current number of FSoE connections on the master |
| Binding | dx_fsoe_init_connection | BOOL | Write | Binds the safety input/output byte counts and PDO offsets and establishes the safety connection; pass ConnectionId 0 to let the SDK allocate one and write it back |
| Binding | dx_fsoe_download_parameters | BOOL | Write | Downloads safety parameters in the PARAMETER state and returns the SRA CRC through an output parameter |
| Binding | dx_fsoe_close_connection | BOOL | Write | Closes the current safety connection |
| Safety input | dx_fsoe_read_safe_input | BOOL | Read | Reads back the safety input bytes and length |
| Safety output | dx_fsoe_write_safe_output | BOOL | Write | Writes safety output bytes; the length must match the size given at binding time |
| Safety output | dx_fsoe_set_failsafe_output | BOOL | Write | Presets the fail-safe output value, which the slave outputs on watchdog timeout or when entering FAILSAFE |
| State control | dx_fsoe_request_state | BOOL | Write | Requests a state machine transition to the target state; the target must be a legal next state of the current state |
| State control | dx_fsoe_get_state | int | Read | Current FSoE state (FSOE_STATE_*) |
| State control | dx_fsoe_get_status | BOOL | Read | Takes a snapshot of state, error counts, sent and received frame counts, CRC and watchdog counters; the return value also answers "is there a valid FSoE connection", and a read triggers one state refresh |
| State control | dx_fsoe_reset | BOOL | Write | Resets the connection to the initial state and re-runs the handshake |
| State control | fsoe_is_valid_state_transition | int | Read | Queries whether one state transition is legal (same predicate as request_state) |
| Diagnostics | dx_fsoe_check_watchdog | BOOL | Read | Checks the watchdog; false means it has expired or the connection is not initialized |
| Diagnostics | dx_fsoe_get_last_error | int | Read | Returns the most recent FSoE error code |
| Diagnostics | dx_fsoe_clear_error | void | Write | Clears the most recent error record |
| Cycle | dx_fsoe_process_cycle | void | Write | Called once per cycle by the runtime's cyclic path; the application layer must not call it again |
| MDP multi-connection | DxSafeMdp* series | BOOL / int / uint16_t | Read/write | Operates on each connection of a modular safety slave by connection_index; semantics match the single-connection entry points |
| Manager | fsoe_manager_* series | int / BOOL | Read/write | Multi-connection lifecycle management: create, add connections, query, and put all connections into fail-safe |
| CRC | fsoe_crc16 / fsoe_crc16_with_conn_id | uint16_t | Read | FSoE CRC16 (ETG.5100 safety polynomial); the ConnID variant prevents frame crosstalk between connections |
| ConnID | dx_fsoe_validate_conn_id / fsoe_validate_conn_id | BOOL | Read | Validates that a ConnID is usable (not in use and not 0) |
| Frame tools | fsoe_pdo_frame_to_bytes / fsoe_pdo_frame_from_bytes / fsoe_pdo_frame_validate_crc_segments | int / BOOL | Read/write | Builds, parses and verifies FSoE PDO frame byte streams offline |
State machine
FSoE establishes an independent safety connection on the EtherCAT PDO channel: the master handles protocol management (handshake, CRC, watchdog, sequence numbers) and the slave handles the safety logic. The state machine order is RESET → SESSION → CONNECTION → PARAMETER → DATA; DATA can only go through FAILSAFE before returning to RESET, and FAILSAFE can return to DATA or RESET. The legal transitions are a fixed set:
| Current state | Allowed target states |
|---|---|
| RESET | SESSION |
| SESSION | CONNECTION, RESET |
| CONNECTION | PARAMETER, RESET |
| PARAMETER | DATA, RESET |
| DATA | FAILSAFE (the only one; a direct transition to RESET is not allowed) |
| FAILSAFE | DATA, RESET |
- FSOE_STATE_RESET (0x100) — initial state, waiting for the master to start a session
- FSOE_STATE_SESSION (0x101) — session established, connection ID exchanged
- FSOE_STATE_CONNECTION (0x102) — connection established, safety address verified
- FSOE_STATE_PARAMETER (0x103) — parameter download phase (SRA CRC check)
- FSOE_STATE_DATA (0x104) — normal safety data exchange
- FSOE_STATE_FAILSAFE (0x105) — fail-safe, the slave outputs the preset safety values
After dx_fsoe_init_connection establishes the connection, the runtime advances the intermediate states (SESSION → CONNECTION → PARAMETER) automatically each cycle; the application layer does not drive the state machine itself. When the device requires safety parameters, call dx_fsoe_download_parameters in the PARAMETER phase.
Safety data access
FSoE safety data cannot be mapped directly like normal PDO data: the IOmap holds FSoE protocol frames, and the user's safety data is wrapped inside those frames with CRC and sequence number checks. Reading or writing the IOmap directly, bypassing the SDK, breaks the integrity of the safety protocol; safety data always goes through the dedicated buffers of dx_fsoe_read_safe_input / dx_fsoe_write_safe_output.
- Input frame integrity checks, output frame CRC calculation, state machine advancement, and watchdog and sequence number checks are all handled by the SDK; the application layer is not involved. On a watchdog timeout or a failed check the safety connection enters FAILSAFE and the slave outputs the preset fail-safe value.
- Neither read nor write is same-cycle synchronous: dx_fsoe_read_safe_input returns safety inputs already refreshed by the runtime, and dx_fsoe_write_safe_output writes into the output buffer that will be committed in the next cycle.
- Do not drive the safety loop from ordinary PDO notifications such as RegisterProcessDataCyclicCallbackSync, and do not call dx_fsoe_process_cycle again (advancing the state machine twice in one cycle breaks the safety timing).
- SafeInputSize / SafeOutputSize must match the sizeof() of the application-side safety data structures, otherwise reads and writes fail.
Type definitions
fsoe_state_t
typedef enum {
FSOE_STATE_RESET = 0x100, /* initial/reset state */
FSOE_STATE_SESSION = 0x101, /* session established */
FSOE_STATE_CONNECTION = 0x102, /* connection established */
FSOE_STATE_PARAMETER = 0x103, /* parameter download */
FSOE_STATE_DATA = 0x104, /* data exchange */
FSOE_STATE_FAILSAFE = 0x105 /* fail-safe */
} fsoe_state_t;
fsoe_error_t
typedef enum {
FSOE_ERROR_NONE = 0x0000, /* no error */
FSOE_ERROR_WRONG_COMMAND = 0x0001, /* wrong command */
FSOE_ERROR_UNKNOWN_COMMAND = 0x0002, /* unknown command */
FSOE_ERROR_WRONG_CONN_ID = 0x0003, /* connection ID mismatch */
FSOE_ERROR_CRC = 0x0004, /* CRC check failed */
FSOE_ERROR_WATCHDOG = 0x0005, /* watchdog timeout */
FSOE_ERROR_WRONG_ADDRESS = 0x0006, /* wrong FSoE address */
FSOE_ERROR_WRONG_DATA = 0x0007, /* invalid data */
FSOE_ERROR_COMM_PARAM_LENGTH = 0x0008, /* communication parameter length error */
FSOE_ERROR_COMM_PARAM = 0x0009, /* communication parameter error */
FSOE_ERROR_APP_PARAM_LENGTH = 0x000A, /* application parameter length error */
FSOE_ERROR_APP_PARAM = 0x000B, /* application parameter error */
FSOE_ERROR_UNEXPECTED_SESSION = 0x000C, /* unexpected session command */
FSOE_ERROR_FAILSAFE_DATA = 0x000D, /* fail-safe data received */
FSOE_ERROR_NOT_INITIALIZED = 0x0100, /* FSoE not initialized (internal error) */
FSOE_ERROR_MAX_CONNECTIONS = 0x0101, /* maximum number of connections reached (internal error) */
FSOE_ERROR_INVALID_STATE = 0x0102 /* invalid state transition (internal error) */
} fsoe_error_t;
fsoe_config_t
typedef struct {
uint16_t ConnectionId; /* unique connection identifier */
uint16_t SafetyAddress; /* FSoE slave safety address */
uint32_t WatchdogTimeMs; /* watchdog timeout (ms) */
uint16_t SafeInputSize; /* safety input data size (bytes) */
uint16_t SafeOutputSize; /* safety output data size (bytes) */
uint32_t PdoInputOffset; /* input offset in the IOmap */
uint32_t PdoOutputOffset; /* output offset in the IOmap */
} fsoe_config_t;
fsoe_status_t
typedef struct {
fsoe_state_t State; /* current FSoE state */
fsoe_error_t LastError; /* last error code */
uint32_t ErrorCount; /* total error count */
uint32_t FramesSent; /* frames sent */
uint32_t FramesReceived; /* valid frames received */
uint32_t CrcErrors; /* CRC error count */
uint32_t WatchdogErrors; /* watchdog timeout count */
uint8_t WatchdogExpired; /* current watchdog state */
uint8_t InFailsafe; /* whether in fail-safe mode */
uint8_t ToggleBit; /* current toggle bit value */
uint8_t Initialized; /* whether the connection is initialized */
} fsoe_status_t;
fsoe_mdp_config_t
typedef struct {
uint16_t ConnectionId; /* unique connection identifier */
uint16_t SafetyAddress; /* FSoE slave safety address */
uint32_t WatchdogTimeMs; /* watchdog timeout (ms) */
uint16_t SafeInputSize; /* safety input data size (bytes) */
uint16_t SafeOutputSize; /* safety output data size (bytes) */
uint32_t PdoInputOffset; /* input offset in the IOmap */
uint32_t PdoOutputOffset; /* output offset in the IOmap */
uint16_t ModuleNumber; /* module number (slot number) */
uint16_t ModuleProfile; /* module profile (190, 195, 290, 790, 6900) */
uint16_t AxisNumber; /* axis number (Drive Connection only) */
uint16_t ConnectionType; /* connection type: 0=Master, 1=Slave */
} fsoe_mdp_config_t;
Single-connection interface (slave/fsoe.h)
The direct entry points for a single connection to a single slave; the addressing parameters are the master index and the slave index. For the multi-connection manager see "FSoE manager" below; for MDP modular safety slaves see "MDP multi-connection".
dx_fsoe_is_slave_capable()
BOOL dx_fsoe_is_slave_capable(uint16_t master_index, uint16_t slave_index);
Queries whether the slave supports FSoE. Call it before establishing a connection and do not bind a slave that does not; never use the return value of dx_fsoe_init_connection as the capability predicate.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- BOOL — TRUE if the slave declares FSoE capability; FALSE if it is not supported or the capability object cannot be read
Example:
if (!dx_fsoe_is_slave_capable(master, slave)) {
printf("该从站不支持 FSoE\n"); /* This slave does not support FSoE */
return 1;
}
dx_fsoe_init_connection()
BOOL dx_fsoe_init_connection(uint16_t master_index, uint16_t slave_index, fsoe_config_t* config);
Binds the safety input/output byte counts and PDO offsets per fsoe_config_t and establishes the safety connection. Passing ConnectionId 0 asks the SDK to allocate one; the actual value is written back to config->ConnectionId.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- config (fsoe_config_t*) — connection configuration (input; when ConnectionId is 0 the actual ID is written back through the output)
Returns:
- BOOL — TRUE on success; FALSE on failure (ConnectionId already in use, invalid configuration, or the current state does not allow the connection)
Example:
fsoe_config_t cfg = {
.ConnectionId = 0, /* 0 = allocated by the SDK */
.SafetyAddress = 0x0100, /* matches the device DIP switch setting */
.WatchdogTimeMs = 100,
.SafeInputSize = 2, .SafeOutputSize = 1,
.PdoInputOffset = 0, .PdoOutputOffset = 0
};
if (!dx_fsoe_init_connection(master, slave, &cfg)) {
printf("安全连接建立失败\n"); /* Failed to establish the safety connection */
return 1;
}
printf("ConnID = 0x%04X\n", cfg.ConnectionId);
dx_fsoe_download_parameters()
BOOL dx_fsoe_download_parameters(uint16_t master_index, uint16_t slave_index,
const uint8_t* param_data, uint32_t param_size,
uint32_t* sra_crc);
Downloads safety parameters in the PARAMETER state (SRA CRC check). When the device requires safety parameters this must complete before DATA is entered, otherwise the connection stays in PARAMETER.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- param_data (const uint8_t*) — safety parameter bytes
- param_size (uint32_t) — parameter byte count
- sra_crc (uint32_t*) — output: SRA CRC of the parameter block; not guaranteed to be written on failure
Returns:
- BOOL — TRUE if the download succeeded; FALSE on failure (the state is not yet PARAMETER or a mailbox write failed)
Example:
uint8_t param[8] = { /* fill in per the safety parameter layout in the device manual */ };
uint32_t sra_crc = 0;
if (!dx_fsoe_download_parameters(master, slave, param, sizeof(param), &sra_crc))
printf("安全参数下发失败\n"); /* Failed to send the safety parameters */
dx_fsoe_close_connection()
BOOL dx_fsoe_close_connection(uint16_t master_index, uint16_t slave_index);
Closes the FSoE connection of the current slave; init_connection can be called again afterwards.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- BOOL — TRUE if closed; FALSE if there was no connection or closing failed
dx_fsoe_read_safe_input()
BOOL dx_fsoe_read_safe_input(uint16_t master_index, uint16_t slave_index,
uint8_t* data, uint32_t* size);
Reads back the safety input bytes. size is an in/out parameter: pass the buffer capacity in, get the actual byte count back.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- data (uint8_t*) — output buffer: safety input bytes (size it for SafeInputSize)
- size (uint32_t*) — input = buffer capacity, output = actual byte count read back
Returns:
- BOOL — TRUE if the read succeeded (size has been written); FALSE if it failed (the connection is not in DATA or does not exist), in which case the output is not guaranteed to be written
Example:
uint8_t safe_in[2] = {0};
uint32_t in_size = sizeof(safe_in); /* reset the available capacity every cycle */
if (dx_fsoe_read_safe_input(master, slave, safe_in, &in_size)) {
/* interpret safe_in[0..in_size-1] using the application-side safety data structure */
}
dx_fsoe_write_safe_output()
BOOL dx_fsoe_write_safe_output(uint16_t master_index, uint16_t slave_index,
const uint8_t* data, uint32_t size);
Writes the safety output bytes; size must match the SafeOutputSize given at binding time. The write goes into the output buffer that will be committed in the next cycle.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- data (const uint8_t*) — safety output bytes
- size (uint32_t) — byte count, must equal SafeOutputSize
Returns:
- BOOL — TRUE if the write succeeded; FALSE on failure (length mismatch, connection not ready)
Example:
uint8_t safe_out[1] = {0x01};
if (!dx_fsoe_write_safe_output(master, slave, safe_out, sizeof(safe_out)))
printf("安全输出写入失败\n"); /* Safety output write failed */
dx_fsoe_set_failsafe_output()
BOOL dx_fsoe_set_failsafe_output(uint16_t master_index, uint16_t slave_index,
const uint8_t* data, uint32_t size);
Presets the fail-safe output value: after a watchdog timeout or on entering FAILSAFE the slave outputs this value directly, without relying on a further write from the master. Call it once after the connection is established and before data exchange starts.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- data (const uint8_t*) — fail-safe output bytes
- size (uint32_t) — byte count, must equal SafeOutputSize
Returns:
- BOOL — TRUE if the value was set; FALSE on failure (length mismatch, connection not ready)
Example:
uint8_t safe_out[1] = {0};
if (!dx_fsoe_set_failsafe_output(master, slave, safe_out, sizeof(safe_out))) {
printf("失效安全值设置失败\n"); /* Failsafe value setup failed */
}
dx_fsoe_request_state()
BOOL dx_fsoe_request_state(uint16_t master_index, uint16_t slave_index, int target_state);
Requests a state machine transition to the target state. The target must be a legal next state of the current state (see the transition table above); an illegal transition returns FALSE immediately.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- target_state (int) — target state (an fsoe_state_t value)
Returns:
- BOOL — TRUE if the request was accepted; FALSE if the target is not a legal next state or there is no connection
Example:
if (dx_fsoe_get_state(master, slave) == FSOE_STATE_FAILSAFE) {
/* FAILSAFE only returns to DATA or RESET on an explicit request */
dx_fsoe_request_state(master, slave, FSOE_STATE_DATA);
}
fsoe_is_valid_state_transition()
int fsoe_is_valid_state_transition(int from_state, int to_state);
Queries whether one state transition is legal, using the same predicate as dx_fsoe_request_state.
Parameters:
- from_state (int) — state before the transition (an fsoe_state_t value)
- to_state (int) — target state (an fsoe_state_t value)
Returns:
- 1 — allowed
- 0 — the state is valid but this transition is not allowed
- -1 — missing parameter or the SDK is not ready
- -5 — the state value is outside 0x100..0x105 (EC_FSOE_ERR_STATE)
Example:
if (fsoe_is_valid_state_transition(FSOE_STATE_FAILSAFE, FSOE_STATE_DATA) == 1)
dx_fsoe_request_state(master, slave, FSOE_STATE_DATA);
dx_fsoe_get_state()
int dx_fsoe_get_state(uint16_t master_index, uint16_t slave_index);
Returns the current FSoE state. When you need to distinguish "cannot be read", use dx_fsoe_get_status (FALSE = the state cannot be trusted).
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- int — current state (an FSOE_STATE_* value)
dx_fsoe_get_status()
BOOL dx_fsoe_get_status(uint16_t master_index, uint16_t slave_index, fsoe_status_t* status);
Takes a connection status snapshot: state, error code, error counts, sent and received frame counts, CRC and watchdog counters, and so on.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- status (fsoe_status_t*) — output: status snapshot
Returns:
- BOOL — TRUE if the snapshot was written; FALSE if there is no connection or the read failed, in which case the output is not guaranteed to be written; never treat an all-0, unwritten snapshot as "safe"
Is there a valid FSoE connection: the C side has no separate IsConnected entry point — the return value of this function is that fact: TRUE = a valid connection exists (the snapshot is trustworthy), FALSE = no connection / read failure / runtime not ready (the snapshot cannot be trusted). A read triggers one state refresh. On the C++ side this is IsConnected() / StatusValid() — the same fact under a different language's naming.
dx_fsoe_reset()
BOOL dx_fsoe_reset(uint16_t master_index, uint16_t slave_index);
Resets the connection to RESET and re-runs the handshake. A triggered fail-safe does not recover automatically: remove the fault source and call this to rebuild the connection.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- BOOL — TRUE if the reset succeeded; FALSE if there is no connection or the reset failed
dx_fsoe_check_watchdog()
BOOL dx_fsoe_check_watchdog(uint16_t master_index, uint16_t slave_index);
Checks whether the watchdog is still within its time window; after a timeout the slave switches its outputs to the fail-safe value.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- BOOL — TRUE if the watchdog is healthy; FALSE if it has expired or the connection is not initialized
Example:
if (!dx_fsoe_check_watchdog(master, slave)) {
printf("看门狗超时, 输出由从站切到失效安全值\n"); /* Watchdog timeout, the slave switched its outputs to the failsafe value */
}
dx_fsoe_get_last_error()
int dx_fsoe_get_last_error(uint16_t master_index, uint16_t slave_index);
Returns the most recent FSoE error code.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- int — error code (an FSOE_ERROR_* value); FSOE_ERROR_NONE means no error
dx_fsoe_clear_error()
void dx_fsoe_clear_error(uint16_t master_index, uint16_t slave_index);
Clears the most recent error record.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns: none
dx_fsoe_get_connection_count()
uint16_t dx_fsoe_get_connection_count(uint16_t master_index);
Returns the current number of FSoE connections on the master.
Parameters:
- master_index (uint16_t) — master index
Returns:
- uint16_t — current connection count
dx_fsoe_process_cycle()
void dx_fsoe_process_cycle(uint16_t master_index);
Processes one FSoE cycle (reads input frames, advances the state machine, builds output frames). It is called once per cycle by the runtime's PDO cyclic path; the application layer must not call it again.
Parameters:
- master_index (uint16_t) — master index
Returns: none
MDP multi-connection (slave/fsoe.h)
An MDP (modular device) slave carries several FSoE safety connections, addressed with the DxSafeMdp* entry points by connection_index; each connection is configured with fsoe_mdp_config_t (the first 7 fields match fsoe_config_t, plus module number, module profile, axis number and connection type). Apart from the extra connection_index parameter, each entry point behaves exactly like its single-connection counterpart.
DxSafeMdpInitConnection()
BOOL DxSafeMdpInitConnection(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index, fsoe_mdp_config_t* config);
Initializes the given connection of an MDP slave. Passing ConnectionId 0 allocates one automatically and writes it back to config->ConnectionId.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
- config (fsoe_mdp_config_t*) — connection configuration (input; when ConnectionId is 0 the actual ID is written back through the output)
Returns:
- BOOL — TRUE on success; FALSE on failure
Example:
fsoe_mdp_config_t cfg = {0};
cfg.ConnectionId = 1; cfg.SafetyAddress = 0x0100; cfg.WatchdogTimeMs = 100;
cfg.SafeInputSize = 2; cfg.SafeOutputSize = 1;
cfg.ModuleNumber = 1; cfg.ModuleProfile = 190; cfg.ConnectionType = 0;
if (!DxSafeMdpInitConnection(master, slave, 0, &cfg)) {
printf("MDP 连接建立失败\n"); /* MDP connection establishment failed */
}
DxSafeMdpCloseConnection()
BOOL DxSafeMdpCloseConnection(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index);
Closes the given connection.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
Returns:
- BOOL — TRUE if closed; FALSE if there is no such connection or closing failed
Example:
DxSafeMdpCloseConnection(master, slave, 0);
DxSafeMdpGetStatus()
BOOL DxSafeMdpGetStatus(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index, fsoe_status_t* status);
Takes a status snapshot of the given connection.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
- status (fsoe_status_t*) — output: status snapshot
Returns:
- BOOL — TRUE if the snapshot was written; FALSE if there is no such connection or the read failed, in which case the output is not guaranteed to be written
DxSafeMdpRequestState()
BOOL DxSafeMdpRequestState(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index, int target_state);
Requests the given connection to transition to the target state; the target must be a legal next state of the current state.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
- target_state (int) — target state (an fsoe_state_t value)
Returns:
- BOOL — TRUE if the request was accepted; FALSE if the target is not a legal next state or there is no such connection
Example:
DxSafeMdpRequestState(master, slave, 0, FSOE_STATE_DATA);
DxSafeMdpGetState()
int DxSafeMdpGetState(uint16_t master_index, uint16_t slave_index, uint16_t connection_index);
Returns the current state of the given connection.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
Returns:
- int — current state (an FSOE_STATE_* value)
DxSafeMdpReset()
BOOL DxSafeMdpReset(uint16_t master_index, uint16_t slave_index, uint16_t connection_index);
Resets the given connection to RESET and re-runs the handshake.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
Returns:
- BOOL — TRUE if the reset succeeded; FALSE if there is no such connection or the reset failed
DxSafeMdpReadSafeInput()
BOOL DxSafeMdpReadSafeInput(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index, uint8_t* data, uint32_t* size);
Reads the safety input of the given connection; size is the buffer capacity on input and the actual byte count on output, and the output is not guaranteed to be written on failure.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
- data (uint8_t*) — output buffer: safety input bytes
- size (uint32_t*) — input = buffer capacity, output = actual byte count read back
Returns:
- BOOL — TRUE if the read succeeded; FALSE on failure
Example:
uint8_t in[2] = {0};
uint32_t n = sizeof(in);
if (DxSafeMdpReadSafeInput(master, slave, 0, in, &n)) {
/* interpret in[0..n-1] using this connection's SafeInputSize */
}
DxSafeMdpWriteSafeOutput()
BOOL DxSafeMdpWriteSafeOutput(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index, const uint8_t* data, uint32_t size);
Writes the safety output of the given connection; size must equal that connection's SafeOutputSize.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
- data (const uint8_t*) — safety output bytes
- size (uint32_t) — byte count, must equal SafeOutputSize
Returns:
- BOOL — TRUE if the write succeeded; FALSE on failure (length mismatch, connection not ready)
Example:
uint8_t out[1] = {0x01};
DxSafeMdpWriteSafeOutput(master, slave, 0, out, sizeof(out));
DxSafeMdpDownloadParameters()
BOOL DxSafeMdpDownloadParameters(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index, const uint8_t* param_data,
uint32_t param_size, uint32_t* sra_crc);
Downloads safety parameters to the given connection in the PARAMETER state; sra_crc returns the SRA CRC through an output parameter, and is not guaranteed to be written on failure.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
- param_data (const uint8_t*) — safety parameter bytes
- param_size (uint32_t) — parameter byte count
- sra_crc (uint32_t*) — output: SRA CRC of the parameter block
Returns:
- BOOL — TRUE if the download succeeded; FALSE on failure (the state is not yet PARAMETER or a mailbox write failed)
DxSafeMdpSetFailsafeOutput()
BOOL DxSafeMdpSetFailsafeOutput(uint16_t master_index, uint16_t slave_index,
uint16_t connection_index, const uint8_t* data, uint32_t size);
Presets the fail-safe output value of the given connection; after a watchdog timeout or on entering FAILSAFE the slave outputs this value.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
- data (const uint8_t*) — fail-safe output bytes
- size (uint32_t) — byte count, must equal SafeOutputSize
Returns:
- BOOL — TRUE if the value was set; FALSE on failure (length mismatch, connection not ready)
DxSafeMdpCheckWatchdog()
BOOL DxSafeMdpCheckWatchdog(uint16_t master_index, uint16_t slave_index, uint16_t connection_index);
Checks whether the watchdog is still within its time window.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
Returns:
- BOOL — TRUE if the watchdog is healthy; FALSE if it has expired or that connection is not initialized
DxSafeMdpGetLastError()
int DxSafeMdpGetLastError(uint16_t master_index, uint16_t slave_index, uint16_t connection_index);
Returns the most recent error code of the given connection.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
Returns:
- int — error code (an FSOE_ERROR_* value)
DxSafeMdpClearError()
void DxSafeMdpClearError(uint16_t master_index, uint16_t slave_index, uint16_t connection_index);
Clears the error record of the given connection.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- connection_index (uint16_t) — connection index
Returns: none
DxSafeMdpGetSlaveConnectionCount()
uint16_t DxSafeMdpGetSlaveConnectionCount(uint16_t master_index, uint16_t slave_index);
Returns the current number of FSoE connections on the slave.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- uint16_t — current connection count
DxSafeMdpDetectConnections()
uint16_t DxSafeMdpDetectConnections(uint16_t master_index, uint16_t slave_index);
Detects whether the slave is an MDP device and returns the number of supported FSoE connections.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
Returns:
- uint16_t — number of FSoE connections supported by that slave
DxSafeMdpGetDeviceAddress()
BOOL DxSafeMdpGetDeviceAddress(uint16_t master_index, uint16_t slave_index,
uint16_t* safety_address);
Reads the device-level FSoE safety address of the slave (object 0xF980:01).
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- safety_address (uint16_t*) — output: device-level safety address
Returns:
- BOOL — TRUE if the read succeeded; FALSE on failure, in which case the output is not guaranteed to be written
DxSafeMdpGetModuleCommParam()
BOOL DxSafeMdpGetModuleCommParam(uint16_t master_index, uint16_t slave_index,
uint16_t module_number, uint8_t* param_data,
uint32_t* param_size);
Reads the FSoE communication parameters of the given module (0x9nn1). param_size is the buffer capacity on input and the actual byte count on output.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- module_number (uint16_t) — module number (slot number)
- param_data (uint8_t*) — output buffer: communication parameter bytes
- param_size (uint32_t*) — input = buffer capacity, output = actual byte count
Returns:
- BOOL — TRUE if the read succeeded; FALSE on failure, in which case the output is not guaranteed to be written
DxSafeMdpGetModuleDiagnosis()
BOOL DxSafeMdpGetModuleDiagnosis(uint16_t master_index, uint16_t slave_index,
uint16_t module_number, uint16_t* connection_state,
uint16_t* connection_diagnosis);
Reads the FSoE diagnostics of the given module (0xAnn0): two output parameters, connection state and connection diagnosis, not guaranteed to be written on failure.
Parameters:
- master_index (uint16_t) — master index
- slave_index (uint16_t) — slave index
- module_number (uint16_t) — module number (slot number)
- connection_state (uint16_t*) — output: connection state
- connection_diagnosis (uint16_t*) — output: connection diagnosis
Returns:
- BOOL — TRUE if the read succeeded; FALSE on failure
ConnID uniqueness validation (ETG.5120 §5.2.3)
An FSoE ConnectionId must be globally unique within a master. dx_fsoe_validate_conn_id / fsoe_validate_conn_id accept only an ID other than 0 that is not already in use; passing 0 to dx_fsoe_init_connection asks the SDK to allocate one (the result is written back to fsoe_config_t.ConnectionId). Higher-level configuration wizards check for a conflict before assigning a new ConnID, so that connection initialization does not fail because the ID is taken.
dx_fsoe_validate_conn_id() / fsoe_validate_conn_id()
BOOL dx_fsoe_validate_conn_id(uint16_t conn_id);
BOOL fsoe_validate_conn_id(uint16_t conn_id); /* SDK-level wrapper with the same semantics */
Validates whether the given ConnID is usable (not held by any active connection and not 0).
Parameters:
- conn_id (uint16_t) — the ConnID to validate
Returns:
- BOOL — TRUE if usable; FALSE if already in use or 0
Example:
/* allocate the next free ConnID automatically */
uint16_t pick_next_conn_id(void)
{
for (uint16_t id = 1; id < 0xFFFF; id++) {
if (dx_fsoe_validate_conn_id(id)) return id;
}
return 0; /* all IDs are in use */
}
uint16_t conn_id = pick_next_conn_id();
if (conn_id == 0) {
printf("无可用 ConnID\n"); /* No ConnID available */
return -1;
}
fsoe_config_t config = {0};
config.ConnectionId = conn_id;
/* ... other parameters */
/* initialize the connection with dx_fsoe_init_connection() or fsoe_manager_add_connection() */
ConnID validation is a read-only operation and can be called from any thread; under concurrent allocation, however, the window between "validate" and "use" can still produce a conflict, so callers should add their own mutex.
FSoE CRC16
fsoe_crc16()
uint16_t fsoe_crc16(const uint8_t* data, uint32_t length);
Computes the FSoE CRC16 (safety polynomial 0x139B7, low 16 bits 0x39B7, initial value 0xFFFF, no reflection, no final XOR); the ConnID is not part of the calculation.
Parameters:
- data (const uint8_t*) — data
- length (uint32_t) — data length
Returns:
- uint16_t — CRC16 value
Example:
uint8_t payload[2] = {0x01, 0x02};
uint16_t crc = fsoe_crc16(payload, sizeof(payload));
fsoe_crc16_with_conn_id()
uint16_t fsoe_crc16_with_conn_id(const uint8_t* data, uint32_t length,
uint16_t connection_id);
CRC16 with a ConnectionId prefix (ETG.5100 §5.5.1): each segment CRC of a standard FSoE PDO frame is computed over the ConnId low and high bytes plus the payload, which binds the frame to a specific connection and prevents crosstalk between connections. Passing connection_id 0 means the ConnID is not included.
Parameters:
- data (const uint8_t*) — segment data
- length (uint32_t) — data length
- connection_id (uint16_t) — FSoE ConnectionId (0 = not included)
Returns:
- uint16_t — CRC16 with the ConnId included
Example:
uint16_t crc = fsoe_crc16_with_conn_id(payload, sizeof(payload), 0x0001);
FSoE PDO frames (slave/fsoe.h)
Build or parse FSoE PDO frame byte streams directly. Typical uses:
- Offline replay analysis of recorded logs (record PDO bytes, then analyze them offline)
- Simulators and test fixtures that build frames by hand
- Self-check scripts that verify whether the frame format sent by the master conforms to ETG.5100
For mid-connection frames (with an advanced sequence number), use the dx_fsoe_* connection APIs to read and write safety data rather than this layer for per-frame verification: this layer assumes first-frame defaults (virtual sequence number 1, previous-frame CRC_0 = 0) and reports a CRC error honestly when a check fails; it never relaxes the predicate.
fsoe_pdo_layout_t
Frame layout description (command byte offset, safety data offset and length, CRC segment offsets, ConnID offset). Both serialization and parsing need a fsoe_pdo_layout_t that describes the byte layout of the target frame.
typedef struct {
uint16_t command_offset; /* Command byte offset */
uint16_t safe_data_offset; /* SafeData start offset */
uint16_t safe_data_length; /* SafeData length (bytes) */
uint16_t crc_segment_count; /* CRC segment count = ceil(SafeData*8/16) */
uint16_t crc_offsets[8]; /* CRC offset of each segment (up to 8 segments, 16 bytes of safety data) */
uint16_t conn_id_offset; /* ConnID byte offset */
uint16_t total_length; /* total frame length */
uint16_t padding_bytes; /* odd-byte padding in the last segment (0/1) */
} fsoe_pdo_layout_t;
fsoe_pdo_frame_t
typedef struct {
uint8_t command; /* command byte */
uint16_t connection_id; /* Connection ID */
uint8_t safe_data[16]; /* safety data (max 16 bytes, agreed with the layout) */
uint16_t crc_segments[8]; /* CRC of each segment */
} fsoe_pdo_frame_t;
fsoe_pdo_frame_to_bytes()
int fsoe_pdo_frame_to_bytes(const fsoe_pdo_frame_t* frame,
const fsoe_pdo_layout_t* layout,
uint8_t* out_buffer, uint32_t out_buffer_size);
Serializes an FSoE PDO frame into a byte array according to layout.
Parameters:
- frame (const fsoe_pdo_frame_t*) — the frame to serialize
- layout (const fsoe_pdo_layout_t*) — target frame layout
- out_buffer (uint8_t*) — output buffer: frame bytes
- out_buffer_size (uint32_t) — buffer capacity
Returns:
- 0 — success
- -1 — invalid parameter (EC_FSOE_ERR_ARG)
- -2 — inconsistent layout (EC_FSOE_ERR_LAYOUT)
- -4 — output buffer too small (EC_FSOE_ERR_BUFFER)
Example:
uint8_t bytes[64];
int rc = fsoe_pdo_frame_to_bytes(&frame, &layout, bytes, sizeof(bytes));
fsoe_pdo_frame_from_bytes()
int fsoe_pdo_frame_from_bytes(const uint8_t* data, uint32_t data_size,
const fsoe_pdo_layout_t* layout,
fsoe_pdo_frame_t* out_frame);
Parses a PDO frame structure from a byte array according to layout; it only parses and does not check the CRC — use fsoe_pdo_frame_validate_crc_segments for that.
Parameters:
- data (const uint8_t*) — frame bytes
- data_size (uint32_t) — byte count
- layout (const fsoe_pdo_layout_t*) — frame layout
- out_frame (fsoe_pdo_frame_t*) — output: parsed frame
Returns:
- 0 — success
- -1 — invalid parameter (EC_FSOE_ERR_ARG)
- -2 — inconsistent layout (EC_FSOE_ERR_LAYOUT)
- -4 — input frame too short (EC_FSOE_ERR_BUFFER)
Example (recorded frame + offline check):
#include "slave/fsoe.h"
uint8_t recorded[64];
int n = read_log_bytes(recorded, sizeof(recorded));
fsoe_pdo_layout_t layout = { /* fill in the offsets for the target device's frame format */ };
fsoe_pdo_frame_t frame;
if (fsoe_pdo_frame_from_bytes(recorded, (uint32_t)n, &layout, &frame) == 0) {
if (!fsoe_pdo_frame_validate_crc_segments(&frame, &layout)) {
printf("CRC 校验失败 (ConnId=0x%04X)\n", frame.connection_id); /* CRC check failed (ConnId=0x%04X) */
} else {
printf("帧合法, ConnId=0x%04X\n", frame.connection_id); /* Frame valid, ConnId=0x%04X */
}
}
fsoe_pdo_frame_validate_crc_segments()
BOOL fsoe_pdo_frame_validate_crc_segments(const fsoe_pdo_frame_t* frame,
const fsoe_pdo_layout_t* layout);
Recomputes the CRC of each segment (derived from connection_id + safe_data) and compares it with crc_segments in the frame.
Parameters:
- frame (const fsoe_pdo_frame_t*) — the frame to verify
- layout (const fsoe_pdo_layout_t*) — frame layout
Returns:
- BOOL — TRUE if all segments match; FALSE if any CRC does not match or the layout / frame is invalid
FSoE manager (ethercat_advanced.h)
Multi-connection lifecycle manager: create it, add configurations per connection, then query status in the cycle loop; it applies fail-safe and reset across all safety connections of one slave together.
fsoe_conn_info_t
typedef struct {
uint16_t connection_id;
uint16_t safety_address;
int state; /* fsoe_state_t */
int last_error; /* fsoe_error_t */
uint32_t error_count;
uint32_t frames_sent;
uint32_t frames_received;
uint32_t crc_errors;
uint32_t watchdog_errors;
uint8_t watchdog_expired;
uint8_t in_failsafe;
} fsoe_conn_info_t;
fsoe_manager_create()
dx_fsoe_manager_t* fsoe_manager_create(uint16_t master, uint16_t slave);
Creates an FSoE manager.
Parameters:
- master (uint16_t) — master index
- slave (uint16_t) — slave index
Returns:
- dx_fsoe_manager_t* — manager instance; release it with fsoe_manager_destroy(); returns NULL if allocation fails
Example:
dx_fsoe_manager_t* mgr = fsoe_manager_create(master, slave);
if (!mgr) return 1;
fsoe_manager_destroy()
void fsoe_manager_destroy(dx_fsoe_manager_t* mgr);
Destroys the manager and closes all of its connections.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
Returns: none
fsoe_manager_add_connection()
int fsoe_manager_add_connection(dx_fsoe_manager_t* mgr,
uint16_t conn_id, uint16_t safety_addr, uint32_t watchdog_ms,
uint16_t in_size, uint16_t out_size,
uint32_t pdo_in_offset, uint32_t pdo_out_offset);
Adds and establishes one FSoE connection. Passing conn_id 0 lets the SDK allocate it; query the actual ID with fsoe_manager_get_info.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
- conn_id (uint16_t) — connection ID (0 = allocated automatically)
- safety_addr (uint16_t) — FSoE slave safety address
- watchdog_ms (uint32_t) — watchdog timeout (ms)
- in_size (uint16_t) — safety input byte count
- out_size (uint16_t) — safety output byte count
- pdo_in_offset (uint32_t) — input offset in the IOmap
- pdo_out_offset (uint32_t) — output offset in the IOmap
Returns:
- int — on success, the connection's index within the manager (starting at 0); on failure, -1 (mgr is NULL, the manager's connection limit is reached, or the connection could not be established)
Example:
int idx = fsoe_manager_add_connection(mgr, 1, 0x0100, 100, 2, 1, 0, 0);
if (idx < 0) printf("连接建立失败\n"); /* Connection establishment failed */
fsoe_manager_close_all()
int fsoe_manager_close_all(dx_fsoe_manager_t* mgr);
Closes all connections inside the manager.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
Returns:
- int — number of connections closed by this call; returns -1 if mgr is NULL
fsoe_manager_reset_all()
int fsoe_manager_reset_all(dx_fsoe_manager_t* mgr);
Resets all connections to RESET and re-runs the handshake.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
Returns:
- int — number of connections reset successfully; returns -1 if mgr is NULL
fsoe_manager_get_info()
int fsoe_manager_get_info(dx_fsoe_manager_t* mgr, int index, fsoe_conn_info_t* info);
Retrieves status information for the given connection.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
- index (int) — connection index (the value returned by fsoe_manager_add_connection)
- info (fsoe_conn_info_t*) — output: connection information
Returns:
- 0 — success, info has been written
- -1 — index out of range or that connection is not initialized
- -2 — status read failed: only connection_id / safety_address in info are trustworthy; the state fields must not be used to declare "safe"
fsoe_manager_connection_count()
int fsoe_manager_connection_count(dx_fsoe_manager_t* mgr);
Returns the number of connections in the manager.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
Returns:
- int — connection count; returns 0 if mgr is NULL
fsoe_manager_all_in_data()
BOOL fsoe_manager_all_in_data(dx_fsoe_manager_t* mgr);
Checks whether all connections are in the DATA state.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
Returns:
- BOOL — TRUE if all are in DATA; FALSE if any connection is not in DATA, there are no connections, or mgr is NULL
Example:
printf("全部 DATA: %s\n", fsoe_manager_all_in_data(mgr) ? "是" : "否"); /* All DATA: %s */
fsoe_manager_any_in_failsafe()
BOOL fsoe_manager_any_in_failsafe(dx_fsoe_manager_t* mgr);
Checks whether any connection is in the FAILSAFE state. The safety semantics are conservative: if the state of any established connection cannot be read, it is treated as unsafe and TRUE is returned.
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
Returns:
- BOOL — TRUE if a connection is in FAILSAFE (or its state cannot be trusted); FALSE if none are in FAILSAFE
fsoe_manager_enter_all_failsafe()
int fsoe_manager_enter_all_failsafe(dx_fsoe_manager_t* mgr);
Puts all connections into the FAILSAFE state (for emergency stop).
Parameters:
- mgr (dx_fsoe_manager_t*) — manager instance
Returns:
- int — number of connections that entered FAILSAFE successfully; returns -1 if mgr is NULL
Example:
fsoe_manager_enter_all_failsafe(mgr);
Full example
Single-connection safety IO
#include "ethercat.h"
#include "slave/fsoe.h"
#include <stdio.h>
int main(void)
{
uint16_t master = Initialize();
if (master == 0 || master == (uint16_t)-1) return 1;
SetNetwork(master, "\\Device\\NPF_{...}", "");
SetStateSequence(master, EC_STATE_OPERATIONAL, 10000);
Start(master);
uint16_t slave = 1;
if (!dx_fsoe_is_slave_capable(master, slave)) {
printf("该从站不支持 FSoE\n"); /* This slave does not support FSoE */
Dispose(master);
return 1;
}
fsoe_config_t cfg = {
.ConnectionId = 1, .SafetyAddress = 0x0100, .WatchdogTimeMs = 100,
.SafeInputSize = 2, /* safety input byte count, matching the application-side structure */
.SafeOutputSize = 1, /* safety output byte count */
.PdoInputOffset = 0, .PdoOutputOffset = 0
};
if (!dx_fsoe_init_connection(master, slave, &cfg)) {
printf("安全连接建立失败\n"); /* Failed to establish the safety connection */
Dispose(master);
return 1;
}
uint8_t safe_in[2] = {0};
uint8_t safe_out[1] = {0};
/* preset the fail-safe value: the slave outputs it on watchdog timeout or when entering FAILSAFE */
if (!dx_fsoe_set_failsafe_output(master, slave, safe_out, sizeof(safe_out))) {
printf("失效安全值设置失败\n"); /* Failsafe value setup failed */
dx_fsoe_reset(master, slave);
Dispose(master);
return 1;
}
/* the master advances RESET -> SESSION -> CONNECTION -> PARAMETER -> DATA;
call dx_fsoe_download_parameters first when the device requires safety parameters */
for (int cycle = 0; cycle < 1000; cycle++) {
uint32_t in_size = sizeof(safe_in); /* reset the available capacity every cycle */
int state = dx_fsoe_get_state(master, slave);
if (state == FSOE_STATE_FAILSAFE) {
/* FAILSAFE only returns to DATA or RESET on an explicit request */
dx_fsoe_request_state(master, slave, FSOE_STATE_DATA);
continue;
}
if (state != FSOE_STATE_DATA)
continue; /* the handshake is still in progress; no data is exchanged this cycle */
if (!dx_fsoe_check_watchdog(master, slave)) {
printf("看门狗超时, 输出由从站切到失效安全值\n"); /* Watchdog timeout, the slave switched its outputs to the failsafe value */
continue;
}
if (dx_fsoe_read_safe_input(master, slave, safe_in, &in_size)) {
safe_out[0] = (safe_in[0] & 0x01) ? 0x01 : 0x00;
if (!dx_fsoe_write_safe_output(master, slave, safe_out, sizeof(safe_out)))
printf("安全输出写入失败\n"); /* Safety output write failed */
}
}
dx_fsoe_reset(master, slave);
Dispose(master);
return 0;
}
Multi-connection manager
#include "ethercat.h"
#include "ethercat_advanced.h"
/* create the FSoE manager */
dx_fsoe_manager_t* mgr = fsoe_manager_create(master, slave);
/* add two safety connections (passing conn_id 0 lets the SDK allocate them) */
fsoe_manager_add_connection(mgr, 1, 0x0100, 100, 2, 1, 0, 0);
fsoe_manager_add_connection(mgr, 2, 0x0200, 100, 2, 1, 2, 1);
/* check status */
printf("连接数: %d\n", fsoe_manager_connection_count(mgr)); /* Connection count: %d */
printf("全部 DATA: %s\n", fsoe_manager_all_in_data(mgr) ? "是" : "否"); /* All DATA: %s */
printf("有失效安全: %s\n", fsoe_manager_any_in_failsafe(mgr) ? "是" : "否"); /* Any in failsafe: %s */
/* read connection information */
for (int i = 0; i < fsoe_manager_connection_count(mgr); i++) {
fsoe_conn_info_t info;
if (fsoe_manager_get_info(mgr, i, &info) == 0) {
printf("连接 %d: 状态=%d, 地址=0x%04X, CRC错误=%u\n", /* Connection %d: state=%d, address=0x%04X, CRC errors=%u */
i, info.state, info.safety_address, info.crc_errors);
}
}
/* emergency: put all connections into fail-safe */
fsoe_manager_enter_all_failsafe(mgr);
/* reset all connections */
fsoe_manager_reset_all(mgr);
/* close and destroy */
fsoe_manager_close_all(mgr);
fsoe_manager_destroy(mgr);
Failures and handling
| Symptom | Cause | Handling |
|---|---|---|
| The connection never reaches data exchange for a long time | The safety address does not match the device DIP switch setting, the safety data byte counts do not match the device definition, or the connection stays in PARAMETER because safety parameters were not downloaded | Check SafetyAddress against the DIP switch setting; re-check SafeInputSize / SafeOutputSize against the device manual; call dx_fsoe_download_parameters first when the device requires parameters |
| dx_fsoe_is_slave_capable returns false before the connection is established | The slave does not declare FSoE, or the capability object cannot be read | Use a slave that supports FSoE; do not judge capability from the return value of init_connection |
| The watchdog check returns false and the outputs switch to the safety values | The watchdog timed out and safety data exchange was interrupted | First confirm the PDO cycle is running and the slave has not dropped out of OP, then call dx_fsoe_reset to re-run the handshake |
| Connection setup fails with a connection ID conflict | That ConnectionId is already held by another safety connection | Use a free ConnectionId, or leave it as 0 and let the SDK allocate one |
| The fail-safe state does not clear automatically after triggering | The fault source is still present and the safety connection stays in the fail-safe state | Remove the fault source, then call dx_fsoe_reset to rebuild the connection; if the device does not allow recovery, follow the safety procedure |