CiA 401 — Generic I/O modules
CiA 401 (IEC 61131-9) is a CoE-based device profile 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.
Accessed through slave.CoE.CiA401. null when the slave does not support CiA 401.
Differences from CiA 402
CiA 401 is a stateless generic I/O profile: no control word / status word / operation mode / enable sequence, only channel read and write, polarity and filter configuration, and safe values for loss of communication. For the servo state machine, operation modes and motion parameters, see CiA 402.
I/O properties
Channels are read and written directly through indexers; channel numbers start at 0.
| Category | Property | Type | Read/write | Description |
|---|---|---|---|---|
| Digital | DI[n] | bool | read-only | Digital input channel n (0x6000, 8 channels per group) |
| DO[n] | bool | read/write | Digital output channel n (0x6200; the read-modify-write leaves the other channels in the group untouched) | |
| Analog | AI[n] | int | read-only | Analog input channel n (0x6400, adapts automatically to 16-bit or 32-bit signed values) |
| AO[n] | short | read/write | Analog output channel n (0x6411, 16-bit signed) |
var io = slave.CoE.CiA401;
if (io == null) return;
bool sensor = io.DI[0];
io.DO[0] = true;
io.DO[1] = sensor;
int adc = io.AI[0];
io.AO[0] = 16384;
Use PDO mapping for high-frequency I/O control; SDO is suitable for parameter configuration and diagnostics.
Channels, groups and bit order
- Every single-channel number starts at 0, 1 less than channel 1 in the slave manual.
- Digital channels are mapped in groups of 8 to 0x6000 / 0x6200:
channel / 8is the group number and the subindex is the group number + 1;channel % 8is the bit number, and bit0 corresponds to the 1st channel of the group. - For the 16-bit and 32-bit bulk methods,
groupis the subindex and starts at 1: group 1 = channels 0-15 (16-bit) or 0-31 (32-bit). - Analog inputs adapt automatically to 16 or 32 bits; values are interpreted as signed by default. Use
ReadAIUnsigned/ReadAI32Unsignedwhen you need 065535 or 04294967295. - Except for
WriteVirtualDI, write methods throwInvalidOperationExceptionwhen the SDO write fails; the slave configuration stays unchanged.
Accessor types
All four indexer properties are thin wrappers with the same behavior as the matching ReadXxx / WriteXxx methods.
DI
public DIAccessor DI { get; }
Digital input indexer. DI[channel] reads the high/low level of a single channel by channel number (starting at 0) and converts it to SDO 0x6000 in groups of 8 channels.
Related structures:
public class DIAccessor
{
public bool this[int channel] { get; } // read-only, true = high level
}
Example:
bool ch0 = io.DI[0];
bool ch5 = io.DI[5];
DO
public DOAccessor DO { get; }
Digital output indexer. DO[channel] both reads back the last written state and sets the target state (writes SDO 0x6200); the read-modify-write leaves the other channels in the group untouched.
Related structures:
public class DOAccessor
{
public bool this[int channel] { get; set; }
}
Example:
io.DO[0] = true;
io.DO[1] = io.DI[0];
bool current = io.DO[0];
AI
public AIAccessor AI { get; }
Analog input indexer. AI[channel] reads SDO 0x6400 (subindex = channel + 1), adapts automatically to 16 or 32 bits based on the returned length, and returns a signed raw value. For unsigned 16-bit or unsigned 32-bit values, use ReadAIUnsigned / ReadAI32Unsigned.
Related structures:
public class AIAccessor
{
public int this[int channel] { get; } // read-only
}
Example:
int adc0 = io.AI[0];
double voltage = adc0 * 10.0 / 32768;
AO
public AOAccessor AO { get; }
Analog output indexer. AO[channel] reads and writes 16-bit signed values (SDO 0x6411). Use WriteAO32 / ReadAO32 for 32-bit high-precision devices.
Related structures:
public class AOAccessor
{
public short this[int channel] { get; set; }
}
Example:
io.AO[0] = 16384; // write 50% of full scale
short echo = io.AO[0]; // read back the current setpoint
Use the method API below when you need to tell read failures apart, read or write 16-bit or 32-bit bulk values, or configure interrupts.
Digital methods
All channel numbers start at 0; the group number of the 16-bit and 32-bit bulk methods starts at 1 (group 1 = channels 0-15 or 0-31). A normal read method returns 0 / false on failure; use the matching TryRead* method to detect failure.
| Category | Method | Type | Read/write | Description |
|---|---|---|---|---|
| Input | ReadDI(int channel) | bool | read-only | 0x6000, high/low level of a single channel |
TryReadDI(int channel, out bool value) | bool | read-only | false = read failed | |
ReadDI16(int group) | ushort | read-only | 0x6001, 16-channel bitmap | |
TryReadDI16(int group, out ushort value) | bool | read-only | — | |
ReadDI32(int group) | uint | read-only | 0x6020, 32-channel bitmap | |
TryReadDI32(int group, out uint value) | bool | read-only | — | |
| Output | WriteDO(int channel, bool state) | void | write-only | 0x6200, read-modify-write of the group byte |
ReadDO(int channel) | bool | read-only | 0x6200 read-back | |
TryReadDO(int channel, out bool value) | bool | read-only | — | |
ReadDO16(int group) / WriteDO16(int group, ushort value) | ushort / void | read/write | 0x6201 | |
TryReadDO16(int group, out ushort value) | bool | read-only | — | |
ReadDO32(int group) / WriteDO32(int group, uint value) | uint / void | read/write | 0x6220 | |
TryReadDO32(int group, out uint value) | bool | read-only | — | |
| Virtual | WriteVirtualDI(int diIndex, bool state) | void | write-only | 0x2120:01-04; falls back to PDO when the SDO write is aborted, and throws InvalidOperationException when both fail |
Except for WriteVirtualDI, write methods throw InvalidOperationException when the SDO write fails (the slave configuration stays unchanged); WriteDO also throws when it fails to read the old value of the group, rather than overwriting the other channels in the group from a 0 baseline.
ushort di16 = io.ReadDI16(1); // channels 0-15
bool ch5 = (di16 & (1 << 5)) != 0;
io.WriteDO16(1, 0x00FF); // channels 0-7 driven high
uint do32 = io.ReadDO32(1); // 32-bit read-back
ReadDI
public bool ReadDI(int channel)
Reads a digital input. The group number and bit offset are derived automatically for groups of 8 channels. A failed read returns false, which is indistinguishable from a real value; use TryReadDI to detect failure.
Parameters:
channel— channel number (starts at 0)
Return value:
bool— channel state (true = high level)
WriteDO(int channel, bool state)
Parameters: channel — channel number (starts at 0); state — target level.
The group byte is read first and only the target bit is changed, so the other channels in the group are unaffected. A failed read of the old group value throws InvalidOperationException instead of rewriting from a 0 baseline; a failed SDO write throws the same exception and leaves the slave configuration as it was.
io.WriteDO(0, true);
io.WriteDO(1, io.DI[0]); // follow the input
ReadDO
public bool ReadDO(int channel)
Reads a digital output state (SDO 0x6200, in groups of 8 channels). A failed read returns false, which is indistinguishable from a real value; use TryReadDO to detect failure.
Parameters:
channel— channel number (starts at 0)
Return value:
bool— channel state (true = high level)
ReadDI16(int group)
public ushort ReadDI16(int group)
Reads the 16-bit digital input (16 channels).
Parameters:
group— group number (subindex, starts at 1, one group per 16 channels: group 1 = channels 0-15)
Return value:
ushort— 16-bit input value, one bit per channel
ReadDI32(int group)
public uint ReadDI32(int group)
Reads the 32-bit digital input (32 channels).
Parameters:
group— group number (subindex, starts at 1, one group per 32 channels: group 1 = channels 0-31)
Return value:
uint— 32-bit input value, one bit per channel
Example:
var io = slave.CoE.CiA401;
ushort di16 = io.ReadDI16(1); // read channels 0-15
uint di32 = io.ReadDI32(1); // read channels 0-31
bool ch5 = (di16 & (1 << 5)) != 0; // check channel 5
ReadDO16 / WriteDO16
public ushort ReadDO16(int group)
public void WriteDO16(int group, ushort value)
Reads or writes the 16-bit digital output (16 channels).
Parameters:
group— group number (subindex, starts at 1, one group per 16 channels: group 1 = channels 0-15)value— 16-bit output value, bit0 corresponds to the 1st channel of the group
Return value:
ushort— 16-bit output value, one bit per channel (ReadDO16only)
ReadDO32 / WriteDO32
public uint ReadDO32(int group)
public void WriteDO32(int group, uint value)
Reads or writes the 32-bit digital output (32 channels).
Parameters:
group— group number (subindex, starts at 1, one group per 32 channels: group 1 = channels 0-31)value— 32-bit output value, bit0 corresponds to the 1st channel of the group
Return value:
uint— 32-bit output value, one bit per channel (ReadDO32only)
Example:
var io = slave.CoE.CiA401;
// 16-bit bulk operation
io.WriteDO16(1, 0x00FF); // channels 0-7 high, 8-15 low
ushort do16 = io.ReadDO16(1); // read back the current value
// 32-bit bulk operation
io.WriteDO32(1, 0x0000FFFF); // channels 0-15 high
uint do32 = io.ReadDO32(1); // read back the current value
WriteVirtualDI(int diIndex, bool state)
Parameters: diIndex — virtual DI subindex, 1-4, corresponding to vendor object 0x2120:01-04; state — target state.
This object is normally accessible only through PDO: after an SDO abort 0x06020000 the write automatically switches to PDO; an SDO failure for any other reason also falls back to a PDO write attempt for that call. When both SDO and PDO fail, it throws InvalidOperationException.
io.WriteVirtualDI(1, true); // 0x2120:01
Analog methods
| Category | Method | Type | Read/write | Description |
|---|---|---|---|---|
| Input | ReadAI(int channel) | int | read-only | 0x6400, signed, adapts automatically to 16 or 32 bits |
TryReadAI(int channel, out int value) | bool | read-only | — | |
ReadAIUnsigned(int channel) | ushort | read-only | 0x6400, unsigned 16-bit (0~65535) | |
TryReadAIUnsigned(int channel, out ushort value) | bool | read-only | — | |
ReadAI32Unsigned(int channel) | uint | read-only | 0x6400, unsigned 32-bit | |
TryReadAI32Unsigned(int channel, out uint value) | bool | read-only | — | |
| Output | WriteAO(int channel, short value) | void | write-only | 0x6411, 16-bit signed |
ReadAO(int channel) | short | read-only | 0x6411 read-back | |
TryReadAO(int channel, out short value) | bool | read-only | — | |
ReadAO32(int channel) / WriteAO32(int channel, int value) | int / void | read/write | 0x6412, 32-bit high precision | |
TryReadAO32(int channel, out int value) | bool | read-only | — |
int aiSigned = io.ReadAI(0); // high values may turn negative
ushort aiU16 = io.ReadAIUnsigned(0); // 0~65535
uint aiU32 = io.ReadAI32Unsigned(0); // high-precision devices
io.WriteAO32(0, 65536);
Analog read conventions
ReadAI adapts to 16 or 32 bits based on the length returned by the slave and always interprets the value as signed. Which read method to use for a channel depends on the device range:
| Range / device | Usage | Returned range |
|---|---|---|
| Bipolar 16-bit | ReadAI / TryReadAI | -32768 ~ 32767 |
| Unipolar 16-bit | ReadAIUnsigned / TryReadAIUnsigned | 0 ~ 65535 |
| High-precision 32-bit unsigned | ReadAI32Unsigned / TryReadAI32Unsigned | 0 ~ 4294967295 |
| 32-bit signed output | ReadAO32 / WriteAO32 | int |
Range scaling (for example 010 V mapped to 032768) is done in the application layer; the SDK returns raw values only.
Analog output writes
WriteAO writes a 16-bit signed value (0x6411) and WriteAO32 writes a 32-bit value (0x6412). A failed write throws InvalidOperationException; use ReadAO / ReadAO32 to read back and TryReadAO / TryReadAO32 to detect failure.
io.WriteAO(0, 16384); // 50% of full scale
io.WriteAO32(1, 65536); // high-precision channel
ReadAI
public int ReadAI(int channel)
Reads an analog input value. It adapts to 16-bit and 32-bit devices based on the length returned by the slave and always interprets the value as signed. A failed read returns 0, which is indistinguishable from a real value; use TryReadAI to detect failure.
Parameters:
channel— channel number (starts at 0)
Return value:
int— signed analog value
WriteAO
public void WriteAO(int channel, short value)
Writes a 16-bit signed analog output value (SDO 0x6411). A failed write throws InvalidOperationException (the slave output is unchanged).
Parameters:
channel— channel number (starts at 0)value— 16-bit signed output value
ReadAO
public short ReadAO(int channel)
Reads the 16-bit analog output value (SDO 0x6411). A failed read returns 0, which is indistinguishable from a real value; use TryReadAO to detect failure.
Parameters:
channel— channel number (starts at 0)
Return value:
short— 16-bit signed analog output value
ReadAIUnsigned
public ushort ReadAIUnsigned(int channel)
Reads an analog input value (unsigned 16-bit). Suited to devices whose AI data range is 0~65535. A failed read returns 0, which is indistinguishable from a real value; use TryReadAIUnsigned to detect failure.
Parameters:
channel— channel number (starts at 0)
Return value:
ushort— unsigned 16-bit analog value
ReadAI32Unsigned
public uint ReadAI32Unsigned(int channel)
Reads an analog input value (unsigned 32-bit). Suited to high-precision AI devices with a data range of 0~4294967295. A failed read returns 0, which is indistinguishable from a real value; use TryReadAI32Unsigned to detect failure.
Parameters:
channel— channel number (starts at 0)
Return value:
uint— unsigned 32-bit analog value
Example:
var io = slave.CoE.CiA401;
// signed read (default, high values may turn negative)
int aiSigned = io.ReadAI(0);
// unsigned 16-bit read (0~65535)
ushort aiU16 = io.ReadAIUnsigned(0);
// unsigned 32-bit read (high-precision devices)
uint aiU32 = io.ReadAI32Unsigned(0);
ReadAO32 / WriteAO32
public int ReadAO32(int channel)
public void WriteAO32(int channel, int value)
Reads or writes the 32-bit analog output value (0x6412). Suited to high-precision analog output devices. A failed write throws InvalidOperationException (the slave output is unchanged); a failed read returns 0, which is indistinguishable from a real value, so use TryReadAO32 to detect failure.
Parameters:
channel— channel number (starts at 0)value— 32-bit output value
Example:
var io = slave.CoE.CiA401;
io.WriteAO32(0, 65536); // write a 32-bit high-precision value
int ao = io.ReadAO32(0); // read back the current value
Interrupts and limits
Analog input limit interrupt configuration:
| Category | Method | Type | Read/write | Description |
|---|---|---|---|---|
| Interrupt | ReadGlobalInterruptEnable() / WriteGlobalInterruptEnable(bool enable) | bool / void | read/write | 0x6423 global enable |
TryReadGlobalInterruptEnable(out bool enable) | bool | read-only | 0x6423 read-back; false = read failure, out is meaningless | |
| Upper limit | ReadAIUpperLimit(int channel) / WriteAIUpperLimit(int channel, short value) | int / void | read/write | 0x6424 |
TryReadAIUpperLimit(int channel, out int value) | bool | read-only | — | |
| Lower limit | ReadAILowerLimit(int channel) / WriteAILowerLimit(int channel, short value) | int / void | read/write | 0x6425 |
TryReadAILowerLimit(int channel, out int value) | bool | read-only | — |
io.WriteGlobalInterruptEnable(true);
io.WriteAIUpperLimit(0, 30000);
io.WriteAILowerLimit(0, 1000);
WriteGlobalInterruptEnable(bool)is the global switch; when it is off, the per-channel upper and lower limits are retained but do not trigger.- The
valueofWriteAIUpperLimit/WriteAILowerLimitis a 16-bit signed raw value in the same scale asReadAI; confirm the device range before setting it, so the limit does not end up triggering always or never. - Read the limits with
ReadAIUpperLimit/ReadAILowerLimit; detect read failures withTryReadAIUpperLimit/TryReadAILowerLimit. - Read the global interrupt enable with
ReadGlobalInterruptEnable; detect read failures withTryReadGlobalInterruptEnable. - Whether an interrupt is actually reported, and in which PDO bit, is decided by the slave; the SDK only configures the object entries.
ReadGlobalInterruptEnable / WriteGlobalInterruptEnable
public bool ReadGlobalInterruptEnable()
public bool TryReadGlobalInterruptEnable(out bool enable)
public void WriteGlobalInterruptEnable(bool enable)
Reads or sets the global analog input interrupt enable (0x6423). A failed read returns false, which is indistinguishable from a real value; a failed write throws InvalidOperationException (the slave configuration is unchanged).
TryReadGlobalInterruptEnable separates "the global interrupt is genuinely off" from "the read failed": it returns false on a read failure, in which case enable is meaningless (the implementation sets it to false); only when it returns true does enable hold the real value.
Parameters:
enable—true= enabled,false= disabled (WriteGlobalInterruptEnable); an output parameter forTryReadGlobalInterruptEnable
Return value:
bool— whether the global interrupt is enabled (ReadGlobalInterruptEnableonly);TryReadGlobalInterruptEnablereturns whether the read succeeded
if (io.TryReadGlobalInterruptEnable(out bool enabled))
Console.WriteLine(enabled ? "全局中断已使能" : "全局中断已关闭"); // global interrupts enabled / global interrupts disabled
else
Console.WriteLine("全局中断使能读取失败, 不能断言已关闭"); // Reading the global interrupt enable failed, cannot assert it is off
ReadAIUpperLimit / WriteAIUpperLimit
public int ReadAIUpperLimit(int channel)
public void WriteAIUpperLimit(int channel, short value)
Reads or writes the analog input interrupt upper limit (0x6424). The interrupt triggers when the AI value exceeds the upper limit. A failed write throws InvalidOperationException (the slave configuration is unchanged); a failed read returns 0, which is indistinguishable from a real value, so use TryReadAIUpperLimit to detect failure.
Parameters:
channel— channel number (starts at 0)value— upper limit (16-bit signed raw value)
ReadAILowerLimit / WriteAILowerLimit
public int ReadAILowerLimit(int channel)
public void WriteAILowerLimit(int channel, short value)
Reads or writes the analog input interrupt lower limit (0x6425). The interrupt triggers when the AI value falls below the lower limit. A failed write throws InvalidOperationException (the slave configuration is unchanged); a failed read returns 0, which is indistinguishable from a real value, so use TryReadAILowerLimit to detect failure.
Parameters:
channel— channel number (starts at 0)value— lower limit (16-bit signed raw value)
Example:
var io = slave.CoE.CiA401;
// enable the global interrupt
io.WriteGlobalInterruptEnable(true);
// 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);
bool enabled = io.ReadGlobalInterruptEnable();
Digital input configuration and error safe values
| Category | Method | Type | Read/write | Description |
|---|---|---|---|---|
| Digital input | SetDIPolarity(int group, byte polarity) / GetDIPolarity(int group) | void / byte | read/write | 0x6002, 1 = invert the corresponding channel |
TryGetDIPolarity(int group, out byte polarity) | bool | read-only | 0x6002 read-back; false = read failure, out is meaningless | |
SetDIFilter(int group, byte filterEnable) / GetDIFilter(int group) | void / byte | read/write | 0x6003, filter enable mask | |
TryGetDIFilter(int group, out byte filterEnable) | bool | read-only | 0x6003 read-back; false = read failure, out is meaningless | |
| Error mode | DOErrorMode(int group, CiA401ErrorMode mode) | void | write-only | 0x6206, output behavior when communication is lost |
DOErrorValue(int group, byte value) | void | write-only | 0x6207, safe output value (8-bit) | |
GetDOErrorMode(int group) / TryGetDOErrorMode(int group, out CiA401ErrorMode mode) | CiA401ErrorMode / bool | read-only | 0x6206 read-back | |
GetDOErrorValue(int group) / TryGetDOErrorValue(int group, out byte value) | byte / bool | read-only | 0x6207 read-back | |
| Analog output | AOErrorMode(int channel, CiA401ErrorMode mode) | void | write-only | 0x6443 |
AOErrorValue(int channel, short value) | void | write-only | 0x6444, 16-bit safe value |
public enum CiA401ErrorMode : byte
{
Hold = 0, // hold the current output value
SafeValue = 1, // switch to the preset safe value
}
io.SetDIPolarity(1, 0x0F); // invert channels 0-3
io.SetDIFilter(1, 10);
SetDIPolarity / SetDIFilter
Parameters:
group— group number (subindex, starts at 1, one group per 8 channels)polarity— polarity mask, 1 = invert the corresponding channel, bit0 corresponds to the 1st channel of the groupfilterEnable— filter enable mask; the bit definitions are defined by the slave implementation
Read-back and failure detection:
GetDIPolarity/GetDIFilterreturn0on a read failure, which is indistinguishable from "polarity / filter is genuinely all zero"; they must not be used as a configuration read-back conclusion. UseTryGetDIPolarity/TryGetDIFilterto tell the cases apart.TryGetDIPolarity/TryGetDIFilterreturnfalseon a read failure, in which case theoutparameter is meaningless (the implementation sets it to0); only when they returntruedoesouthold the slave's real value.
io.SetDIPolarity(1, 0x0F); // invert channels 0-3 of group 1
io.SetDIFilter(1, 10);
if (io.TryGetDIPolarity(1, out byte pol))
Console.WriteLine($"极性 = 0x{pol:X2}"); // Polarity = 0x{pol:X2}
else
Console.WriteLine("极性读取失败, 不能断言为 0"); // Polarity read failed, cannot assert it is 0
if (!io.TryGetDIFilter(1, out byte flt))
Console.WriteLine("滤波使能读取失败"); // Filter enable read failed
Error safe values
DOErrorMode(int group, CiA401ErrorMode mode) decides how the digital outputs respond when EtherCAT communication is lost; DOErrorValue(int group, byte value) is the 8-bit safe value in SafeValue mode (bit0 corresponds to the 1st channel of the group). AOErrorMode / AOErrorValue work the same way for analog outputs, with a 16-bit safe value.
Hold— keep the last output valueSafeValue— switch to the preset safe value; whenSafeValueis configured withoutDOErrorValue/AOErrorValue, 0 is used- The
groupof the error mode starts at 0 (group 0 = channels 0-7), unlike the group number of polarity and filter (starts at 1) - These settings take effect only when communication is lost; normal cyclic output is still controlled by
DO/AO - Read the configuration back with
GetDOErrorMode/GetDOErrorValue; detect failures withTryGetDOErrorMode/TryGetDOErrorValue
io.DOErrorMode(0, CiA401ErrorMode.SafeValue);
io.DOErrorValue(0, 0x00); // drive the whole group low when communication is lost
io.AOErrorMode(0, CiA401ErrorMode.SafeValue);
io.AOErrorValue(0, 0);
// read the configuration back
var mode = io.GetDOErrorMode(0);
byte safeVal = io.GetDOErrorValue(0);
Console.WriteLine($"错误模式: {mode}, 安全值: 0x{safeVal:X2}"); // Error mode: {mode}, safe value: 0x{safeVal:X2}
Telling read failures apart
Normal read methods (ReadDI / ReadAI / ReadDO16 and so on) return the placeholder 0 / false / Hold on failure, which is indistinguishable from a real value, so they must not take part in interlocks as process state. A Try-prefixed method returns false to signal a failed read (the out parameter is meaningless then); only when it returns true does out hold the real value.
Configuration read-back follows the same rule: TryGetDIPolarity / TryGetDIFilter / TryReadGlobalInterruptEnable, together with TryGetDOErrorMode / TryGetDOErrorValue / TryReadAIUpperLimit / TryReadAILowerLimit.
| Method family | Placeholder returned on a failed read |
|---|---|
ReadDI / ReadDO / ReadGlobalInterruptEnable | false |
ReadDI16 / ReadDO16 / ReadDI32 / ReadDO32 | 0 |
ReadAI / ReadAIUnsigned / ReadAI32Unsigned | 0 |
ReadAO / ReadAO32 / ReadAIUpperLimit / ReadAILowerLimit | 0 |
GetDIPolarity / GetDIFilter / GetDOErrorValue | 0 |
GetDOErrorMode | Hold |
if (io.TryReadAI(0, out int ai))
Console.WriteLine($"AI0 = {ai}");
else
Console.WriteLine("AI0 读取失败, 不可当测量值使用"); // AI0 read failed, must not be used as a measurement value
This "real value / not supported / read failed" convention matches observable read-only reads in CiA 402.
Return values and exceptions
| Category | Behavior |
|---|---|
| Normal read | A failed read returns the placeholder 0 / false / Hold, which is indistinguishable from a real value |
TryRead* | false = read failed, and out is meaningless then; when it returns true, out holds the real value |
| Write | A failed SDO write throws InvalidOperationException; the slave configuration is unchanged |
| Virtual DI | Falls back to PDO when the SDO is aborted; throws InvalidOperationException when both fail |
| Accessor | DI[n] / DO[n] / AI[n] / AO[n] have the same semantics as the methods: read accessors do not throw and return the placeholder on a failed read; assigning to DO[n] / AO[n] also throws InvalidOperationException on failure |
Typical sequence
- Confirm that
slave.CoE.CiA401is notnull. - Configure input polarity and filtering (
SetDIPolarity/SetDIFilter). - Configure the error mode and safe values for loss of communication (
DOErrorMode/DOErrorValue/AOErrorMode/AOErrorValue). - If analog limit interrupts are needed, configure
WriteGlobalInterruptEnableand the upper and lower limits. - Use PDO or the indexers for cyclic read and write; use the SDO methods for parameter configuration and diagnostics.
Real-time behavior and threads
- Use PDO for high-frequency cyclic control; do not issue per-channel SDO requests from a real-time thread.
- Indexers and methods access the same object dictionary; when several threads read or write the same channel at once, the application layer must ensure ordering.
WriteDOis a read-modify-write, not an atomic operation; concurrent writes to several channels of the same group must be serialized by the application layer.- The error safe values are a "communication lost" policy, not an application-level emergency stop; emergency stops still go through PDO and the drive safety chain.
Consistency and snapshots
A per-channel read is an independent SDO each time, so the values may come from different cycles and must not be used in interlocks as a snapshot of a single instant. When same-cycle consistency is required:
- Read the whole group in one call with
ReadDI16/ReadDI32/ReadDO16/ReadDO32(a single transaction) - Or use the PDO process image directly, where all channels update together within one cycle
Channel range
The SDK does not restrict channel numbers; the usable range is determined by the slave objects. A subindex beyond those the slave implements either fails to read or returns an abort, and TryRead* distinguishes a read failure from a real value of 0. The same applies to group numbers: check the subindex range given in the slave documentation before a bulk read.
Writing and reading back
- When several channels of a group must change together, write the whole group at once with
WriteDO16/WriteDO32to avoid intermediate states. - A successful SDO write only means the object dictionary was written; it does not guarantee that the output terminal has already acted. For critical outputs, read back with
ReadDO/ReadAOafter writing, or confirm from the PDO input side. - During a communication loss, writes may be overridden by the drive error safe values; after recovery the target values must be sent again.
FAQ
| Symptom | Check order |
|---|---|
DI[n] is always false | Whether the channel number starts at 0, whether polarity is inverted, whether the input voltage reaches the threshold; use TryReadDI to tell a failed read apart |
Other channels in the group change after writing DO[n] | Check whether 0x6200 bytes are written directly, bypassing WriteDO; WriteDO itself changes only the target bit |
No output after writing AO | Confirm the range and that 32-bit devices use WriteAO32, that the error mode is not SafeValue, and that communication is healthy |
| Analog reading has the wrong sign | Switch to ReadAIUnsigned / ReadAI32Unsigned when the device range is unipolar |
| Interrupt does not trigger | Whether the global interrupt is enabled, whether the limits fall within the actual reading range, and whether the slave supports the object |
Write throws InvalidOperationException | Check the object index in the log; most often an SDO write failure or a failed read of the old group value, and the slave configuration has not been rewritten |
Standard object index constants
These can be used directly with slave.CoE.SDORead / SDOWrite:
| Constant | Value | Description |
|---|---|---|
| OD_VIRTUAL_DI | 0x2120 | Virtual DI command (vendor object, must go through PDO) |
| OD_DI | 0x6000 | Digital input |
| OD_DI_16BIT | 0x6001 | 16-bit digital input |
| 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 input |
| OD_DO | 0x6200 | Digital output |
| OD_DO_16BIT | 0x6201 | 16-bit digital output |
| 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 output |
| OD_AI | 0x6400 | Analog input |
| 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 output |
| OD_AO_32BIT | 0x6412 | 32-bit analog output |
| 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 |
Full example — initialization and cyclic read/write
var io = slave.CoE.CiA401;
if (io == null) return;
// one-time configuration
io.SetDIFilter(1, 0x0F);
io.DOErrorMode(0, CiA401ErrorMode.SafeValue);
io.DOErrorValue(0, 0x00);
// read and write in the cyclic task (switch to PDO for high frequencies)
while (running)
{
if (io.TryReadAI(0, out int adc) && io.TryReadDI(0, out bool start))
{
io.WriteDO(0, start);
io.WriteAO(0, (short)Math.Clamp(adc, -32768, 32767));
}
Thread.Sleep(10);
}