Skip to content

fix virtual channel name logic with library interpreter and DAQmx 24.5+ #723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ All notable changes to this project will be documented in this file.

* ### Resolved Issues
* Fix PEP 660 builds by setting `build-system` to use `poetry-core`
* [579: nidaqmx does not generate numbered virtual channel names correctly](https://github.com/ni/nidaqmx-python/issues/579)

* ### Major Changes
* Removed the `docs` extra and converted it to a Poetry dependency group.
Expand Down
4 changes: 4 additions & 0 deletions generated/nidaqmx/_base_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,10 @@ def get_write_attribute_uint32(self, task, attribute):
def get_write_attribute_uint64(self, task, attribute):
raise NotImplementedError

@abc.abstractmethod
def internal_get_last_created_chan(self):
raise NotImplementedError

@abc.abstractmethod
def is_task_done(self, task):
raise NotImplementedError
Expand Down
3 changes: 3 additions & 0 deletions generated/nidaqmx/_grpc_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3597,6 +3597,9 @@ def set_runtime_environment(
self, environment, environment_version, reserved_1, reserved_2):
raise NotImplementedError

def internal_get_last_created_chan(self):
raise NotImplementedError


def _assign_numpy_array(numpy_array, grpc_array):
"""
Expand Down
24 changes: 24 additions & 0 deletions generated/nidaqmx/_library_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4048,6 +4048,30 @@ def get_write_attribute_uint64(self, task, attribute):
self.check_for_error(error_code)
return value.value

def internal_get_last_created_chan(self):
cfunc = lib_importer.windll.DAQmxInternalGetLastCreatedChan
if cfunc.argtypes is None:
with cfunc.arglock:
if cfunc.argtypes is None:
cfunc.argtypes = [
ctypes.c_char_p, ctypes.c_uint]

temp_size = 0
while True:
value = ctypes.create_string_buffer(temp_size)
size_or_code = cfunc(
value, temp_size)
if is_string_buffer_too_small(size_or_code):
# Buffer size must have changed between calls; check again.
temp_size = 0
elif size_or_code > 0 and temp_size == 0:
# Buffer size obtained, use to retrieve data.
temp_size = size_or_code
else:
break
self.check_for_error(size_or_code)
return value.value.decode(lib_importer.encoding)

def is_task_done(self, task):
is_task_done = c_bool32()

Expand Down
31 changes: 21 additions & 10 deletions generated/nidaqmx/task/collections/_ai_channel_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy

from nidaqmx.errors import DaqFunctionNotSupportedError
from nidaqmx.task.channels._ai_channel import AIChannel
from nidaqmx.task.collections._channel_collection import ChannelCollection
from nidaqmx.utils import unflatten_channel_string
Expand Down Expand Up @@ -45,18 +46,28 @@ def _create_chan(self, physical_channel, name_to_assign_to_channel=''):

Specifies the newly created AIChannel object.
"""
if name_to_assign_to_channel:
num_channels = len(unflatten_channel_string(physical_channel))

if num_channels > 1:
name = '{}0:{}'.format(
name_to_assign_to_channel, num_channels-1)
# Attempt to retrieve the last created channel name. This is only supported on DAQmx 24Q3+ with the library
# interpreter.
virtual_channel_name = None
try:
virtual_channel_name = self._interpreter.internal_get_last_created_chan()
except (NotImplementedError, DaqFunctionNotSupportedError):
pass

# Fallback implementation is sometimes incorrect.
if virtual_channel_name is None:
if name_to_assign_to_channel:
num_channels = len(unflatten_channel_string(physical_channel))

if num_channels > 1:
virtual_channel_name = '{}0:{}'.format(
name_to_assign_to_channel, num_channels-1)
else:
virtual_channel_name = name_to_assign_to_channel
else:
name = name_to_assign_to_channel
else:
name = physical_channel
virtual_channel_name = physical_channel

return AIChannel(self._handle, name, self._interpreter)
return AIChannel(self._handle, virtual_channel_name, self._interpreter)

def add_ai_accel_4_wire_dc_voltage_chan(
self, physical_channel, name_to_assign_to_channel="",
Expand Down
31 changes: 21 additions & 10 deletions generated/nidaqmx/task/collections/_ao_channel_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Do not edit this file; it was automatically generated.

from nidaqmx.errors import DaqFunctionNotSupportedError
from nidaqmx.task.channels._ao_channel import AOChannel
from nidaqmx.task.collections._channel_collection import ChannelCollection
from nidaqmx.utils import unflatten_channel_string
Expand Down Expand Up @@ -31,18 +32,28 @@ def _create_chan(self, physical_channel, name_to_assign_to_channel=''):

Specifies the newly created AOChannel object.
"""
if name_to_assign_to_channel:
num_channels = len(unflatten_channel_string(physical_channel))

if num_channels > 1:
name = '{}0:{}'.format(
name_to_assign_to_channel, num_channels-1)
# Attempt to retrieve the last created channel name. This is only supported on DAQmx 24Q3+ with the library
# interpreter.
virtual_channel_name = None
try:
virtual_channel_name = self._interpreter.internal_get_last_created_chan()
except (NotImplementedError, DaqFunctionNotSupportedError):
pass

# Fallback implementation is sometimes incorrect.
if virtual_channel_name is None:
if name_to_assign_to_channel:
num_channels = len(unflatten_channel_string(physical_channel))

if num_channels > 1:
virtual_channel_name = '{}0:{}'.format(
name_to_assign_to_channel, num_channels-1)
else:
virtual_channel_name = name_to_assign_to_channel
else:
name = name_to_assign_to_channel
else:
name = physical_channel
virtual_channel_name = physical_channel

return AOChannel(self._handle, name, self._interpreter)
return AOChannel(self._handle, virtual_channel_name, self._interpreter)

def add_ao_current_chan(
self, physical_channel, name_to_assign_to_channel="", min_val=0.0,
Expand Down
31 changes: 21 additions & 10 deletions generated/nidaqmx/task/collections/_ci_channel_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Do not edit this file; it was automatically generated.

from nidaqmx.errors import DaqFunctionNotSupportedError
from nidaqmx.task.channels._ci_channel import CIChannel
from nidaqmx.task.collections._channel_collection import ChannelCollection
from nidaqmx.utils import unflatten_channel_string
Expand Down Expand Up @@ -33,18 +34,28 @@ def _create_chan(self, counter, name_to_assign_to_channel=''):

Specifies the newly created CIChannel object.
"""
if name_to_assign_to_channel:
num_counters = len(unflatten_channel_string(counter))

if num_counters > 1:
name = '{}0:{}'.format(
name_to_assign_to_channel, num_counters-1)
# Attempt to retrieve the last created channel name. This is only supported on DAQmx 24Q3+ with the library
# interpreter.
virtual_channel_name = None
try:
virtual_channel_name = self._interpreter.internal_get_last_created_chan()
except (NotImplementedError, DaqFunctionNotSupportedError):
pass

# Fallback implementation is sometimes incorrect.
if virtual_channel_name is None:
if name_to_assign_to_channel:
num_counters = len(unflatten_channel_string(counter))

if num_counters > 1:
virtual_channel_name = '{}0:{}'.format(
name_to_assign_to_channel, num_counters-1)
else:
virtual_channel_name = name_to_assign_to_channel
else:
name = name_to_assign_to_channel
else:
name = counter
virtual_channel_name = counter

return CIChannel(self._handle, name, self._interpreter)
return CIChannel(self._handle, virtual_channel_name, self._interpreter)

def add_ci_ang_encoder_chan(
self, counter, name_to_assign_to_channel="",
Expand Down
31 changes: 21 additions & 10 deletions generated/nidaqmx/task/collections/_co_channel_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Do not edit this file; it was automatically generated.

from nidaqmx.errors import DaqFunctionNotSupportedError
from nidaqmx.task.channels._co_channel import COChannel
from nidaqmx.task.collections._channel_collection import ChannelCollection
from nidaqmx.utils import unflatten_channel_string
Expand Down Expand Up @@ -31,18 +32,28 @@ def _create_chan(self, counter, name_to_assign_to_channel=''):

Specifies the newly created COChannel object.
"""
if name_to_assign_to_channel:
num_counters = len(unflatten_channel_string(counter))

if num_counters > 1:
name = '{}0:{}'.format(
name_to_assign_to_channel, num_counters-1)
# Attempt to retrieve the last created channel name. This is only supported on DAQmx 24Q3+ with the library
# interpreter.
virtual_channel_name = None
try:
virtual_channel_name = self._interpreter.internal_get_last_created_chan()
except (NotImplementedError, DaqFunctionNotSupportedError):
pass

# Fallback implementation is sometimes incorrect.
if virtual_channel_name is None:
if name_to_assign_to_channel:
num_counters = len(unflatten_channel_string(counter))

if num_counters > 1:
virtual_channel_name = '{}0:{}'.format(
name_to_assign_to_channel, num_counters-1)
else:
virtual_channel_name = name_to_assign_to_channel
else:
name = name_to_assign_to_channel
else:
name = counter
virtual_channel_name = counter

return COChannel(self._handle, name, self._interpreter)
return COChannel(self._handle, virtual_channel_name, self._interpreter)

def add_co_pulse_chan_freq(
self, counter, name_to_assign_to_channel="",
Expand Down
45 changes: 29 additions & 16 deletions generated/nidaqmx/task/collections/_di_channel_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Do not edit this file; it was automatically generated.

from nidaqmx.errors import DaqFunctionNotSupportedError
from nidaqmx.task.channels._di_channel import DIChannel
from nidaqmx.task.collections._channel_collection import ChannelCollection
from nidaqmx.utils import unflatten_channel_string
Expand Down Expand Up @@ -34,25 +35,37 @@ def _create_chan(self, lines, line_grouping, name_to_assign_to_lines=''):

Specifies the newly created DIChannel object.
"""
unflattened_lines = unflatten_channel_string(lines)
num_lines = len(unflattened_lines)

if line_grouping == LineGrouping.CHAN_FOR_ALL_LINES:
if name_to_assign_to_lines or num_lines == 1:
name = lines
else:
name = unflattened_lines[0] + '...'
else:
if name_to_assign_to_lines:
if num_lines > 1:
name = '{}0:{}'.format(
name_to_assign_to_lines, num_lines-1)
# Attempt to retrieve the last created channel name. This is only supported on DAQmx 24Q3+ with the library
# interpreter.
virtual_channel_name = None
try:
virtual_channel_name = self._interpreter.internal_get_last_created_chan()
except (NotImplementedError, DaqFunctionNotSupportedError):
pass

# Fallback implementation is sometimes incorrect.
if virtual_channel_name is None:
unflattened_lines = unflatten_channel_string(lines)
num_lines = len(unflattened_lines)

if line_grouping == LineGrouping.CHAN_FOR_ALL_LINES:
if name_to_assign_to_lines:
virtual_channel_name = name_to_assign_to_lines
elif num_lines == 1:
virtual_channel_name = lines
else:
name = name_to_assign_to_lines
virtual_channel_name = unflattened_lines[0] + '...'
else:
name = lines
if name_to_assign_to_lines:
if num_lines > 1:
virtual_channel_name = '{}0:{}'.format(
name_to_assign_to_lines, num_lines-1)
else:
virtual_channel_name = name_to_assign_to_lines
else:
virtual_channel_name = lines

return DIChannel(self._handle, name, self._interpreter)
return DIChannel(self._handle, virtual_channel_name, self._interpreter)

def add_di_chan(
self, lines, name_to_assign_to_lines="",
Expand Down
45 changes: 29 additions & 16 deletions generated/nidaqmx/task/collections/_do_channel_collection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Do not edit this file; it was automatically generated.

from nidaqmx.errors import DaqFunctionNotSupportedError
from nidaqmx.task.channels._do_channel import DOChannel
from nidaqmx.task.collections._channel_collection import ChannelCollection
from nidaqmx.utils import unflatten_channel_string
Expand Down Expand Up @@ -34,25 +35,37 @@ def _create_chan(self, lines, line_grouping, name_to_assign_to_lines=''):

Specifies the newly created DOChannel object.
"""
unflattened_lines = unflatten_channel_string(lines)
num_lines = len(unflattened_lines)

if line_grouping == LineGrouping.CHAN_FOR_ALL_LINES:
if name_to_assign_to_lines or num_lines == 1:
name = lines
else:
name = unflattened_lines[0] + '...'
else:
if name_to_assign_to_lines:
if num_lines > 1:
name = '{}0:{}'.format(
name_to_assign_to_lines, num_lines-1)
# Attempt to retrieve the last created channel name. This is only supported on DAQmx 24Q3+ with the library
# interpreter.
virtual_channel_name = None
try:
virtual_channel_name = self._interpreter.internal_get_last_created_chan()
except (NotImplementedError, DaqFunctionNotSupportedError):
pass

# Fallback implementation is sometimes incorrect.
if virtual_channel_name is None:
unflattened_lines = unflatten_channel_string(lines)
num_lines = len(unflattened_lines)

if line_grouping == LineGrouping.CHAN_FOR_ALL_LINES:
if name_to_assign_to_lines:
virtual_channel_name = name_to_assign_to_lines
elif num_lines == 1:
virtual_channel_name = lines
else:
name = name_to_assign_to_lines
virtual_channel_name = unflattened_lines[0] + '...'
else:
name = lines
if name_to_assign_to_lines:
if num_lines > 1:
virtual_channel_name = '{}0:{}'.format(
name_to_assign_to_lines, num_lines-1)
else:
virtual_channel_name = name_to_assign_to_lines
else:
virtual_channel_name = lines

return DOChannel(self._handle, name, self._interpreter)
return DOChannel(self._handle, virtual_channel_name, self._interpreter)

def add_do_chan(
self, lines, name_to_assign_to_lines="",
Expand Down
26 changes: 26 additions & 0 deletions src/codegen/metadata/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -17186,6 +17186,32 @@
'python_codegen_method': 'CustomCode',
'returns': 'int32'
},
'InternalGetLastCreatedChan': {
'calling_convention': 'StdCall',
'codegen_method': 'private',
'parameters': [
{
'ctypes_data_type': 'ctypes.c_char_p',
'direction': 'out',
'name': 'value',
'python_data_type': 'str',
'size': {
'mechanism': 'ivi-dance',
'value': 'size'
},
'type': 'char[]'
},
{
'ctypes_data_type': 'ctypes.c_uint32',
'direction': 'in',
'name': 'size',
'python_data_type': 'int',
'type': 'uInt32'
}
],
'python_codegen_method': 'CustomCode',
'returns': 'int32'
},
'IsTaskDone': {
'calling_convention': 'StdCall',
'handle_parameter': {
Expand Down
Loading