CiA 401 — generic I/O modules
CiA 401 is the CoE-based protocol for generic digital/analog I/O devices.
CiA 401 runs over CoE (SDO) and is available only when the slave supports CoE.
Create an instance with CiA401(coe), passing a CoE& reference. Use coe.IsCiA401() to check whether the slave is a CiA 401 device.
Quick start
auto& coe = master.GetSlave(1).GetCoE();
if (coe.IsCiA401()) {
CiA401 io(coe);
// Digital IO
bool sensor = io.ReadDI(0);
io.WriteDO(0, true);
io.WriteDO(1, sensor);
// Analog IO
int32_t adc = io.ReadAI(0);
io.WriteAO(0, 16384);
}
For high-frequency I/O control use PDO mapping rather than SDO. SDO is suited to parameter configuration and diagnostics.
Channel and numbering conventions
- Channel number
channel: starts at 0; the group number and bit offset are derived automatically in groups of 8 channels. - Group number
group(bulk functions): used directly as the SDO subindex, usually starting at 1; passing 0 reads the number of entries, not group 1. - Byte group number
byteIndex: used directly as the subindex, starting at 1. groupin the error-state functions: starts at 0 (internally +1 becomes the subindex); this is not the same numbering as the bulk functions, and mixing them shifts by one group.
Digital inputs
ReadDI()
bool ReadDI(int channel) const;
Reads a digital input. The group number and bit offset are derived internally in groups of 8 channels.
Parameters:
channel(int) — channel number (starts at 0)
Returns:
bool— channel state (true = high). A failed read returnsfalse(indistinguishable from low); to confirm whether the read succeeded, usecoe.SDOReadU8(CiA401Obj::DI, group)and check thestd::optional
ReadDIByte()
uint8_t ReadDIByte(int byteIndex) const;
Reads one 8-bit digital input group.
Parameters:
byteIndex(int) — byte group number (subindex, starts at 1)
Returns:
uint8_t— 8-bit input value. A failed read falls back to0(indistinguishable from a real 0); to confirm the read, usecoe.SDOReadU8and check thestd::optional
ReadAllDI()
std::vector<uint8_t> ReadAllDI() const;
Reads all 8-bit digital input groups (up to 32 groups).
Returns:
std::vector<uint8_t>— input values per group. The group count is taken from0x6000:00; when the count cannot be read or is out of range (< 1 or > 32) it returns 4 groups, and a failed single-group read is filled with0
ReadDI16()
uint16_t ReadDI16(int group) const;
Reads 16-bit digital inputs (16 channels, 0x6001).
Parameters:
group(int) — group number, used directly as the SDO subindex (starts at 1; group N covers the 16 channels starting at channel (N-1)*16)
Returns:
uint16_t— 16-bit input value; a failed read falls back to0
ReadDI32()
uint32_t ReadDI32(int group) const;
Reads 32-bit digital inputs (32 channels, 0x6020).
Parameters:
group(int) — group number, used directly as the SDO subindex (starts at 1; group N covers the 32 channels starting at channel (N-1)*32)
Returns:
uint32_t— 32-bit input value; a failed read falls back to0
Example:
CiA401 io(coe);
// Per-channel reads
bool ch0 = io.ReadDI(0);
bool ch5 = io.ReadDI(5);
// 16/32-bit batch read
uint16_t di16 = io.ReadDI16(1); // Channels 0-15
uint32_t di32 = io.ReadDI32(1); // Channels 0-31
bool ch5_alt = (di16 & (1 << 5)) != 0;
Digital outputs
WriteDO()
bool WriteDO(int channel, bool value) const;
Sets a digital output without affecting the other channels in the same group (read-modify-write internally).
On a failed read it writes nothing and returns false directly (the other channels in the group keep their values) — it does not write back from a 0 baseline and clear the other outputs in the group.
Parameters:
channel(int) — channel number (starts at 0)value(bool) — target state (true = high)
Returns:
bool— whether it succeeded; a failed read (nothing written, the group keeps its values) and a failed write both returnfalse
WriteVirtualDI()
bool WriteVirtualDI(int diIndex, bool value) const;
Writes a virtual DI command (0x2120:0x). This object must go through PDO: an SDO write is rejected by the slave (typically abort 0x06020000), in which case it falls back to a PDO write automatically and logs it.
Parameters:
diIndex(int) — virtual DI subindex (starting at 1, e.g. 1 → 0x2120:01)value(bool) — target state (true = 1)
Returns:
bool—true= the write succeeded (SDO or PDO);false= both SDO and PDO failed (including the case where 0x2120 has no PDO mapping — map 0x2120 into the RxPDO and retry)
WriteDOByte()
bool WriteDOByte(int byteIndex, uint8_t value) const;
Writes one 8-bit digital output group.
Parameters:
byteIndex(int) — byte group number (subindex, starts at 1)value(uint8_t) — 8-bit output value
Returns:
bool— whether the write succeeded
WriteAllDO()
bool WriteAllDO(const std::vector<uint8_t>& data) const;
Writes all digital output groups.
Parameters:
data— output values per group (std::vector<uint8_t>);data[i]is written to0x6200:(i+1)
Returns:
bool—truewhen every group was written;falsewhen any group failed (groups already written keep their written values)
ReadDO16() / WriteDO16()
uint16_t ReadDO16(int group) const;
bool WriteDO16(int group, uint16_t value) const;
Reads or writes 16-bit digital outputs (16 channels, 0x6201).
Parameters:
group(int) — group number, used directly as the SDO subindex (starts at 1)value(uint16_t) — 16-bit output value
Returns:
uint16_t— 16-bit output value; a failed read falls back to0bool— whether the write succeeded
ReadDO32() / WriteDO32()
uint32_t ReadDO32(int group) const;
bool WriteDO32(int group, uint32_t value) const;
Reads or writes 32-bit digital outputs (32 channels, 0x6220).
Parameters:
group(int) — group number, used directly as the SDO subindex (starts at 1)value(uint32_t) — 32-bit output value
Returns:
uint32_t— 32-bit output value; a failed read falls back to0bool— whether the write succeeded
Example:
CiA401 io(coe);
// Per-channel operations
io.WriteDO(0, true);
io.WriteDO(1, false);
// Virtual DI (must go through the PDO)
io.WriteVirtualDI(1, true);
// 16-bit batch operations
io.WriteDO16(1, 0x00FF); // Channels 0-7 high, 8-15 low
uint16_t do16 = io.ReadDO16(1); // Read the current value back
// 32-bit batch operations
io.WriteDO32(1, 0x0000FFFF); // Channels 0-15 high
uint32_t do32 = io.ReadDO32(1); // Read the current value back
Analog inputs
ReadAI()
int32_t ReadAI(int channel) const;
Reads an analog input value. Adapts automatically to 16-bit and 32-bit devices (0x6400).
Parameters:
channel(int) — channel number (starts at 0; internally +1 becomes the subindex)
Returns:
int32_t— analog value (signed). A failed read falls back to0(indistinguishable from a real 0); to confirm the read, usecoe.SDOReadI16/coe.SDOReadU16and check thestd::optional
ReadAIRaw()
uint16_t ReadAIRaw(int channel) const;
Reads the raw analog input value (unsigned 16-bit).
Parameters:
channel(int) — channel number (starts at 0)
Returns:
uint16_t— raw value; a failed read falls back to0
ReadAI32Unsigned()
uint32_t ReadAI32Unsigned(int channel) const;
Reads an unsigned 32-bit analog input.
Parameters:
channel(int) — channel number (starts at 0)
Returns:
uint32_t— unsigned 32-bit value; a failed read falls back to0
Example:
CiA401 io(coe);
int32_t adc = io.ReadAI(0);
double voltage = adc * 10.0 / 32767.0;
printf("电压: %.3f V\n", voltage); // Voltage: %.3f V
Analog outputs
WriteAO()
bool WriteAO(int channel, int16_t value) const;
Writes a 16-bit analog output value (0x6411).
Parameters:
channel(int) — channel number (starts at 0; internally +1 becomes the subindex)value(int16_t) — 16-bit output value
Returns:
bool— whether the write succeeded
WriteAORaw()
bool WriteAORaw(int channel, uint16_t value) const;
Writes the raw analog output value (unsigned 16-bit).
Parameters:
channel(int) — channel number (starts at 0)value(uint16_t) — unsigned 16-bit output value
Returns:
bool— whether the write succeeded
ReadAO()
int16_t ReadAO(int channel) const;
Reads a 16-bit analog output value.
Parameters:
channel(int) — channel number (starts at 0)
Returns:
int16_t— output value; a failed read falls back to0
ReadAO32() / WriteAO32()
int32_t ReadAO32(int channel) const;
bool WriteAO32(int channel, int32_t value) const;
Reads or writes a 32-bit analog output value (0x6412). For high-precision analog output devices.
Parameters:
channel(int) — channel number (starts at 0)value(int32_t) — 32-bit output value
Returns:
int32_t— output value; a failed read falls back to0bool— whether the write succeeded
Example:
CiA401 io(coe);
// 16-bit analog output
io.WriteAO(0, 16384);
int16_t ao = io.ReadAO(0);
// 32-bit analog output (high-precision devices)
io.WriteAO32(0, 65536);
int32_t ao32 = io.ReadAO32(0);
Digital input configuration
GetDIPolarity() / SetDIPolarity()
uint8_t GetDIPolarity(int group) const;
bool SetDIPolarity(int group, uint8_t polarity) const;
Reads or sets the digital input polarity (0x6002). The input signal logic can be inverted per bit.
Parameters:
group(int) — group number (subindex, starts at 1)polarity(uint8_t) — polarity mask, 1 = invert the corresponding channel
Returns:
uint8_t— polarity mask; a failed read falls back to0bool— whether the write succeeded
Example:
CiA401 io(coe);
io.SetDIPolarity(1, 0x0F); // Invert the polarity of channels 0-3
uint8_t pol = io.GetDIPolarity(1); // Read the current polarity configuration
TryGetDIPolarity()
std::optional<uint8_t> TryGetDIPolarity(int group) const;
Reads the digital input polarity (0x6002) and keeps "could not read" apart from a real value of 0.
GetDIPolarity() folds a failed SDO read into the legal value 0 through SDOReadU8(...).value_or(0), so a failed read and a polarity that really is all 0 are indistinguishable. This entry point returns std::optional: std::nullopt means the SDO read failed, while 0 is a genuine device value. The old GetDIPolarity() entry point keeps its exact behaviour (backward compatible).
Use it when a configuration read-back, a diagnostic or an interlock decision must tell "could not read" apart from when it really is 0; do not treat nullopt as 0.
Parameters:
group(int) — group number (subindex, starts at 1)
Returns:
std::optional<uint8_t>— the polarity mask (one bit per channel, 1 = invert);std::nullopt= the SDO read failed
Do not treat std::nullopt as 0; a read that never succeeded says nothing about the device.
Example:
CiA401 io(coe);
if (auto pol = io.TryGetDIPolarity(1)) {
printf("极性掩码 = 0x%02X\n", *pol); // The truth value, which can be 0
} else {
printf("SDO 读失败, 不要当作极性全 0\n"); // SDO read failed — do not read this as "all polarity bits 0"
}
GetDIPolarityChannel() / SetDIPolarityChannel()
bool GetDIPolarityChannel(int channel) const;
bool SetDIPolarityChannel(int channel, bool inverted) const;
Reads or writes the input polarity per channel (0x6002): internally channel / 8 + 1 gives the group number and channel % 8 gives the bit, so only the target channel's bit is touched.
Parameters:
channel(int) — channel number (starts at 0)inverted(bool) —true= invert this channel,false= normal
Returns:
bool(read) —true= this channel is inverted,false= normal. A failed read also returnsfalse, indistinguishable from "normal"; to tell them apart usecoe.SDOReadU8(CiA401Obj::DI_POLARITY, group)and check thestd::optionalbool(write) —true= read-modify-write succeeded;false= the read failed (nothing written, the other channels in the group keep their values) or the write failed. On a failed read it does not write back from a 0 baseline, so the polarity of the other 7 channels in the group is not cleared
Example:
CiA401 io(coe);
io.SetDIPolarityChannel(0, true); // Invert channel 0
bool inv = io.GetDIPolarityChannel(0); // true
io.SetDIPolarityChannel(0, false); // Recover
GetDIFilter() / SetDIFilter()
uint8_t GetDIFilter(int group) const;
bool SetDIFilter(int group, uint8_t filterEnable) const;
Reads or sets the digital input filter enable (0x6003). Used to remove jitter on input signals.
Parameters:
group(int) — group number (subindex, starts at 1)filterEnable(uint8_t) — filter enable mask
Returns:
uint8_t— filter configuration; a failed read falls back to0bool— whether the write succeeded
Example:
CiA401 io(coe);
io.SetDIFilter(1, 10); // Set the filter enable
uint8_t flt = io.GetDIFilter(1); // Read the current filter configuration
TryGetDIFilter()
std::optional<uint8_t> TryGetDIFilter(int group) const;
Reads the digital input filter enable (0x6003) and keeps "could not read" apart from a real value of 0.
GetDIFilter() folds a failed SDO read into the legal value 0 through SDOReadU8(...).value_or(0), so a failed read and "the filter really is fully disabled" are indistinguishable. This entry point returns std::optional: std::nullopt means the SDO read failed, while 0 is a genuine device value. The old GetDIFilter() entry point keeps its exact behaviour (backward compatible).
Use it when a configuration read-back, a diagnostic or an interlock decision must tell "could not read" apart from when it really is 0; do not treat nullopt as 0.
Parameters:
group(int) — group number (subindex, starts at 1)
Returns:
std::optional<uint8_t>— the filter enable mask;std::nullopt= the SDO read failed
Do not treat std::nullopt as 0; a read that never succeeded says nothing about the device.
Example:
CiA401 io(coe);
if (auto flt = io.TryGetDIFilter(1))
printf("滤波掩码 = 0x%02X\n", *flt); // The truth value, which can be 0
else
printf("SDO 读失败, 不要当作滤波全关\n"); // SDO read failed — not the same as "filter fully off"
SetDIFilterChannel()
bool SetDIFilterChannel(int channel, bool enabled) const;
Sets the input filter enable per channel (0x6003): channel / 8 + 1 gives the group number and channel % 8 gives the bit, and the read-modify-write touches only the target bit.
Parameters:
channel(int) — channel number (starts at 0)enabled(bool) — whether to enable the filter for this channel
Returns:
bool—true= read-modify-write succeeded;false= the read failed (nothing written, the filter settings of the other channels in the group keep their values) or the write failed. On a failed read it does not write back from a 0 baseline, so the filters of the other 7 channels in the group are not cleared
Example:
CiA401 io(coe);
io.SetDIFilterChannel(3, true); // Turn on filtering for channel 3 only
Digital input interrupts
0x6005 (interrupt trigger) and 0x6006 (interrupt edge) have no dedicated wrapper; write them directly through the CoE SDO interface:
coe.SDOWriteU8(CiA401Obj::DI_INTERRUPT, 1, 0xFF); // Enable interrupts on all channels of group 1
coe.SDOWriteU8(CiA401Obj::DI_INTERRUPT_EDGE, 1, 0x00); // 0 = rising edge, 1 = falling edge
Analog input interrupts and limits
ReadGlobalInterruptEnable() / WriteGlobalInterruptEnable()
bool ReadGlobalInterruptEnable() const;
bool WriteGlobalInterruptEnable(bool enable) const;
Reads or sets the analog input global interrupt enable (0x6423).
Parameters:
enable(bool) — whether to enable the global interrupt
Returns:
bool— read: whether it is currently enabled (a failed read falls back tofalse); write: whether it succeeded
TryReadGlobalInterruptEnable()
std::optional<bool> TryReadGlobalInterruptEnable() const;
Reads the analog input global interrupt enable (0x6423:00) and keeps "could not read" apart from a real value.
ReadGlobalInterruptEnable() folds a failed SDO read into false through .value_or(0) != 0, so a failed read and "the interrupt really is disabled" are indistinguishable. This entry point returns std::optional: std::nullopt means the SDO read failed, while false is a genuine device value. The old ReadGlobalInterruptEnable() entry point keeps its exact behaviour (backward compatible).
Use it when an interlock decision must tell "could not read" apart from "really disabled"; do not treat nullopt as false.
Returns:
std::optional<bool>—true= enabled,false= disabled;std::nullopt= the SDO read failed
Do not treat std::nullopt as false; an enable state that was never read must not be reported as "interrupt off".
Example:
CiA401 io(coe);
if (auto en = io.TryReadGlobalInterruptEnable()) {
printf("全局中断: %s\n", *en ? "使能" : "关闭"); // The truth value
} else {
printf("SDO 读失败, 不要当作已关闭\n"); // SDO read failed — do not read this as "already off"
}
ReadAIUpperLimit() / WriteAIUpperLimit()
int32_t ReadAIUpperLimit(int channel) const;
bool WriteAIUpperLimit(int channel, int16_t value) const;
Reads or sets the analog input interrupt upper limit (0x6424).
Parameters:
channel(int) — channel number (starts at 0; internally +1 becomes the subindex)value(int16_t) — upper limit
Returns:
int32_t— upper limit (adapts to 16 / 32 bit); a failed read falls back to0bool— whether the write succeeded
ReadAILowerLimit() / WriteAILowerLimit()
int32_t ReadAILowerLimit(int channel) const;
bool WriteAILowerLimit(int channel, int16_t value) const;
Reads or sets the analog input interrupt lower limit (0x6425).
Parameters:
channel(int) — channel number (starts at 0; internally +1 becomes the subindex)value(int16_t) — lower limit
Returns:
int32_t— lower limit (adapts to 16 / 32 bit); a failed read falls back to0bool— whether the write succeeded
An interrupt is triggered when the AI value crosses the upper or lower limit.
Example:
CiA401 io(coe);
// Global interrupt enable
bool intEnabled = io.ReadGlobalInterruptEnable();
io.WriteGlobalInterruptEnable(true);
// Upper/lower interrupt limits
int32_t upper = io.ReadAIUpperLimit(0);
int32_t lower = io.ReadAILowerLimit(0);
io.WriteAIUpperLimit(0, 30000);
io.WriteAILowerLimit(0, -30000);
Error handling
The behaviour of digital and analog outputs on communication loss is controlled by the CiA401ErrorMode enum:
enum class CiA401ErrorMode : uint8_t {
Hold = 0, // Hold the current output value
SafeValue = 1 // Switch to the preset safe value
};
Digital output error modes
CiA401ErrorMode GetDOErrorMode(int group) const;
bool SetDOErrorMode(int group, CiA401ErrorMode mode) const;
uint8_t GetDOErrorValue(int group) const;
bool SetDOErrorValue(int group, uint8_t value) const;
Sets the digital output error mode and safe value. It determines how the outputs respond when EtherCAT communication is lost.
Parameters:
group(int) — group number (starts at 0, one group per 8 channels: group 0 = channels 0-7, group 1 = channels 8-15; internally +1 becomes the subindex)mode(CiA401ErrorMode) — error modevalue(uint8_t) — safe output value (8 bits, one bit per channel)
Returns:
CiA401ErrorMode— current error mode (0x6206)uint8_t— current safe value (0x6207)bool— whether the write succeeded
GetErrorMode()=GetDOErrorMode(0)SetErrorMode(mode)=SetDOErrorMode(0, mode)GetErrorValue(channel)=GetDOErrorValue(channel / 8)(converts the channel to a group number in groups of 8;channel / 8is not guaranteed to fall within the device's actual group count)SetErrorValue(channel, value)=SetDOErrorValue(channel / 8, value)
They add no validation or failure semantics: a failed read also falls back to CiA401ErrorMode::Hold / 0. To distinguish the device's real value, use the std::optional-returning coe.SDOReadU8(CiA401Obj::DO_ERROR_MODE, group + 1) / coe.SDOReadU8(CiA401Obj::DO_ERROR_VALUE, group + 1) directly.
Analog output error modes
CiA401ErrorMode GetAOErrorMode(int channel) const;
bool SetAOErrorMode(int channel, CiA401ErrorMode mode) const;
int16_t GetAOErrorValue(int channel) const;
bool SetAOErrorValue(int channel, int16_t value) const;
Sets the analog output error mode and safe value.
Parameters:
channel(int) — channel number (starts at 0)mode(CiA401ErrorMode) — error modevalue(int16_t) — 16-bit safe output value
Returns:
CiA401ErrorMode— current error mode (0x6443)int16_t— current safe value (0x6444)bool— whether the write succeeded
The safe value applies per bit / per channel.
GetDOErrorMode() / GetAOErrorMode() fall back to CiA401ErrorMode::Hold when the SDO read fails, and GetDOErrorValue() / GetAOErrorValue() fall back to 0 — neither can be distinguished from "the device is really configured this way". To confirm the device's current configuration, use an entry point that returns std::optional, such as coe.SDOReadU8(CiA401Obj::DO_ERROR_MODE, group + 1), and decide yourself.
Example:
CiA401 io(coe);
// Digital output safety configuration
io.SetDOErrorMode(0, CiA401ErrorMode::SafeValue);
io.SetDOErrorValue(0, 0x00); // All outputs low when communication is lost
// Analog output safety configuration
io.SetAOErrorMode(0, CiA401ErrorMode::SafeValue);
io.SetAOErrorValue(0, 0); // Output 0 when communication is lost
// Read the current error configuration
auto mode = io.GetDOErrorMode(0);
uint8_t safeVal = io.GetDOErrorValue(0);
printf("错误模式: %d, 安全值: 0x%02X\n", // Error mode: %d, safe value: 0x%02X
static_cast<int>(mode), safeVal);
CiA401Obj constants
CiA401Obj lists the object indices used by this protocol; they can be passed directly to coe.SDORead* / coe.SDOWrite*:
| Constant | Value | Description |
|---|---|---|
| DI | 0x6000 | Digital input 8-bit groups (RO) |
| DI_16BIT | 0x6001 | 16-bit digital input (RO) |
| DI_POLARITY | 0x6002 | Digital input polarity (RW) |
| DI_FILTER | 0x6003 | Digital input filter enable (RW) |
| DI_INTERRUPT | 0x6005 | Digital input interrupt trigger (RW) |
| DI_INTERRUPT_EDGE | 0x6006 | Digital input interrupt edge (RW) |
| DI_32BIT | 0x6020 | 32-bit digital input (RO) |
| VIRTUAL_DI | 0x2120 | Virtual DI command (WO). Write it with WriteVirtualDI(diIndex, value); diIndex is the subindex (starting at 1). It must go through PDO; when SDO is rejected it switches to PDO automatically |
| DO | 0x6200 | Digital output 8-bit groups (RW) |
| DO_16BIT | 0x6201 | 16-bit digital output (RW) |
| DO_POLARITY | 0x6202 | Digital output polarity (RW) |
| DO_ERROR_MODE | 0x6206 | Digital output error mode (RW) |
| DO_ERROR_VALUE | 0x6207 | Digital output error value (RW) |
| DO_32BIT | 0x6220 | 32-bit digital output (RW) |
| AI | 0x6400 | Analog input 16-bit (RO) |
| AI_SI_UNIT | 0x6420 | Analog input range (RW) |
| AI_GLOBAL_INT | 0x6423 | Analog input global interrupt enable (RW) |
| AI_UPPER_LIMIT | 0x6424 | Analog input interrupt upper limit (RW) |
| AI_LOWER_LIMIT | 0x6425 | Analog input interrupt lower limit (RW) |
| AO | 0x6411 | Analog output 16-bit (RW) |
| AO_32BIT | 0x6412 | 32-bit analog output (RW) |
| AO_SI_UNIT | 0x6430 | Analog output range (RW) |
| AO_ERROR_MODE | 0x6443 | Analog output error mode (RW) |
| AO_ERROR_VALUE | 0x6444 | Analog output error value (RW) |
Full example
#include "ethercat.hpp"
using namespace darra;
void cia401_demo(EtherCATMaster& master) {
auto& coe = master.GetSlave(1).GetCoE();
if (!coe.IsCiA401()) {
printf("从站不是 CiA 401 设备\n"); // This slave is not a CiA 401 device
return;
}
CiA401 io(coe);
// Digital IO
bool sensor = io.ReadDI(0);
io.WriteDO(0, true);
io.WriteDO(1, sensor);
// Analog IO
int32_t adc = io.ReadAI(0);
io.WriteAO(0, static_cast<int16_t>(adc > 50 ? 0 : 16384));
// Safety configuration on communication loss
io.SetDOErrorMode(0, CiA401ErrorMode::SafeValue);
io.SetDOErrorValue(0, 0x00);
}