CiA 401 -- Generic I/O modules
CiA 401 is a generic digital and analog I/O device profile built on CoE.
CiA 401 runs on top of CoE (SDO) and is available only when the slave supports CoE. Determine the capability by checking whether the tri-state query GetSlaveProtoSupportState(master, slave, MBXPROT_COE) equals EC_PROTO_SUPPORT_YES; GetSlaveCoEDetails(master, slave) is a cached SII value that may also read 0, so it cannot by itself justify a "not supported" conclusion (see Protocol capability tri-state).
IO attributes
A CiA 401 device has 4 standard IO categories, each accessed through an SDO index and a PDO offset.
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| DI | 0x6000 / 0x6001 / 0x6020 | UINT8 / UINT16 / UINT32 | read | Digital input; the subindex identifies the channel (1-based), 1 bit per channel |
| DO | 0x6200 / 0x6201 / 0x6220 | UINT8 / UINT16 / UINT32 | read/write | Digital output; the subindex identifies the channel |
| AI | 0x6400 (alias 0x6401) | INT16 | read | Analog input; use cia401_read_ai_unsigned() for an unsigned read |
| AO | 0x6411 / 0x6412 | INT16 / INT32 | read/write | Analog output |
PDO data access
CiA 401 digital and analog IO data is exchanged through PDO mapping. The exact offsets depend on the slave's PDO mapping configuration. The recommended path is to call GetIO once after entering OP to establish a direct mapping: with the live IOmap pointer, take the channels from the packed structure as documented in the slave manual, and the next read after a bus refresh returns fresh values. CopySlaveInputPdo is a snapshot (not a direct mapping); use WriteSlaveOutputPdo to write a whole frame (the complete frame of a single real-time owner), and use CopySlaveOutputPdoVersioned / CommitSlaveOutputPdoVersioned for partial updates.
int32_t out_size = 0, in_size = 0;
uint8_t* out_ptr = NULL;
uint8_t* in_ptr = NULL;
if (ec_pdo_io_is_ok(GetIO(master, slave, &out_size, &out_ptr, &in_size, &in_ptr))
&& in_ptr && out_ptr) {
uint8_t di = in_ptr[0]; /* digital input; the offset depends on the mapping */
int16_t ai;
memcpy(&ai, in_ptr + 2, 2); /* analog input example */
out_ptr[0] = 0xFF; /* write the output frame according to the mapping */
}
Use PDO mapping rather than SDO for high-frequency I/O control. SDO is suited to parameter configuration and diagnostics.
Convenience function API (slave/cia401.h)
slave/cia401.h provides a set of plain C functions that access channels directly (no handle, no function-pointer table): pass the channel number to read or write the corresponding SDO bit or word. Reads come in two forms: bit fields and 16-bit unsigned fields return int (a failed read returns DARRA_READ_FAIL_INT = -1, because 0 and 0xFFFF are both valid values in those fields); analog values and 32-bit fields return BOOL and fill an output parameter (a failure only reports FALSE and leaves the output parameter untouched). Writes return BOOL, and only TRUE means the write succeeded.
cia401_read_di / cia401_write_do
int cia401_read_di(uint16_t master, uint16_t slave, int channel);
int cia401_read_do(uint16_t master, uint16_t slave, int channel);
BOOL cia401_write_do(uint16_t master, uint16_t slave, int channel, uint8_t value);
Read a digital input, read back a digital output, or write a digital output by channel (starting at 0). The write is a read-modify-write and does not affect other channels in the same group.
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexchannel(int) — channel number (starting at 0)value(uint8_t) — target level (1 = high, 0 = low; cia401_write_do only)
Return value:
cia401_read_di/cia401_read_do→int— channel level (1/0); a failed read returns -1 (which is not a low level)cia401_write_do→BOOL— TRUE = write succeeded; FALSE = failure (including a failed baseline read in the read-modify-write, in which case the other channels in the group are left untouched)
cia401_write_virtual_di
BOOL cia401_write_virtual_di(uint16_t master, uint16_t slave, int diIndex, uint8_t value);
Writes a virtual DI command (0x2120:01–04). The object is usually provided through PDO (a slave rejects an SDO write with 0x06020000), so the function tries SDO first and automatically falls back to a PDO write on failure; it returns FALSE and reports diagnostics only when both paths fail.
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexdiIndex(int) — virtual DI subindex (1–4 maps to 0x2120:01–04)value(uint8_t) — target state (1 = high level, 0 = low level)
Return value:
BOOL— TRUE = success (either the SDO or the PDO path worked); FALSE = both paths failed (including 0x2120 not being mapped into the RxPDO)
Example:
if (!cia401_write_virtual_di(master, slave, 1, 1))
printf("虚拟 DI 写入失败\n"); /* Virtual DI write failed */
cia401_read_ai / cia401_write_ao
BOOL cia401_read_ai(uint16_t master, uint16_t slave, int channel, int16_t* out_value);
int cia401_read_ai_unsigned(uint16_t master, uint16_t slave, int channel);
BOOL cia401_read_ai32(uint16_t master, uint16_t slave, int channel, int32_t* out_value);
BOOL cia401_read_ai32_unsigned(uint16_t master, uint16_t slave, int channel, uint32_t* out_value);
BOOL cia401_read_ao(uint16_t master, uint16_t slave, int channel, int16_t* out_value);
BOOL cia401_write_ao(uint16_t master, uint16_t slave, int channel, int16_t value);
Read an analog input, read back an analog output, or write a 16-bit analog output by channel. cia401_read_ai32 adapts the return to 16- or 32-bit values.
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexchannel(int) — channel number (starting at 0)out_value— output parameter, filled with the value read on success (not written on failure)value(int16_t) — analog output value to write
Return value:
cia401_read_ai/cia401_read_ai32/cia401_read_ai32_unsigned/cia401_read_ao→BOOL— TRUE = the real value was filled in; FALSE = read failed (the output parameter is not written)cia401_read_ai_unsigned→int— unsigned 16-bit analog value (0–65535); a failed read returns -1cia401_write_ao→BOOL— TRUE = write succeeded
32-bit analog output
BOOL cia401_read_ao32(uint16_t master, uint16_t slave, int channel, int32_t* out_value);
BOOL cia401_write_ao32(uint16_t master, uint16_t slave, int channel, int32_t value);
Read and write the 32-bit analog output (0x6412).
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexchannel(int) — channel number (starting at 0)out_value(int32_t*) — output parameter, filled on success (not written on failure)value(int32_t) — analog output value to write
Return value:
BOOL— for reads, TRUE = the real value was filled in / FALSE = read failed; for writes, TRUE = write succeeded / FALSE = failure
Analog input interrupts and limits (slave/cia401.h)
cia401_get/set_global_interrupt_enable
int cia401_get_global_interrupt_enable(uint16_t master, uint16_t slave); /* 0x6423 */
BOOL cia401_set_global_interrupt_enable(uint16_t master, uint16_t slave, BOOL enabled);
Global interrupt enable for analog inputs (SDO 0x6423).
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexenabled(BOOL) — TRUE enables / FALSE disables (setter only)
Return value:
cia401_get_global_interrupt_enable→int— current enable state:1= enabled,0= disabled (0 is a legal value); a failed read returnsDARRA_READ_FAIL_INT(-1), test withDARRA_IS_READ_FAIL_INT(v)cia401_set_global_interrupt_enable→BOOL— TRUE = write succeeded
Description: A global interrupt that is "disabled" and a "read failure" are two different things. Reading 0 means the interrupt really is off (a legal state); only a true DARRA_IS_READ_FAIL_INT(v) means nothing was read — never fold a failure into 0 and treat it as "disabled".
cia401_get/set_ai_upper_limit
BOOL cia401_get_ai_upper_limit(uint16_t master, uint16_t slave, int channel, int16_t* out_value); /* 0x6424 */
BOOL cia401_set_ai_upper_limit(uint16_t master, uint16_t slave, int channel, int16_t value);
Set or read the channel upper-limit warning value (SDO 0x6424, signed 16-bit); an interrupt is triggered when the AI value exceeds the upper limit.
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexchannel(int) — channel number (starting at 0)out_value(int16_t*) — output parameter, filled with the upper limit on success (not written on failure)value(int16_t) — upper limit value to write
Return value:
BOOL— for reads, TRUE = the real value was filled in / FALSE = read failed; for writes, TRUE = write succeeded / FALSE = failure
cia401_get/set_ai_lower_limit
BOOL cia401_get_ai_lower_limit(uint16_t master, uint16_t slave, int channel, int16_t* out_value); /* 0x6425 */
BOOL cia401_set_ai_lower_limit(uint16_t master, uint16_t slave, int channel, int16_t value);
Set or read the channel lower-limit warning value (SDO 0x6425, signed 16-bit); an interrupt is triggered when the AI value drops below the lower limit.
Parameters:
- Same as the upper-limit functions (
out_valueis filled with the lower limit /valueis the lower limit to write)
Return value:
BOOL— for reads, TRUE = the real value was filled in / FALSE = read failed; for writes, TRUE = write succeeded / FALSE = failure
Bulk IO functions (slave/cia401.h)
For the functions below, the group parameter is the SDO subindex (1-based).
int cia401_read_di16(uint16_t master, uint16_t slave, uint8_t group); /* 0x6001 */
BOOL cia401_read_di32(uint16_t master, uint16_t slave, uint8_t group, uint32_t* out_value); /* 0x6020 */
int cia401_read_do16(uint16_t master, uint16_t slave, uint8_t group); /* 0x6201 */
BOOL cia401_write_do16(uint16_t master, uint16_t slave, uint8_t group, uint16_t value);
BOOL cia401_read_do32(uint16_t master, uint16_t slave, uint8_t group, uint32_t* out_value); /* 0x6220 */
BOOL cia401_write_do32(uint16_t master, uint16_t slave, uint8_t group, uint32_t value);
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexgroup(uint8_t) — group number (SDO subindex, starting at 1)out_value— output parameter, filled with the 32-bit value on success (not written on failure)value— 16- or 32-bit output pattern to write
Return value:
cia401_read_di16/cia401_read_do16→int— 16-bit pattern (0–65535); a failed read returns -1 (both 0 and 0xFFFF are valid patterns and cannot serve as a sentinel)cia401_read_di32/cia401_read_do32→BOOL— TRUE = the real value was filled in; FALSE = read failedcia401_write_do16/cia401_write_do32→BOOL— TRUE = write succeeded
Digital input configuration (slave/cia401.h)
int cia401_get_di_polarity(uint16_t master, uint16_t slave, uint8_t group); /* 0x6002 */
BOOL cia401_set_di_polarity(uint16_t master, uint16_t slave, uint8_t group, uint8_t polarity);
int cia401_get_di_filter(uint16_t master, uint16_t slave, uint8_t group); /* 0x6003 */
BOOL cia401_set_di_filter(uint16_t master, uint16_t slave, uint8_t group, uint8_t filter);
Set or read the digital input polarity (0x6002, 1 = inverted per bit) and the filter enable (0x6003).
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexgroup(uint8_t) — group number (SDO subindex, starting at 1)polarity/filter(uint8_t) — polarity / filter pattern to write
Return value:
cia401_get_di_polarity/cia401_get_di_filter→int— current pattern (true value range 0–65535, a 16-bit mask); a failed read returnsDARRA_READ_FAIL_INT(-1), test withDARRA_IS_READ_FAIL_INT(v)cia401_set_di_polarity/cia401_set_di_filter→BOOL— TRUE = write succeeded
Both getters return int rather than uint16_t specifically to leave an out-of-domain value available as the failure sentinel:
- The true value range is 0–65535.
0is a legal value (all channels not inverted / no filtering), not an error, and not "nothing was read". 0xFFFFis also a legal mask (all inverted / all filtered) and cannot be used as the sentinel — that is why a failure is not folded into0, and why the out-of-domain value-1is used instead.- Always test with
DARRA_IS_READ_FAIL_INT(v). Do not cast to(uint16_t)before testing: the cast turns-1into0xFFFF, which is then indistinguishable from a legal mask.
Example:
int pol = cia401_get_di_polarity(m, 1, 1);
if (DARRA_IS_READ_FAIL_INT(pol))
printf("读失败 —— 这不是「全部不反转」\n"); /* a failure must not be used as 0 */
else
printf("极性掩码 = 0x%04X\n", (unsigned)pol); /* both 0 and 0xFFFF are real values */
Error status readback (slave/cia401.h)
group semanticsFor the DO error functions in this section, group starts at 0 (1 is added internally to form the SDO subindex, covering DO channels 0-7, 8-15, ...); this differs from the 1-based group of the bulk IO functions above (used directly as the subindex), so mixing the two shifts everything by one group.
/* digital output error mode (0 = hold, 1 = safe value) / error value */
int cia401_get_do_error_mode(uint16_t master, uint16_t slave, uint8_t group); /* 0x6206 */
BOOL cia401_set_do_error_mode(uint16_t master, uint16_t slave, uint8_t group, uint8_t mode);
int cia401_get_do_error_value(uint16_t master, uint16_t slave, uint8_t group); /* 0x6207 */
BOOL cia401_set_do_error_value(uint16_t master, uint16_t slave, uint8_t group, uint8_t value);
/* analog output error mode / error value */
int cia401_get_ao_error_mode(uint16_t master, uint16_t slave, int channel); /* 0x6443 */
BOOL cia401_set_ao_error_mode(uint16_t master, uint16_t slave, int channel, uint8_t mode);
BOOL cia401_get_ao_error_value(uint16_t master, uint16_t slave, int channel, int16_t* out_value); /* 0x6444 */
BOOL cia401_set_ao_error_value(uint16_t master, uint16_t slave, int channel, int16_t value);
Parameters:
master(uint16_t) — master indexslave(uint16_t) — slave indexgroup(uint8_t) — DO group number (starting at 0; see thegroupsemantics above)channel(int) — AO channel number (starting at 0)mode(uint8_t) — error mode (CIA401_ERROR_MODE_HOLD/CIA401_ERROR_MODE_SAFE_VALUE)value/out_value— error value (a uint8 pattern for DO; an int16 for AO, filled through the output parameter on a read)
Return value:
cia401_get_do_error_mode/cia401_get_do_error_value/cia401_get_ao_error_mode→int— current value; a failed read returns -1cia401_get_ao_error_value→BOOL— TRUE = the real value was filled in; FALSE = read failed- All setters →
BOOL— TRUE = write succeeded
Error handling
DOErrorMode
Digital output error mode (SDO 0x6206). 0 = hold the last value, 1 = switch to the safe value.
#define CIA401_ERROR_MODE_HOLD 0
#define CIA401_ERROR_MODE_SAFE_VALUE 1
DOErrorValue
Digital output error value (SDO 0x6207). When the error mode is 1, this value is used as the safe output.
AOErrorMode
Analog output error mode (SDO 0x6443), with the same semantics as DOErrorMode.
AOErrorValue
Analog output error value (SDO 0x6444). When the error mode is 1, this value is used as the safe output.
Standard object index constants
Object index macros defined in slave/cia401.h (note the CIA401_OD_ prefix):
| Constant | Value | Description |
|---|---|---|
| CIA401_OD_DI | 0x6000 | Digital input (8-bit group) |
| CIA401_OD_VIRTUAL_DI | 0x2120 | Virtual digital input (for cia401_write_virtual_di only) |
| CIA401_OD_DI_16BIT | 0x6001 | 16-bit digital input group |
| CIA401_OD_DI_POLARITY | 0x6002 | Digital input polarity |
| CIA401_OD_DI_FILTER | 0x6003 | Digital input filter enable |
| CIA401_OD_DI_INTERRUPT | 0x6005 | Digital input interrupt trigger |
| CIA401_OD_DI_INTERRUPT_EDGE | 0x6006 | Digital input interrupt edge |
| CIA401_OD_DI_32BIT | 0x6020 | 32-bit digital input group |
| CIA401_OD_DO | 0x6200 | Digital output (8-bit group) |
| CIA401_OD_DO_16BIT | 0x6201 | 16-bit digital output group |
| CIA401_OD_DO_POLARITY | 0x6202 | Digital output polarity |
| CIA401_OD_DO_ERROR_MODE | 0x6206 | Digital output error mode |
| CIA401_OD_DO_ERROR_VALUE | 0x6207 | Digital output error value |
| CIA401_OD_DO_32BIT | 0x6220 | 32-bit digital output group |
| CIA401_OD_AI | 0x6400 | Analog input (16-bit signed) |
| CIA401_OD_AI_16BIT | 0x6401 | Analog input 16-bit (alias) |
| CIA401_OD_AI_SI_UNIT | 0x6420 | Analog input range |
| CIA401_OD_AI_GLOBAL_INTERRUPT | 0x6423 | Analog input global interrupt enable |
| CIA401_OD_AI_UPPER_LIMIT | 0x6424 | Analog input interrupt upper limit |
| CIA401_OD_AI_LOWER_LIMIT | 0x6425 | Analog input interrupt lower limit |
| CIA401_OD_AO | 0x6411 | Analog output (16-bit signed) |
| CIA401_OD_AO_32BIT | 0x6412 | 32-bit analog output |
| CIA401_OD_AO_SI_UNIT | 0x6430 | Analog output range |
| CIA401_OD_AO_ERROR_MODE | 0x6443 | Analog output error mode |
| CIA401_OD_AO_ERROR_VALUE | 0x6444 | Analog output error value |
Error mode enum macros: CIA401_ERROR_MODE_HOLD (0) / CIA401_ERROR_MODE_SAFE_VALUE (1).
Typed SDO read and write
ethercat_advanced.h provides typed SDO wrappers:
#include "ethercat_advanced.h"
uint8_t polarity;
sdo_read_u8(master, 1, CIA401_OD_DI_POLARITY, 0x01, &polarity);
int16_t ai;
sdo_read_i16(master, 1, CIA401_OD_AI, 0x01, &ai);
sdo_write_i16(master, 1, CIA401_OD_AO, 0x01, 16384);
Full example
#include "ethercat.h"
#include "ethercat_advanced.h"
#include <stdio.h>
int main(void) {
uint16_t master = Initialize();
if (master == 0 || master == (uint16_t)-1) return 1; /* returns (uint16_t)-1 on failure */
uint16_t slaves_found = SetNetwork(master, "\\Device\\NPF_{GUID}", "");
if (slaves_found == 0 || slaves_found == (uint16_t)-1) return 1; /* > 0 = number of slaves found by the scan */
if (!SetStateSequence(master, EC_STATE_OPERATIONAL, 10000)) return 1;
if (!Start(master)) return 1;
uint16_t slave = 1;
/* PDO digital / analog IO: call GetIO once after entering OP to establish the direct mapping */
int32_t out_size = 0, in_size = 0;
uint8_t* out_ptr = NULL;
uint8_t* in_ptr = NULL;
if (ec_pdo_io_is_ok(GetIO(master, slave, &out_size, &out_ptr, &in_size, &in_ptr))
&& in_ptr && out_ptr) {
printf("DI=0x%02X\n", in_ptr[0]);
out_ptr[0] = 0xFF; /* write the output frame according to the mapping */
}
/* safe output on communication loss: a failed write must be visible, otherwise the old mode stays in effect on a fault */
if (!cia401_set_do_error_mode(master, slave, 0, CIA401_ERROR_MODE_SAFE_VALUE) ||
!cia401_set_do_error_value(master, slave, 0, 0x00))
printf("DO 安全值写入失败\n"); /* DO safe value write failed */
/* analog input warnings */
if (!cia401_set_global_interrupt_enable(master, slave, TRUE) ||
!cia401_set_ai_upper_limit(master, slave, 0, 30000) ||
!cia401_set_ai_lower_limit(master, slave, 0, 1000))
printf("AI 告警配置写入失败\n"); /* AI alarm configuration write failed */
getchar();
Stop(master);
Dispose(master);
return 0;
}