Skip to content
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

Can read channel_names of PersistedTask but not channels #216

Closed
robin-po opened this issue Mar 2, 2023 · 4 comments · Fixed by #217
Closed

Can read channel_names of PersistedTask but not channels #216

robin-po opened this issue Mar 2, 2023 · 4 comments · Fixed by #217

Comments

@robin-po
Copy link

robin-po commented Mar 2, 2023

I created an analog output task in MAX named "AO" and 2 channels inside this task called "EV01" and "EV02".

I tried to launch this basic code :

import nidaqmx

task: nidaqmx.Task = nidaqmx.system.storage.persisted_task.PersistedTask('AO').load()

try:
    print(task.channel_names)
    print(task.channels)
finally:
    task.close()

Output :

['EV01', 'EV02']
Traceback (most recent call last):
  File "...\for_test\daq_test.py", line 7, in <module>
    print(task.channels)
          ^^^^^^^^^^^^^
  File "...\venv\Lib\site-packages\nidaqmx\task.py", line 163, in channels
    return Channel._factory(
           ^^^^^^^^^^^^^^^^^
  File "...\venv\Lib\site-packages\nidaqmx\_task_modules\channels\channel.py", line 115, in _factory
    check_for_error(error_code)
  File "...\venv\Lib\site-packages\nidaqmx\errors.py", line 196, in check_for_error
    raise DaqError(error_buffer.value.decode('utf-8'), error_code)
nidaqmx.errors.DaqError: Specified channel is not in the task.
Channel Name: EV1

Task Name: AO

Status Code: -200486

It seems that I can read chanel_names but on reading channels it omits the "0"

Python version : 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]
NI-DAQmx version : 2022 Q3

@zhindes
Copy link
Collaborator

zhindes commented Mar 2, 2023

Oof, good catch. Sorry about that. The problem is that flatten_channel_string doesn't handle leading 0 in a channel name correctly.

from nidaqmx.utils import flatten_channel_string

chans = ["EV01", "EV02"]
print(f"{chans} flattened: {flatten_channel_string(chans)}")

That code produces this incorrect output:

['EV01', 'EV02'] flattened: EV1:2

@zhindes
Copy link
Collaborator

zhindes commented Mar 2, 2023

@robin-po - The workaround would be to rename your channels. The fix is to improve our flatten_channel_string implementation in utils.py. I'm not sure when I'll get to that, but if you wanted to give it a shot (and add some tests!), I would appreciate the contribution :)

@zhindes
Copy link
Collaborator

zhindes commented Mar 2, 2023

Got bored. Tentative fix here: https://github.com/zhindes/nidaqmx-python/tree/fix_flatten_unflatten_leading_zeros. Need to add more test cases and then validate all the other tests still work.

@robin-po
Copy link
Author

robin-po commented Mar 3, 2023

The fix works pretty well on my specific names, thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants