Skip to main content

CiA 401 — Generic I/O modules

CiA 401 (IEC 61131-9) is the CoE-based protocol for generic digital and analog I/O devices.

CiA 401 runs on top of CoE (SDO) and is available only when the slave supports CoE.

Access it through slave.cia401 (created automatically on first access, available on every slave). Whether a slave actually supports CiA 401 depends on its object dictionary: when the object does not exist, the read interfaces return 0 / False (read_all_di() of Complete Access returns None on failure) and the write interfaces return False. For SDO failure semantics and abort codes see CoE.

Quick start​

Read and write I/O channels through methods. The channel of the per-channel API starts at 0; the group of the 16/32-bit bulk API and the polarity/filter API is used directly as the SDO subindex (starts at 1); the group of the error mode / safe value API starts at 0 (the subindex is converted internally).

CategoryMethodTypeRead/writeDescription
Digital I/Oread_di(n)boolread-onlyRead digital input channel n
write_do(n, state)boolwrite-onlyWrite digital output channel n, returns the SDO write result
Analog I/Oread_ai(n)intread-onlyRead analog input channel n
write_ao(n, value)boolwrite-onlyWrite analog output channel n, returns the SDO write result

Example:

io = slave.cia401

# Digital I/O
sensor: bool = io.read_di(0)
io.write_do(0, True)
io.write_do(1, sensor)

# Analog I/O
adc: int = io.read_ai(0)
io.write_ao(0, 16384)
Real-time control

High-frequency I/O control should use PDO mapping instead of SDO. SDO is suitable for parameter configuration and diagnostics.

Method API​

All write_* / set_* methods return bool (whether the SDO write succeeded; a failure does not throw).

Read-back and write semantics
  • read_do() / read_ao() read back the current output value from the object dictionary, not the return value of the previous call
  • Per-channel digital writes (write_do) are read-modify-write transactions: the other channels in the same group keep their values, and if reading the current value fails nothing is written and False is returned immediately; analog channels have their own subindexes and are overwritten directly
  • Unsigned writes are truncated by a bit-width mask; out-of-range signed write arguments throw from struct packing
  • The read interfaces return 0 / False when the object does not exist, so a read of 0 must be judged together with object existence; read_all_di() returns None on failure, which distinguishes it from an all-0 input
  • Three read interfaces have distinguishable variants: try_get_di_polarity() / try_get_di_filter() / try_read_global_interrupt_enable(), which return None on a read failure (0 / False are valid real values)

read_di()​

def read_di(self, channel: int) -> bool

Reads a digital input. The SDK automatically computes the group number and bit offset in groups of 8 channels.

Parameters:

  • channel (int) — channel number (starting at 0)

read_do()​

def read_do(self, channel: int) -> bool

Reads back the digital output state last written (SDO 0x6200).

Parameters:

  • channel (int) — channel number (starting at 0)

write_do()​

def write_do(self, channel: int, state: bool) -> bool

Sets a digital output without affecting the other channels in the same group (read-modify-write). If reading the current value fails, nothing is written and False is returned immediately.

Parameters:

  • channel (int) — channel number (starting at 0)
  • state (bool) — target state

Returns:

  • bool — whether the SDO write succeeded

read_ai()​

def read_ai(self, channel: int) -> int

Reads an analog input value. Adapts automatically to 16-bit and 32-bit devices and returns a signed value.

Parameters:

  • channel (int) — channel number (starting at 0)

read_ao()​

def read_ao(self, channel: int) -> int

Reads back the current analog output setpoint (SDO 0x6411), signed 16-bit.

Parameters:

  • channel (int) — channel number (starting at 0)

write_ao()​

def write_ao(self, channel: int, value: int) -> bool

Writes an analog output value (signed 16-bit).

Parameters:

  • channel (int) — channel number (starting at 0)
  • value (int) — signed 16-bit output value

Returns:

  • bool — whether the SDO write succeeded

write_ao_raw()​

def write_ao_raw(self, channel: int, value: int) -> bool

Writes a raw analog output value (unsigned 16-bit), for devices with an output range of 0~65535.

Parameters:

  • channel (int) — channel number (starting at 0)
  • value (int) — unsigned 16-bit output value

Returns:

  • bool — whether the SDO write succeeded

read_ai_raw()​

def read_ai_raw(self, channel: int) -> int

Reads a raw analog input value (unsigned 16-bit). For devices whose AI data range is 0~65535.

Parameters:

  • channel (int) — channel number (starting at 0)

Returns:

  • int — unsigned 16-bit analog value

read_ai32()​

def read_ai32(self, channel: int) -> int

Reads a 32-bit analog input value (unsigned). For high-precision AI devices with a data range of 0~4294967295.

Parameters:

  • channel (int) — channel number (starting at 0)

Returns:

  • int — unsigned 32-bit analog value

Example:

io = slave.cia401

# Signed read (default; high values may become negative)
ai_signed = io.read_ai(0)

# Unsigned 16-bit raw read (0~65535)
ai_u16 = io.read_ai_raw(0)

# Unsigned 32-bit read (high-precision devices)
ai_u32 = io.read_ai32(0)

Bulk I/O methods​

Besides per-channel operations, 16/32-bit bulk read/write is supported, operating on multiple channels at once. The group of the bulk API is used directly as the SDO subindex and starts at 1.

read_di16()​

def read_di16(self, group: int) -> int

Reads 16 digital inputs (16 channels).

Parameters:

  • group (int) — group number (that is, the SDO subindex, starting at 1)

Returns:

  • int — 16-bit input value, one bit per channel

read_di32()​

def read_di32(self, group: int) -> int

Reads 32 digital inputs (32 channels).

Parameters:

  • group (int) — group number (that is, the SDO subindex, starting at 1)

Returns:

  • int — 32-bit input value, one bit per channel

read_do16() / write_do16()​

def read_do16(self, group: int) -> int
def write_do16(self, group: int, value: int) -> bool

Reads or writes 16 digital outputs (16 channels).

Parameters:

  • group (int) — group number (that is, the SDO subindex, starting at 1)
  • value (int) — 16-bit output value (write_do16 only)

Returns:

  • read_do16 → int — 16-bit output value
  • write_do16 → bool — whether the SDO write succeeded

Example:

io.write_do16(1, 0x00FF)          # group 1: low 8 bits output high, high 8 bits output low
do16 = io.read_do16(1)

read_do32() / write_do32()​

def read_do32(self, group: int) -> int
def write_do32(self, group: int, value: int) -> bool

Reads or writes 32 digital outputs (32 channels). Parameters as in read_do16 / write_do16.

read_ao32() / write_ao32()​

def read_ao32(self, channel: int) -> int
def write_ao32(self, channel: int, value: int) -> bool

Reads or writes a 32-bit analog output value (signed). For high-precision analog output devices.

Parameters:

  • channel (int) — channel number (starting at 0)
  • value (int) — signed 32-bit output value (write_ao32 only)

Example:

io.write_ao32(0, 65536)           # write a 32-bit high-precision value
ao = io.read_ao32(0) # read back the current value

Byte-wise and whole-object access​

read_di_byte() / write_do_byte()​

def read_di_byte(self, index: int) -> int
def write_do_byte(self, index: int, value: int) -> bool

Reads digital inputs byte-wise (0x6000) / writes digital outputs byte-wise (0x6200).

Parameters:

  • index (int) — byte index (that is, the SDO subindex, starting at 1)
  • value (int) — 8-bit output value (write_do_byte only)

read_all_di() / write_all_do()​

def read_all_di(self) -> Optional[bytes]
def write_all_do(self, data: bytes) -> bool

Complete access reads or writes all digital input/output bytes at once.

Parameters:

  • data (bytes) — all digital output data bytes (write_all_do only)

Returns:

  • read_all_di → Optional[bytes] — all input data, None on failure
  • write_all_do → bool — whether the SDO write succeeded

