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

examples: Check for DAQmx event registration errors #928

Merged
merged 3 commits into from
May 25, 2023
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
11 changes: 9 additions & 2 deletions examples/nidaqmx/analog-input-every-n-samples-aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ def check_for_warning(response):
f"{warning_message.error_string}\nWarning status: {response.status}\n"
)

async def check_for_stream_error(stream):
"""Raise an exception if the stream was closed with an error."""
if stream.done() and await stream.code() != grpc.StatusCode.OK:
_ = await stream.read()

try:
create_task_response = await client.CreateTask(nidaqmx_types.CreateTaskRequest())
task = create_task_response.task
Expand Down Expand Up @@ -97,15 +102,17 @@ def check_for_warning(response):
)
)

# Wait for initial_metadata to ensure that the callback is registered before starting
# the task.
# Wait for initial_metadata and check for stream errors to ensure that the callback is
# registered successfully before starting the task.
await every_n_samples_stream.initial_metadata()
await check_for_stream_error(every_n_samples_stream)

done_event_stream = client.RegisterDoneEvent(
nidaqmx_types.RegisterDoneEventRequest(task=task)
)

await done_event_stream.initial_metadata()
await check_for_stream_error(done_event_stream)

start_task_response = await client.StartTask(nidaqmx_types.StartTaskRequest(task=task))
check_for_warning(start_task_response)
Expand Down
11 changes: 9 additions & 2 deletions examples/nidaqmx/analog-input-every-n-samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ def check_for_warning(response):
f"{warning_message.error_string}\nWarning status: {response.status}\n"
)

def check_for_stream_error(stream):
"""Raise an exception if the stream was closed with an error."""
if stream.done() and stream.exception() is not None:
raise stream.exception()

try:
create_task_response = client.CreateTask(nidaqmx_types.CreateTaskRequest())
task = create_task_response.task
Expand Down Expand Up @@ -99,15 +104,17 @@ def check_for_warning(response):
)
)

# Wait for initial_metadata to ensure that the callback is registered before starting
# the task.
# Wait for initial_metadata and check for stream errors to ensure that the callback is
# registered successfully before starting the task.
every_n_samples_stream.initial_metadata()
check_for_stream_error(every_n_samples_stream)

done_event_stream = client.RegisterDoneEvent(
nidaqmx_types.RegisterDoneEventRequest(task=task)
)

done_event_stream.initial_metadata()
check_for_stream_error(done_event_stream)

start_task_response = client.StartTask(nidaqmx_types.StartTaskRequest(task=task))
check_for_warning(start_task_response)
Expand Down