NI Analog Read Task
Learn how to acquire analog data from NI devices with Synnax.
For task lifecycle management, see the Task Basics page.
Task Configuration Reference
Channel Types Reference
Accelerometer (ai_accel)
ai_accel) Bridge (ai_bridge)
ai_bridge) Current (ai_current)
ai_current) Force Bridge Table (ai_force_bridge_table)
ai_force_bridge_table) Force Bridge Two-Point Linear (ai_force_bridge_two_point_lin)
ai_force_bridge_two_point_lin) Force IEPE (ai_force_iepe)
ai_force_iepe) Microphone (ai_microphone)
ai_microphone) Pressure Bridge Table (ai_pressure_bridge_table)
ai_pressure_bridge_table) Pressure Bridge Two-Point Linear (ai_pressure_bridge_two_point_lin)
ai_pressure_bridge_two_point_lin) Resistance (ai_resistance)
ai_resistance) RTD (ai_rtd)
ai_rtd) Strain Gauge (ai_strain_gauge)
ai_strain_gauge) Temperature Built-In Sensor (ai_temp_builtin)
ai_temp_builtin) Thermocouple (ai_thermocouple)
ai_thermocouple) Torque Bridge Table (ai_torque_bridge_table)
ai_torque_bridge_table) Torque Bridge Two-Point Linear (ai_torque_bridge_two_point_lin)
ai_torque_bridge_two_point_lin) Velocity IEPE (ai_velocity_iepe)
ai_velocity_iepe) Voltage (ai_voltage)
ai_voltage)Important Rules
- Sample rates -> All channels in a task sample at the same rate. Create separate tasks for different rates.
- One task per module -> Only one running task can claim a module at a time.
- Stream rate optimization -> For low-rate tasks (< 50 Hz), set the stream rate to the sample rate. For high-rate tasks, keep the stream rate less than 50 Hz for better performance.
How-To
Configure and run task
import synnax as sy
from synnax import ni
client = sy.Synnax()
# Retrieve devices
v_dev = client.devices.retrieve(name="Mod1_Voltage")
c_dev = client.devices.retrieve(name="Mod2_Current")
tc_dev = client.devices.retrieve(name="Mod1_TC")
# Create index and data channels
ai_time = client.channels.create(
name="ai_time",
is_index=True,
data_type=sy.DataType.TIMESTAMP,
retrieve_if_name_exists=True,
)
voltage_chan = client.channels.create(
name="voltage_chan",
index=ai_time.key,
data_type=sy.DataType.FLOAT32,
retrieve_if_name_exists=True,
)
current_chan = client.channels.create(
name="current_chan",
index=ai_time.key,
data_type=sy.DataType.FLOAT32,
retrieve_if_name_exists=True,
)
temp_chan = client.channels.create(
name="temp_chan",
index=ai_time.key,
data_type=sy.DataType.FLOAT32,
retrieve_if_name_exists=True,
)
# Create and configure task
task = ni.AnalogReadTask(
name="Analog Read Task",
sample_rate=sy.Rate.HZ * 100,
stream_rate=sy.Rate.HZ * 25,
data_saving=True,
channels=[
ni.AIVoltageChan(
channel=voltage_chan.key,
device=v_dev.key,
port=0,
min_val=-10.0,
max_val=10.0,
terminal_config="Diff",
),
ni.AICurrentChan(
channel=current_chan.key,
device=c_dev.key,
port=0,
min_val=0.004,
max_val=0.02,
),
ni.AIThermoChan(
channel=temp_chan.key,
device=tc_dev.key,
port=0,
units="DegC",
thermocouple_type="J",
cjc_source="BuiltIn",
),
],
)
client.tasks.configure(task)
# Start task and read data
with task.run():
with client.open_streamer(["voltage_chan", "current_chan", "temp_chan"]) as streamer:
for _ in range(10):
frame = streamer.read()
print(frame)
Edit task configuration
# Retrieve existing task
task = client.tasks.retrieve(name="Analog Read Task")
task = ni.AnalogReadTask(internal=task)
# Update task-level configuration
task.config.auto_start = True
task.config.stream_rate = int(sy.Rate.HZ * 50)
# Update voltage channel configuration
task.config.channels[0].port = 1
task.config.channels[0].min_val = -5.0
task.config.channels[0].max_val = 5.0
# Update current channel configuration
task.config.channels[1].port = 2
task.config.channels[1].min_val = 0.002
task.config.channels[1].max_val = 0.01
# Update thermocouple channel configuration
task.config.channels[2].port = 3
task.config.channels[2].thermocouple_type = "K"
# Apply changes
client.tasks.configure(task) Configure and run task
import { Synnax } from "@synnaxlabs/client";
const client = new Synnax();
// Retrieve devices
const vDev = await client.devices.retrieve({ name: "Mod1_Voltage" });
const cDev = await client.devices.retrieve({ name: "Mod2_Current" });
const tcDev = await client.devices.retrieve({ name: "Mod1_TC" });
// Create index and data channels
const aiTime = await client.channels.create({
name: "ai_time",
isIndex: true,
dataType: "timestamp",
retrieveIfNameExists: true,
});
const voltageChan = await client.channels.create({
name: "voltage_chan",
index: aiTime.key,
dataType: "float32",
retrieveIfNameExists: true,
});
const currentChan = await client.channels.create({
name: "current_chan",
index: aiTime.key,
dataType: "float32",
retrieveIfNameExists: true,
});
const tempChan = await client.channels.create({
name: "temp_chan",
index: aiTime.key,
dataType: "float32",
retrieveIfNameExists: true,
});
// Create and configure task
const task = await client.tasks.create({
name: "Analog Read Task",
type: "ni_analog_read",
config: JSON.stringify({
sample_rate: 100,
stream_rate: 25,
data_saving: true,
channels: [
{
type: "ai_voltage",
channel: voltageChan.key,
device: vDev.key,
port: 0,
min_val: -10.0,
max_val: 10.0,
terminal_config: "Diff",
},
{
type: "ai_current",
channel: currentChan.key,
device: cDev.key,
port: 0,
min_val: 0.004,
max_val: 0.02,
},
{
type: "ai_thermo",
channel: tempChan.key,
device: tcDev.key,
port: 0,
units: "DegC",
thermocouple_type: "J",
cjc_source: "BuiltIn",
},
],
}),
});
// Start task
await task.executeCommandSync("start");
// Read data
const streamer = await client.openStreamer([
"voltage_chan",
"current_chan",
"temp_chan",
]);
for (let i = 0; i < 10; i++) {
const frame = await streamer.read();
console.log(frame);
}
// Stop task
await task.executeCommandSync("stop");
await streamer.close(); Edit task configuration
// Retrieve existing task
const task = await client.tasks.retrieve({ name: "Analog Read Task" });
// Parse and update configuration
const config = JSON.parse(task.config);
// Update task-level configuration
config.auto_start = true;
config.stream_rate = 50;
// Update voltage channel configuration
config.channels[0].port = 1;
config.channels[0].min_val = -5.0;
config.channels[0].max_val = 5.0;
// Update current channel configuration
config.channels[1].port = 2;
config.channels[1].min_val = 0.002;
config.channels[1].max_val = 0.01;
// Update thermocouple channel configuration
config.channels[2].port = 3;
config.channels[2].thermocouple_type = "K";
// Apply changes
await client.tasks.create({
key: task.key,
name: task.name,
type: task.type,
config: JSON.stringify(config),
});