Analog input interrupts and limits​

read_global_interrupt_enable() / write_global_interrupt_enable()​

def read_global_interrupt_enable(self) -> bool
def write_global_interrupt_enable(self, enable: bool) -> bool

Reads or sets the analog input global interrupt enable (0x6423).

Parameters:

  • enable (bool) — True = enable, False = disable (write_global_interrupt_enable only)

try_read_global_interrupt_enable()​

def try_read_global_interrupt_enable(self) -> Optional[bool]

Distinguishable variant for reading the analog input global interrupt enable (0x6423).

Returns:

  • bool — the real value from the slave; False = the global interrupt is really disabled, a valid real value
  • None — read failure (SDO timeout / communication error / object does not exist / empty data)

Notes:

  • The old entry read_global_interrupt_enable() returns False on a read failure, which is indistinguishable from "really disabled"; use this variant to tell them apart.
  • Read-only, with no object dictionary change; writes still use write_global_interrupt_enable() (which returns bool).

Example:

enabled = io.try_read_global_interrupt_enable()
if enabled is None:
print("全局中断使能读取失败(不是'已关闭')") # Failed to read global interrupt enable (this is not 'disabled')
elif enabled:
print("全局中断已使能") # Global interrupt enabled

read_ai_upper_limit() / write_ai_upper_limit()​

def read_ai_upper_limit(self, channel: int) -> int
def write_ai_upper_limit(self, channel: int, value: int) -> bool

Reads or writes the analog input interrupt upper limit (0x6424). An interrupt is triggered when the AI value exceeds the upper limit. Writes are signed 16-bit; reads adapt to 16/32 bits.

Parameters:

  • channel (int) — channel number (starting at 0)
  • value (int) — signed 16-bit upper limit value (write_ai_upper_limit only)

read_ai_lower_limit() / write_ai_lower_limit()​

def read_ai_lower_limit(self, channel: int) -> int
def write_ai_lower_limit(self, channel: int, value: int) -> bool

Reads or writes the analog input interrupt lower limit (0x6425). An interrupt is triggered when the AI value falls below the lower limit. Writes are signed 16-bit; reads adapt to 16/32 bits.

Parameters:

  • channel (int) — channel number (starting at 0)
  • value (int) — signed 16-bit lower limit value (write_ai_lower_limit only)

Example:

io = slave.cia401

# Enable the global interrupt
io.write_global_interrupt_enable(True)

# Set the upper and lower limits of channel 0
io.write_ai_upper_limit(0, 30000)
io.write_ai_lower_limit(0, 1000)

# Read the current limit configuration
upper = io.read_ai_upper_limit(0)
lower = io.read_ai_lower_limit(0)
enabled = io.read_global_interrupt_enable()

Digital input configuration​

set_di_polarity() / get_di_polarity()​

def set_di_polarity(self, group: int, polarity: int) -> bool
def get_di_polarity(self, group: int) -> int

Sets or reads the digital input polarity (0x6002). Can invert the input signal logic per bit.

Parameters:

  • group (int) — group number (that is, the SDO subindex, starting at 1)
  • polarity (int) — 8-bit polarity mask, one bit per channel, 1 = invert (set_di_polarity only)

set_di_filter() / get_di_filter()​

def set_di_filter(self, group: int, filter_enable: int) -> bool
def get_di_filter(self, group: int) -> int

Sets or reads the digital input filter enable (0x6003). Used to suppress jitter on input signals.

Parameters:

  • group (int) — group number (that is, the SDO subindex, starting at 1)
  • filter_enable (int) — 8-bit filter enable mask (set_di_filter only)

try_get_di_polarity() / try_get_di_filter()​

def try_get_di_polarity(self, group: int) -> Optional[int]
def try_get_di_filter(self, group: int) -> Optional[int]

Distinguishable variants for reading the digital input polarity (0x6002) / filter enable (0x6003): a read failure returns None.

Parameters:

  • group (int) — group number (that is, the SDO subindex, starting at 1)

