Skip to main content

CiA 401 — Generic I/O modules

CiA 401 (IEC 61131-9) is a generic digital/analog I/O device protocol built on CoE.

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

Access via slave.cia401().​

IO attributes​

IO channels are read and written directly through methods; all channel numbers start at 0.

CategoryMethodTypeRead/writeDescription
Digitalread_di(n)Option<bool>read-onlyDigital input channel n (None = the read of that channel failed)
read_do(n)Option<bool>read-onlyRead-back value of digital output channel n (None = read failed)
write_do(n, v)Result<()>write-onlyDigital output channel n
Analogread_ai(n)Option<i32>read-onlyAnalog input channel n (None = read failed)
read_ao(n)Option<i16>read-onlyRead-back value of analog output channel n (None = read failed)
write_ao(n, v)Result<()>write-onlyAnalog output channel n

All read_* / read-only configuration getters return Option<T>: None = that SDO read failed or the subindex does not exist. The caller must decide the fallback value itself; do not treat None as 0.

Example:

let io = slave.cia401();

// digital IO
let sensor = io.read_di(0).unwrap_or(false); // None = read failed, handled with a fallback value
io.write_do(0, true)?;
io.write_do(1, sensor)?;

// analog IO
let adc = io.read_ai(0).unwrap_or(0); // None = read failed, handled with a fallback value
io.write_ao(0, 16384)?;
Real-time control

Use PDO mapping rather than SDO for high-frequency I/O control. SDO is suited to parameter configuration and diagnostics.

Method API​

read_di()​

pub fn read_di(&self, channel: i32) -> Option<bool>

Reads a digital input. Internally the group number and bit offset are derived automatically, 8 channels per group.

Parameters:

  • channel — channel number (starting at 0)

Returns:

  • Option<bool> — Some(true/false) = channel state (high/low); None = read failed (SDO read failed / subindex does not exist), never fabricated as false

write_do()​

pub fn write_do(&self, channel: i32, state: bool) -> Result<()>

Sets a digital output without affecting the other channels in the same group. Internally a read-modify-write is used.

Parameters:

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

read_do()​

pub fn read_do(&self, channel: i32) -> Option<bool>

Reads back the current value of a digital output channel. None = read failed.

read_ai()​

pub fn read_ai(&self, channel: i32) -> Option<i32>

Reads an analog input value. Automatically accommodates 16-bit and 32-bit devices.

Parameters:

  • channel — channel number (starting at 0)

read_ai_unsigned()​

pub fn read_ai_unsigned(&self, channel: i32) -> Option<u16>

Reads a 16-bit unsigned analog input value. Unlike read_ai(), the data is interpreted as an unsigned value, which suits unipolar ADCs.

Parameters:

  • channel — channel number (starting at 0)

read_ai32_unsigned()​

pub fn read_ai32_unsigned(&self, channel: i32) -> Option<u32>

Reads a 32-bit unsigned analog input value, which suits high-precision ADCs (such as sensor modules with a 24-bit / 32-bit measurement range).

Parameters:

  • channel — channel number (starting at 0)

write_ao()​

pub fn write_ao(&self, channel: i32, value: i16) -> Result<()>

Writes an analog output value.

Parameters:

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

read_ao()​

pub fn read_ao(&self, channel: i32) -> Option<i16>

Reads back the current value of an analog output channel (16-bit). None = read failed.

Batch IO methods​

In addition to per-channel operations, 16/32-bit batch reads and writes are supported, operating on multiple channels at once.

Digital input batch reads​

read_di16()​

pub fn read_di16(&self, group: i32) -> Option<u16>

Reads 16 digital inputs (16 channels).

Parameters:

  • group — group number (subindex, starting at 1; each group holds 16 channels)

Returns:

  • Option<u16> — Some(值) = 16-bit input value (one bit per channel); None = read failed

read_di32()​

pub fn read_di32(&self, group: i32) -> Option<u32>

Reads 32 digital inputs (32 channels).

Parameters:

  • group — group number (subindex, starting at 1; each group holds 32 channels)

Returns:

  • Option<u32> — Some(值) = 32-bit input value (one bit per channel); None = read failed

