CiA 401 — Generic I/O modules
CiA 401 (IEC 61131-9) is the CoE-based device protocol for generic digital / analog I/O devices.
CiA 401 runs on top of CoE (SDO) and is available only when the slave supports CoE.
Accessed directly through slave.cia401(). The child object is created lazily and always returns an instance (never null). CiA 401 runs over CoE, so calling its methods on a slave that is not a CoE / I/O module fails (returns 0 / false); before calling, use slave.supportsCoE() to check whether the slave supports CoE.
IO properties
Read and write IO channels directly through the accessors; all channel numbers start at 0.
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| Digital | DI.get(n) | boolean | read-only | Digital input channel n |
| DO.get(n) / DO.set(n, val) | boolean | read/write | Digital output channel n | |
| Analog | AI.get(n) | int | read-only | Analog input channel n |
| AO.get(n) / AO.set(n, val) | short | read/write | Analog output channel n |
Example:
// the slave.cia401() child object is created lazily and always returns an instance (never null)
CiA401 io = slave.cia401();
// digital IO
boolean sensor = io.DI.get(0);
io.DO.set(0, true);
io.DO.set(1, sensor);
// analog IO
int adc = io.AI.get(0);
io.AO.set(0, (short) 16384);
High-frequency I/O control should use PDO mapping instead of SDO. SDO is suited to parameter configuration and diagnostics.
DI
public DIAccessor DI;
Digital input accessor. DI.get(channel) reads the high / low level of a single channel by channel number (starting at 0), internally converting to SDO 0x6000 in groups of 8 channels.
Related structures:
public class DIAccessor {
public boolean get(int channel); // read-only, true = high level
}
Example:
boolean ch0 = io.DI.get(0);
boolean ch5 = io.DI.get(5);
DO
public DOAccessor DO;
Digital output accessor. DO.get(channel) reads back the last written state; DO.set(channel, state) sets the target state (writes SDO 0x6200) without affecting other channels in the same group.
Related structures:
public class DOAccessor {
public boolean get(int channel);
public void set(int channel, boolean state);
}
Example:
io.DO.set(0, true);
io.DO.set(1, io.DI.get(0));
boolean current = io.DO.get(0);
AI
public AIAccessor AI;
Analog input accessor. AI.get(channel) returns a 16/32-bit signed raw value corresponding to SDO 0x6401 / 0x6402. For an unsigned 32-bit read, use readAI32Unsigned.
Related structures:
public class AIAccessor {
public int get(int channel); // read-only
}
Example:
int adc0 = io.AI.get(0);
double voltage = adc0 * 10.0 / 32768;
AO
public AOAccessor AO;
Analog output accessor. AO.get(channel) / AO.set(channel, value) read and write a 16-bit signed value (SDO 0x6411). For 32-bit high-precision devices use writeAO32 / readAO32.
Related structures:
public class AOAccessor {
public short get(int channel);
public void set(int channel, short value);
}
Example:
io.AO.set(0, (short) 16384); // write 50% of full scale
short echo = io.AO.get(0); // read back the current setpoint
Method API
The accessors call the following methods internally; they can also be used directly.
readDI
public boolean readDI(int channel)
Reads a digital input. Internally it computes the group number and bit offset automatically in groups of 8 channels.
Parameters:
channel— channel number (starting at 0)
writeDO
public void writeDO(int channel, boolean state)
Sets a digital output without affecting other channels in the same group.
Parameters:
channel— channel number (starting at 0)state— target state
readAI
public int readAI(int channel)
Reads an analog input value. It adapts automatically to 16-bit and 32-bit devices.
Parameters:
channel— channel number (starting at 0)
writeAO
public void writeAO(int channel, short value)
Writes an analog output value.
Parameters:
channel— channel number (starting at 0)value— 16-bit output value
readAI32Unsigned
public long readAI32Unsigned(int channel)
Reads an analog input value (unsigned 32-bit, returned as a long). Suited to high-precision AI devices; data range 0~4294967295.
Parameters:
channel— channel number (starting at 0)
Returns:
long— unsigned 32-bit analog value
Example:
CiA401 io = slave.cia401();
// signed read (default; large values may become negative)
int aiSigned = io.readAI(0);
// unsigned 32-bit read (high-precision devices)
long aiU32 = io.readAI32Unsigned(0);
Virtual DI writing
Subindices 01-04 of the virtual digital input object 0x2120 correspond to 4 virtual DI channels. By firmware convention this object must be accessed through the PDO mapping, and an SDO write is rejected by the slave with 0x06020000; writeVirtualDI first attempts SDO and, once rejected, automatically falls back to a PDO write.
writeVirtualDI(int diIndex, boolean state)
public void writeVirtualDI(int diIndex, boolean state)
Writes one virtual DI channel. After the first SDO rejection, subsequent calls on the same instance go straight to PDO, so the failed attempt is not repeated for every command.
Parameters:
diIndex(int) — virtual DI subindex; 1-4 corresponds to0x2120:01-0x2120:04state(boolean) — target state;truewrites 1,falsewrites 0
Returns: none (void).
Exceptions:
IllegalStateException— neither the SDO nor the PDO path can write the value (including the case where the slave has not mapped0x2120into the RxPDO); the message includes the slave index and subindex
Example:
CiA401 io = slave.cia401();
// Set virtual DI 1 to 1 and virtual DI 2 to 0
io.writeVirtualDI(1, true);
io.writeVirtualDI(2, false);
WriteVirtualDI(int diIndex, boolean state)
public void WriteVirtualDI(int diIndex, boolean state)
Cross-language contract name (identical in C# / C++); its semantics, parameters, return value and exceptions are exactly the same as writeVirtualDI(int, boolean), and the call is forwarded to it directly.
Parameters:
diIndex(int) — virtual DI subindex (1-4)state(boolean) — target state
Returns: none (void).
Analog input interrupts and limits
readGlobalInterruptEnable / writeGlobalInterruptEnable
public int readGlobalInterruptEnable()
public boolean writeGlobalInterruptEnable(int value)
Reads or sets the analog input global interrupt enable (0x6423).
Parameters:
value— enable value (0 = disable, a value other than 0 = enable)
readGlobalInterruptEnableOrNull()
public Integer readGlobalInterruptEnableOrNull()
Failure-aware read entry point: null = read failure, 0 is a legitimate value (global interrupt disabled). The older readGlobalInterruptEnable() returns 0 on a read failure, indistinguishable from a real 0.
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| Analog input | readGlobalInterruptEnableOrNull() | Integer | read-only | 0x6423 interrupt enable; null = read failure, 0 = real value (disabled) |
Example:
Integer en = slave.cia401().readGlobalInterruptEnableOrNull();
if (en == null) {
/* read failure: do not treat it as "interrupt disabled" */
} else if (en == 0) {
/* real 0: the global interrupt really is disabled */
}
readAIUpperLimit / writeAIUpperLimit
public int readAIUpperLimit(int channel)
public boolean writeAIUpperLimit(int channel, int value)
Reads or writes the analog input interrupt upper limit (0x6424). An interrupt is triggered when the AI value exceeds the upper limit.
Parameters:
channel— channel number (starting at 0)value— upper limit
readAILowerLimit / writeAILowerLimit
public int readAILowerLimit(int channel)
public boolean writeAILowerLimit(int channel, int value)
Reads or writes the analog input interrupt lower limit (0x6425). An interrupt is triggered when the AI value falls below the lower limit.
Parameters:
channel— channel number (starting at 0)value— lower limit
Example:
CiA401 io = slave.cia401();
// enable the global interrupt
io.writeGlobalInterruptEnable(1);
// set the upper and lower limits of channel 0
io.writeAIUpperLimit(0, 30000);
io.writeAILowerLimit(0, 1000);
// read the current limit configuration
int upper = io.readAIUpperLimit(0);
int lower = io.readAILowerLimit(0);
int enabled = io.readGlobalInterruptEnable();
Bulk IO methods
Besides per-channel operations, 16/32-bit bulk reads and writes are supported, operating on several channels at once.
readDI16(int group)
public short readDI16(int group)
Reads 16-bit digital inputs (16 channels).
Parameters:
group— group number (subindex, starting at 1)
Returns:
short— 16-bit input value, one bit per channel
readDI32(int group)
public int readDI32(int group)
Reads 32-bit digital inputs (32 channels).
Parameters:
group— group number (subindex, starting at 1)
Returns:
int— 32-bit input value, one bit per channel
Example:
CiA401 io = slave.cia401();
short di16 = io.readDI16(1); // read channels 0-15
int di32 = io.readDI32(1); // read channels 0-31
boolean ch5 = (di16 & (1 << 5)) != 0; // check channel 5
readDO16 / writeDO16
public short readDO16(int group)
public void writeDO16(int group, short value)
Reads or writes 16-bit digital outputs (16 channels).
Parameters:
group— group number (subindex, starting at 1)value— 16-bit output value
readDO32 / writeDO32
public int readDO32(int group)
public void writeDO32(int group, int value)
Reads or writes 32-bit digital outputs (32 channels).
Parameters:
group— group number (subindex, starting at 1)value— 32-bit output value
Example:
CiA401 io = slave.cia401();
// 16-bit bulk operation
io.writeDO16(1, (short) 0x00FF); // channels 0-7 output high, 8-15 output low
short do16 = io.readDO16(1); // read back the current value
// 32-bit bulk operation
io.writeDO32(1, 0x0000FFFF); // channels 0-15 output high
int do32 = io.readDO32(1); // read back the current value
readAO32 / writeAO32
public int readAO32(int channel)
public void writeAO32(int channel, int value)
Reads or writes 32-bit analog output values. Suited to high-precision analog output devices.
Parameters:
channel— channel number (starting at 0)value— 32-bit output value
Example:
CiA401 io = slave.cia401();
io.writeAO32(0, 65536); // write a 32-bit high-precision value
int ao = io.readAO32(0); // read back the current value
Digital input configuration
setDIPolarity / getDIPolarity
public void setDIPolarity(int group, byte polarity)
public byte getDIPolarity(int group)
Sets or reads the digital input polarity (0x6002). The input signal logic can be inverted bit by bit.
Parameters:
group— group number (subindex, starting at 1)polarity— polarity mask; 1 = invert the corresponding channel
Example:
CiA401 io = slave.cia401();
io.setDIPolarity(1, (byte) 0x0F); // invert the polarity of channels 0-3
byte pol = io.getDIPolarity(1); // read the current polarity configuration
setDIFilter / getDIFilter
public void setDIFilter(int group, byte filterEnable)
public byte getDIFilter(int group)
Sets or reads the digital input filter enable (0x6003). Used to remove jitter from input signals.
Parameters:
group— group number (subindex, starting at 1)filterEnable— filter enable mask
Example:
CiA401 io = slave.cia401();
io.setDIFilter(1, (byte) 10); // set the filter enable
byte flt = io.getDIFilter(1); // read the current filter configuration
getDIPolarityOrNull / getDIFilterOrNull
public Byte getDIPolarityOrNull(int group)
public Byte getDIFilterOrNull(int group)
Failure-aware read entry points: null = read failure, while a returned 0 is a legitimate value ("polarity not inverted" / "filter all off"). The older getDIPolarity / getDIFilter collapse a read failure to 0 (legacy contract), so their return value alone cannot distinguish "real 0" from "not read".
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| Digital input | getDIPolarityOrNull(group) | Byte | read-only | 0x6002 polarity mask; null = read failure, 0 = real value (no inversion) |
| getDIFilterOrNull(group) | Byte | read-only | 0x6003 filter enable; null = read failure, 0 = real value (all off) |
Parameters:
group— group number (subindex, starting at 1)
Example:
CiA401 io = slave.cia401();
Byte pol = io.getDIPolarityOrNull(1);
if (pol == null) {
/* read failure: do not treat it as "no channel inverted" */
} else if (pol == 0) {
/* real 0: the device really inverts no channel */
}
Byte flt = io.getDIFilterOrNull(1);
if (flt == null) {
/* read failure: do not treat it as "filter all off" */
}
Error status reading
getDOErrorMode / getDOErrorValue
public ErrorMode getDOErrorMode(int group)
public byte getDOErrorValue(int group)
Reads the current configuration of the digital output error mode and error safe value.
Parameters:
group— group number (starting at 0)
Example:
CiA401 io = slave.cia401();
CiA401.ErrorMode mode = io.getDOErrorMode(0);
byte safeVal = io.getDOErrorValue(0);
System.out.printf("错误模式: %s, 安全值: 0x%02X%n", mode, safeVal); // Error mode: %s, safe value: 0x%02X%n
Error handling
dOErrorMode
public void dOErrorMode(int group, ErrorMode mode)
Sets the digital output error mode. It determines how the outputs respond when EtherCAT communication is lost.
Parameters:
group— group number (starting at 0; one group per 8 channels: group 0 = channels 0-7, group 1 = channels 8-15)mode— error mode
Related structures:
public enum ErrorMode {
HOLD((byte) 0), // hold the current output value
SAFE_VALUE((byte) 1); // switch to the preset safe value
}
dOErrorValue
public void dOErrorValue(int group, byte value)
Sets the safe value for the digital outputs when communication is lost. Effective only in SAFE_VALUE mode.
Parameters:
group— group number (starting at 0)value— safe output value (8-bit, one bit per channel)
aOErrorMode
public void aOErrorMode(int channel, ErrorMode mode)
Sets the analog output error mode.
Parameters:
channel— channel number (starting at 0)mode— error mode
aOErrorValue
public void aOErrorValue(int channel, short value)
Sets the safe value for the analog outputs when communication is lost. Effective only in SAFE_VALUE mode.
Parameters:
channel— channel number (starting at 0)value— 16-bit safe output value
Example:
io.dOErrorMode(0, CiA401.ErrorMode.SAFE_VALUE);
io.dOErrorValue(0, (byte) 0x00);
io.aOErrorMode(0, CiA401.ErrorMode.SAFE_VALUE);
io.aOErrorValue(0, (short) 0);
Standard object index constants
Can be used with slave.coe().SDORead / SDOWrite for direct access:
| Constant | Value | Description |
|---|---|---|
| OD_DI | 0x6000 | Digital inputs |
| OD_DI_16BIT | 0x6001 | 16-bit digital inputs |
| OD_DI_POLARITY | 0x6002 | Digital input polarity |
| OD_DI_FILTER | 0x6003 | Digital input filter enable |
| OD_DI_INTERRUPT | 0x6005 | Digital input interrupt trigger |
| OD_DI_INTERRUPT_EDGE | 0x6006 | Digital input interrupt edge |
| OD_DI_32BIT | 0x6020 | 32-bit digital inputs |
| OD_DO | 0x6200 | Digital outputs |
| OD_DO_16BIT | 0x6201 | 16-bit digital outputs |
| OD_DO_POLARITY | 0x6202 | Digital output polarity |
| OD_DO_ERROR_MODE | 0x6206 | Digital output error mode |
| OD_DO_ERROR_VALUE | 0x6207 | Digital output error value |
| OD_DO_32BIT | 0x6220 | 32-bit digital outputs |
| OD_AI | 0x6400 | Analog inputs |
| OD_AI_SI_UNIT | 0x6420 | Analog input range |
| OD_AI_GLOBAL_INTERRUPT | 0x6423 | Analog input global interrupt enable |
| OD_AI_UPPER_LIMIT | 0x6424 | Analog input interrupt upper limit |
| OD_AI_LOWER_LIMIT | 0x6425 | Analog input interrupt lower limit |
| OD_AO | 0x6411 | Analog outputs |
| OD_AO_32BIT | 0x6412 | 32-bit analog outputs |
| OD_AO_SI_UNIT | 0x6430 | Analog output range |
| OD_AO_ERROR_MODE | 0x6443 | Analog output error mode |
| OD_AO_ERROR_VALUE | 0x6444 | Analog output error value |
Complete example
// the slave.cia401() child object is created lazily and always returns an instance (never null)
CiA401 io = slave.cia401();
// digital IO
boolean sensor = io.DI.get(0);
boolean limit = io.DI.get(5);
io.DO.set(0, true);
io.DO.set(1, sensor);
// analog IO
int adc = io.AI.get(0);
double temp = adc * 0.1;
io.AO.set(0, (short) (temp > 50 ? 0 : 16384));
// safety configuration
io.dOErrorMode(0, CiA401.ErrorMode.SAFE_VALUE);
io.dOErrorValue(0, (byte) 0x00);