Returns:

  • int — the mask that was read; 0 is a valid real value (a mask of all 0s), not a failure signal
  • None — read failure (SDO timeout / communication error / object does not exist / empty data)

Notes:

  • Difference from the old entries: get_di_polarity() / get_di_filter() fold a read failure into 0 because of their old signature, making it indistinguishable from a real 0.
  • Read-only, with no object dictionary change; writes still use set_di_polarity() / set_di_filter() (which return bool).

Example:

polarity = io.try_get_di_polarity(1)
if polarity is None:
print("极性读取失败(不是'极性全 0')") # Failed to read polarity (this is not 'all polarity zero')
else:
print(f"组 1 极性掩码=0x{polarity:02X}") # Group 1 polarity mask=0x{polarity:02X}

mask = io.try_get_di_filter(1)
if mask is None:
print("滤波配置读取失败") # Failed to read the filter configuration

Virtual DI​

write_virtual_di()​

def write_virtual_di(self, di_index: int, state: bool) -> bool

Writes the virtual DI command (0x2120:0x). 0x2120 is a PdoMapped object, so an SDO write is aborted with 0x06020000; the method automatically falls back to a PDO write and logs it (the decision is cached after the first SDO write failure, and later calls go straight to PDO; failures other than 0x06020000 also fall back and are logged).

Parameters:

  • di_index (int) — virtual DI subindex (1-4 → 0x2120:01-04)
  • state (bool) — target state (True = 1)

Returns:

  • bool — True = write succeeded (SDO or PDO); False = both SDO and PDO failed (no PDO mapping for 0x2120 currently)

Error status reading​

get_do_error_mode() / get_do_error_value()​

def get_do_error_mode(self, group: int) -> int
def get_do_error_value(self, group: int) -> int

Reads the current configuration of the digital output error mode and error safe value. group starts at 0 (one group per 8 channels). get_do_error_mode() returns an integer (0=HOLD / 1=SAFE_VALUE).

Error handling​

set_do_error_mode()​

def set_do_error_mode(self, group: int, mode: int) -> bool

Sets the digital output error mode. Decides how the outputs respond when EtherCAT communication is lost.

Parameters:

  • group (int) — group number (starting at 0, one group per 8 channels: group 0 = channels 0-7, group 1 = channels 8-15)
  • mode (int) — error mode (pass CiA401ErrorMode.HOLD / CiA401ErrorMode.SAFE_VALUE, that is, the integers 0 / 1)

CiA401ErrorMode constant class:

CiA401ErrorMode is a plain constant class (not an IntEnum); its members are integer constants.

from darra_ethercat import CiA401ErrorMode

class CiA401ErrorMode:
HOLD = 0 # hold the current output value
SAFE_VALUE = 1 # switch to the preset safe value
The error-mode methods take and return int

The mode parameter of set_do_error_mode() / set_ao_error_mode() and the return values of get_do_error_mode() / get_ao_error_mode() are all of type int. Passing CiA401ErrorMode.SAFE_VALUE is the integer 1, and the read-back is also the integer 0 / 1, so it can be compared directly with the members of that constant class.

set_do_error_value()​

def set_do_error_value(self, group: int, value: int) -> bool

Sets the safe value for digital outputs when communication is lost. Takes effect only in SAFE_VALUE mode.

Parameters:

  • group (int) — group number (starting at 0)
  • value (int) — safe output value (8-bit, one bit per channel)

set_ao_error_mode()​

def set_ao_error_mode(self, channel: int, mode: int) -> bool

Sets the analog output error mode.

Parameters:

  • channel (int) — channel number (starting at 0)
  • mode (int) — error mode (pass CiA401ErrorMode.HOLD / CiA401ErrorMode.SAFE_VALUE, that is, the integers 0 / 1)

set_ao_error_value()​

def set_ao_error_value(self, channel: int, value: int) -> bool

Sets the safe value for analog outputs when communication is lost. Takes effect only in SAFE_VALUE mode.

Parameters:

  • channel (int) — channel number (starting at 0)
  • value (int) — signed 16-bit safe output value

get_ao_error_mode() / get_ao_error_value()​

def get_ao_error_mode(self, channel: int) -> int
def get_ao_error_value(self, channel: int) -> int

Reads the current configuration of the analog output error mode and error safe value. get_ao_error_mode() returns an integer (0=HOLD / 1=SAFE_VALUE), get_ao_error_value() returns a signed 16-bit safe value.

Example:

io.set_do_error_mode(0, CiA401ErrorMode.SAFE_VALUE)
io.set_do_error_value(0, 0x00)

io.set_ao_error_mode(0, CiA401ErrorMode.SAFE_VALUE)
io.set_ao_error_value(0, 0)

Standard object index constants​

The following constants are class attributes of the CiA401 class (accessed as CiA401.OD_DI etc.) and can be combined with slave.coe.sdo_read / sdo_write to access the object dictionary directly. "Read/write" in the table means the access capability of that object through SDO:

CategoryConstantValueRead/writeDescription
Digital inputOD_DI0x6000read-onlyDigital inputs
Virtual DIOD_VIRTUAL_DI0x2120write-onlyVirtual DI command (PdoMapped; an SDO write is aborted with 0x06020000, must go through PDO)
Digital inputOD_DI_16BIT0x6001read-only16-bit digital inputs
Digital inputOD_DI_POLARITY0x6002read/writeDigital input polarity
Digital inputOD_DI_FILTER0x6003read/writeDigital input filter enable
Digital inputOD_DI_32BIT0x6020read-only32-bit digital inputs
Digital outputOD_DO0x6200read/writeDigital outputs
Digital outputOD_DO_16BIT0x6201read/write16-bit digital outputs
Digital outputOD_DO_ERROR_MODE0x6206read/writeDigital output error mode
Digital outputOD_DO_ERROR_VALUE0x6207read/writeDigital output error value
Digital outputOD_DO_32BIT0x6220read/write32-bit digital outputs
Analog inputOD_AI0x6400read-onlyAnalog inputs
Analog inputOD_AI_SI_UNIT0x6420read-onlyAnalog input range
Analog inputOD_AI_GLOBAL_INTERRUPT0x6423read/writeAnalog input global interrupt enable
Analog inputOD_AI_UPPER_LIMIT0x6424read/writeAnalog input interrupt upper limit
Analog inputOD_AI_LOWER_LIMIT0x6425read/writeAnalog input interrupt lower limit
Analog outputOD_AO0x6411read/writeAnalog outputs
Analog outputOD_AO_32BIT0x6412read/write32-bit analog outputs
Analog outputOD_AO_SI_UNIT0x6430read-onlyAnalog output range
Analog outputOD_AO_ERROR_MODE0x6443read/writeAnalog output error mode
Analog outputOD_AO_ERROR_VALUE0x6444read/writeAnalog output error value

Complete example​

from darra_ethercat import CiA401ErrorMode

io = slave.cia401

# Digital I/O
sensor = io.read_di(0)
limit = io.read_di(5)

io.write_do(0, True)
io.write_do(1, sensor)

# Bulk digital I/O
di16 = io.read_di16(1) # read group 1 (16 channels)
io.write_do16(1, 0x00FF) # group 1: low 8 bits output high

# Analog I/O
adc = io.read_ai(0)
temp = adc * 0.1
io.write_ao(0, 0 if temp > 50 else 16384)

# Unsigned reads
ai_u16 = io.read_ai_raw(0)
ai_u32 = io.read_ai32(0)

# Analog input limit configuration
io.write_global_interrupt_enable(True)
io.write_ai_upper_limit(0, 30000)
io.write_ai_lower_limit(0, 1000)

# Safety configuration
io.set_do_error_mode(0, CiA401ErrorMode.SAFE_VALUE)
io.set_do_error_value(0, 0x00)

io.set_ao_error_mode(0, CiA401ErrorMode.SAFE_VALUE)
io.set_ao_error_value(0, 0)