Example:

let io = slave.cia401();

let di16 = io.read_di16(1).unwrap_or(0); // read channels 0-15; None = read failed
let di32 = io.read_di32(1).unwrap_or(0); // read channels 0-31; None = read failed
let ch5 = (di16 & (1 << 5)) != 0; // check channel 5

Digital output batch read/write​

read_do16() / write_do16()​

pub fn read_do16(&self, group: i32) -> Option<u16>
pub fn write_do16(&self, group: i32, value: u16) -> Result<()>

Reads or writes 16 digital outputs (16 channels).

Parameters:

  • group — group number (subindex, starting at 1; each group holds 16 channels)
  • value — 16-bit output value

read_do32() / write_do32()​

pub fn read_do32(&self, group: i32) -> Option<u32>
pub fn write_do32(&self, group: i32, value: u32) -> Result<()>

Reads or writes 32 digital outputs (32 channels).

Parameters:

  • group — group number (subindex, starting at 1; each group holds 32 channels)
  • value — 32-bit output value

Example:

let io = slave.cia401();

// 16-bit batch operations
io.write_do16(1, 0x00FF)?; // channels 0-7 output high, 8-15 output low
let do16 = io.read_do16(1).unwrap_or(0); // read back the current value; None = read failed

// 32-bit batch operations
io.write_do32(1, 0x0000FFFF)?; // channels 0-15 output high
let do32 = io.read_do32(1).unwrap_or(0); // read back the current value; None = read failed

32-bit analog output​

read_ao32() / write_ao32()​

pub fn read_ao32(&self, channel: i32) -> Option<i32>
pub fn write_ao32(&self, channel: i32, value: i32) -> Result<()>

Reads or writes a 32-bit analog output value. Suited to high-precision analog output devices.

Parameters:

  • channel — channel number (starting at 0)
  • value — 32-bit output value

Example:

let io = slave.cia401();
io.write_ao32(0, 65536)?; // write a 32-bit high-precision value
let ao = io.read_ao32(0).unwrap_or(0); // read back the current value; None = read failed

Digital input configuration​

set_di_polarity() / di_polarity()​

pub fn set_di_polarity(&self, group: i32, polarity: u8) -> Result<()>
pub fn di_polarity(&self, group: i32) -> Option<u8>

Sets or reads the digital input polarity (0x6002). The input signal logic can be inverted per bit.

Parameters:

  • group — group number (subindex, starting at 1)
  • polarity — polarity mask, 1 = invert the corresponding channel

Example:

let io = slave.cia401();
io.set_di_polarity(1, 0x0F)?; // invert the polarity of channels 0-3
let pol = io.di_polarity(1).unwrap_or(0); // read the current polarity configuration; None = read failed

set_di_filter() / di_filter()​

pub fn set_di_filter(&self, group: i32, filter_enable: u8) -> Result<()>
pub fn di_filter(&self, group: i32) -> Option<u8>

Sets or reads the digital input filter enable (0x6003). Used to eliminate input signal jitter.

Parameters:

  • group — group number (subindex, starting at 1)
  • filter_enable — filter enable mask

Example:

let io = slave.cia401();
io.set_di_filter(1, 10)?; // set the filter enable
let flt = io.di_filter(1).unwrap_or(0); // read the current filter configuration; None = read failed

Virtual digital inputs (0x2120)​

A virtual DI command sets one digital input of the slave directly from the master; it is typically used for commissioning or self-test when the input is not wired. Subindexes 01–04 of object 0x2120 correspond to virtual channels 1–4.

write_virtual_di()​

pub fn write_virtual_di(&self, di_index: i32, state: bool) -> Result<()>

Parameters:

  • di_index — virtual DI channel number; by convention 1–4 correspond to 0x2120:01–0x2120:04 (the method itself does not check the range)
  • state — true writes 1, false writes 0

Access path and return value:

