NI counter read task
Read frequency, edge counts, pulse widths, and encoder positions from NI counters into channels.
An NI counter read task reads the counter inputs of NI devices into channels. A counter
measures digital pulses: their frequency, their count, their width, or the position and
speed of an encoder. For example, it can record the speed of a flow meter on ctr0 of a
USB-6343.
Before you start, you need configured devices.
Counters and timing
Each channel reads one counter, set by its Port: port 0 is ctr0. The pulses come
in on a PFI terminal. Set Input terminal to choose the terminal, or leave it empty
to use the default terminal of the counter. The device manual lists the counters and
their terminals.
The Driver reads the counters on the computer clock, at the Sample rate. See timing.
Create an NI counter read task
Console
Python
TypeScript
- In the devices toolbar, right-click a device and select Create counter read task.
- Set Sample rate and Stream rate.
- Click Add channel. In Source, select the Device and the Port.
- In Signal, select the Channel type, then set its fields. See the channel reference.
- Click (Start).
The Console creates the channels. For a device with the identifier dev1, counter 0
gives dev1_ctr_0, with the index channel dev1_ctr_time.
Python does not create channels. Create them first.
import synnax as sy
from synnax import ni
dev = client.devices.retrieve(key="<device key>")
time = client.channels.create(
name="dev1_ctr_time", is_index=True, data_type=sy.DataType.TIMESTAMP
)
flow = client.channels.create(
name="flow_meter_hz", index=time.key, data_type=sy.DataType.FLOAT64
)
task = ni.CounterReadTask(
name="Flow meter",
device=dev.key,
sample_rate=100,
stream_rate=10,
channels=[
ni.CIFrequencyChannel(port=0, min_val=1, max_val=1000, channel=flow.key)
],
)
client.tasks.configure(task)
task.start()Python creates the task on the embedded Driver of the Core. For a device on another Driver, use the Console or TypeScript. If you open the task in the Console and click Start, the Console replaces your channels with its own.
TypeScript does not create channels. Create them first, as in the Python example.
import { ni } from "@synnaxlabs/client";
const dev = await client.devices.retrieve({ key: "<device key>" });
const task = await client.tasks.create({
name: "Flow meter",
rack: dev.rack,
type: "ni_counter_read",
config: ni.counterReadConfigZ.parse({
sampleRate: 100,
streamRate: 10,
channels: [
{
type: "ci_frequency",
device: dev.key,
port: 0,
minVal: 1,
maxVal: 1000,
channel: flow.key,
},
],
}),
});
await task.start();Tare channels
Tare sets the current value of a channel to zero. The task must be running.
- In the channel list, click Tare on a channel. To tare several channels, select them, right-click, and select Tare.
- The values of those channels start from zero.
Task options
Channel reference
Every channel has a channel, the Synnax channel that records the value, a device,
the key of the device (Device), and a port, the counter number (Port). In
Python, a channel with no device uses the device of the task.
Each table shows the Console label of a setting, with its Python field under it. The Notes column lists the values that code uses. The Console shows the same values in plain words. Every type except edge count also has Custom scaling.
Pulse
Encoder
Custom scaling
Channels with Custom scaling convert the raw value before the Driver writes it. In
the Console, the options are in the Scale section of the channel. In code, set
custom_scale to one of these scales:
Every scale except None also has Prescaled units (pre_scaled_units, Volts)
and Scaled units (scaled_units).
Troubleshooting
- Cause
Two channels use the same counter of one device.
- Fix
Change the port of one of them.
- Cause
A Python channel has no
device, and the task has nodeviceeither.- Fix
Set one of them.
- Cause
The channels use devices on two Drivers.
- Fix
Create one task for each Driver.
For other problems, see Configure an NI device and Troubleshoot hardware.