0x2120 must be accessed over PDO; an SDO write is rejected (typically abort 0x06020000). The method tries the paths in this order:

  1. First it writes 0x2120:<di_index> over SDO; on success it returns Ok(())
  2. After the SDO is rejected it falls back to PDO automatically: it looks up the byte offset of 0x2120 in the RxPDO and writes the value into this cycle's output image, returning Ok(()) on success. The fallback happens once; the instance then remembers that this object has to go over PDO and does not retry SDO
  3. If neither path is usable (0x2120 is not mapped into the RxPDO) → it returns Err(DarraError::Other) and writes one warning log telling you to map 0x2120 into the RxPDO and try again
let io = slave.cia401();

// Set virtual input 1 high, virtual input 2 low
io.write_virtual_di(1, true)?;
io.write_virtual_di(2, false)?;

Analog input configuration​

global_interrupt_enable() / set_global_interrupt_enable()​

pub fn global_interrupt_enable(&self) -> Option<bool>
pub fn set_global_interrupt_enable(&self, enabled: bool) -> Result<()>

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

ai_upper_limit() / set_ai_upper_limit()​

pub fn ai_upper_limit(&self, channel: i32) -> Option<i32>
pub fn set_ai_upper_limit(&self, channel: i32, value: i16) -> Result<()>

Reads or sets the analog input interrupt upper limit (0x6424).

Parameters:

  • channel — channel number (starting at 0)
  • value — upper limit value

ai_lower_limit() / set_ai_lower_limit()​

pub fn ai_lower_limit(&self, channel: i32) -> Option<i32>
pub fn set_ai_lower_limit(&self, channel: i32, value: i16) -> Result<()>

Reads or sets the analog input interrupt lower limit (0x6425).

Parameters:

  • channel — channel number (starting at 0)
  • value — lower limit value

Read failure versus a legitimate truth value​

The three read entries for digital input configuration and the analog input interrupt enable return Option: None means only that this read did not succeed; Some(0) / Some(false) is the value the slave actually returned, not a failure.

CategoryPropertyTypeRead/writeDescription
Digital inputdi_polarity(group)Option<u8>read-onlyPolarity mask (0x6002); Some(0) = really returned "invert nothing", None = read failed
Digital inputdi_filter(group)Option<u8>read-onlyFilter enable mask (0x6003); Some(0) = really returned "enable nothing", None = read failed
Analog inputglobal_interrupt_enable()Option<bool>read-onlyGlobal interrupt enable (0x6423); Some(false) = really returned "disabled", None = read failed
  • A Some is returned only after a successful read; the 0 / false inside it is a legitimate truth value, not a read-failure sentinel.
  • The Option entries do not carry the failure cause (abort code / status); they distinguish only "read succeeded" from "did not read".
  • Folding with unwrap_or(0) makes "read failed" and "true value 0" the same shape — exactly the ambiguity this three-state form avoids.

Example:

let io = slave.cia401();
match io.di_polarity(1) {
Some(0) => println!("从站真返回:通道全不反转"), // Slave actually returned: no channels inverted at all
Some(mask) => println!("极性掩码 0x{mask:02X}"), // Polarity mask 0x{mask:02X}
None => println!("读取失败 —— 不是 0,按失败处理"), // Read failed — this is not a 0, so treat it as a failure
}

// Use match when the distinction matters; do not fold with unwrap_or(0)
let enabled = io.global_interrupt_enable();
if enabled == Some(false) {
println!("从站真返回:全局中断未使能"); // Slave actually returned: global interrupt not enabled
}

Error status readback​

do_error_mode() / do_error_value()​

pub fn do_error_mode(&self, group: i32) -> Option<CiA401ErrorMode>
pub fn do_error_value(&self, group: i32) -> Option<u8>

Reads the current configuration of the digital output error mode and error safe value.

Parameters:

  • group — group number (starting at 0)

Example:

let io = slave.cia401();
let mode = io.do_error_mode(0);
let safe_val = io.do_error_value(0).unwrap_or(0); // None = read failed
println!("错误模式: {:?}, 安全值: 0x{:02X}", mode, safe_val); // Error mode: {:?}, safe value: 0x{:02X}

get_ao_error_mode() / get_ao_error_value()​

pub fn get_ao_error_mode(&self, channel: i32) -> Option<CiA401ErrorMode>
pub fn get_ao_error_value(&self, channel: i32) -> Option<i16>

Reads the current configuration of the analog output error mode and error safe value.

Parameters:

  • channel — channel number (starting at 0)

Error handling​

set_do_error_mode()​

pub fn set_do_error_mode(&self, group: i32, mode: CiA401ErrorMode) -> Result<()>

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

Parameters:

  • group — group number (starting at 0; each group holds 8 channels: group 0 = channels 0-7, group 1 = channels 8-15)
  • mode — error mode

Related structures:

#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CiA401ErrorMode {
Hold = 0, // hold the current output value
SafeValue = 1, // switch to the preset safe value
}

set_do_error_value()​

pub fn set_do_error_value(&self, group: i32, value: u8) -> Result<()>

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

Parameters:

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

set_ao_error_mode()​

pub fn set_ao_error_mode(&self, channel: i32, mode: CiA401ErrorMode) -> Result<()>

Sets the analog output error mode.

Parameters:

  • channel — channel number (starting at 0)
  • mode — error mode

set_ao_error_value()​

pub fn set_ao_error_value(&self, channel: i32, value: i16) -> Result<()>

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

Parameters:

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

Example:

io.set_do_error_mode(0, CiA401ErrorMode::SafeValue)?;
io.set_do_error_value(0, 0x00)?;

io.set_ao_error_mode(0, CiA401ErrorMode::SafeValue)?;
io.set_ao_error_value(0, 0)?;

Standard object index constants​

The CiA 401 IO module object dictionary follows the ETG.5001 standard index layout: digital input / digital output / analog input / analog output each occupy a separate index range, and the subentries also cover configuration such as polarity, filtering, interrupts, upper and lower limits, error mode and error value. The SDK exports strongly typed constants such as OD_DI / OD_DO / OD_AI / OD_AO from the darra_ethercat::slave::cia401 module, used directly with the slave's typed SDO read/write (slave.sdo_read_value() / sdo_write_value()), so there is no need to memorize numbers by hand.

use darra_ethercat::slave::cia401 as od;

// read a digital input through a constant (single-byte channel 0)
let val: u8 = slave.sdo_read_value(od::OD_DI, 0x01)?;

// write the digital output global polarity through a constant
slave.sdo_write_value(od::OD_DO_POLARITY, 0x00, &0u32)?;
Relationship to slave.cia401()

For day-to-day work the high-level slave.cia401() API is recommended (read_di, write_do, read_ai, ...); fall back to constants + SDO only when you need batch traversal or need to read subindexes that the above APIs do not cover directly.

Complete example​

use darra_ethercat::CiA401ErrorMode;

let io = slave.cia401();

// digital IO
let sensor = io.read_di(0).unwrap_or(false); // None = read failed, handled with a fallback value
let limit = io.read_di(5).unwrap_or(false); // None = read failed, handled with a fallback value
io.write_do(0, true)?;
io.write_do(1, sensor)?;

// batch digital IO
let di16 = io.read_di16(1).unwrap_or(0); // None = read failed, handled with a fallback value
io.write_do16(1, 0x00FF)?;

// digital input configuration
io.set_di_polarity(1, 0x0F)?; // invert channels 0-3
io.set_di_filter(1, 10)?; // enable the filter

// analog IO
let adc = io.read_ai(0).unwrap_or(0); // None = read failed, handled with a fallback value
let temp = adc as f64 * 0.1;
io.write_ao(0, if temp > 50.0 { 0 } else { 16384 })?;

// 32-bit analog output
io.write_ao32(0, 65536)?;

// error status readback
let mode = io.do_error_mode(0);
let safe_val = io.do_error_value(0).unwrap_or(0); // None = read failed
println!("错误模式: {:?}, 安全值: 0x{:02X}", mode, safe_val); // Error mode: {:?}, safe value: 0x{:02X}

// safe configuration
io.set_do_error_mode(0, CiA401ErrorMode::SafeValue)?;
io.set_do_error_value(0, 0x00)?;

io.set_ao_error_mode(0, CiA401ErrorMode::SafeValue)?;
io.set_ao_error_value(0, 0)?;