02-25-2020 06:41 AM
Dear community,
I'm relatively new to NI and it Python API, so bare with me. I'm working on the control of a tank system. In the pas,t I used a NI card in the computer and on demand measuring to get the data I needed.
Now, we switched to NI 9202 and NI9264. When setting up the program, I wanted to use:
.read_one_sample() as I did before, but got the following error message:
DaqError: Task contains physical channels on one or more devices that require you to specify the Sample Clock rate. Use the Sample Clock Timing function/VI to specify a Sample Clock rate.
You cannot specify a Sample Clock rate if Mode is set to On Demand.
Based on that I switched the mode to sample rate and set it to 2 samples per channel. I call .read_one_sample() twice to get the newest data. It's working although I'm aware that it's not a nice solution. Furthermore, I realized that my controller loop is much slower now. I guess it wouldn't be a slow network problem. How can I set up a measurement method that is similar to on_demand while using a sample clock which seems to be needed by my hardware.
Regards,
Tim
02-26-2020 05:33 AM
Dear community,
I think I can ask my question more clearly now. I guess the question would be "how can I read the most recent sample?"
I found this in the API documentation:
OVERWRITE_UNREAD_SAMPLES
= 10252
When an acquisition encounters unread data in the buffer, the acquisition continues and overwrites the unread samples with new ones. You can read the new samples by setting relative_to to ReadRelativeTo.MOST_RECENT_SAMPLE and setting offset to the appropriate number of samples.
I tried to implement it like this:
ni.constants.OverwriteMode = 10252 # overwrite unread samples
ni.constants.ReadRelativeTo = 10428 # read most recent sample
temp3=ni.Task()
temp3.ai_channels.add_ai_voltage_chan(Mod1 + "/ai1")
temp3.timing.cfg_samp_clk_timing(rate = sample_rate)
temp3.in_stream.offset = -1 # read relative to offset
temp3_reader = AnalogSingleChannelReader(temp3.out_stream)
temp3_reading = temp3_reader.read_one_sample()
Now, I get an error message that -1 as an offset doesn't work. I guess there is some fundamental that I don't understand.
Thank you,
Tim
02-26-2020 08:46 AM
I don't know details of that text API (Python?) , but what you're trying to do is generally supported. I see where you set the property temp3.in_stream.offset=-1, but don't see where you set the corresponding property for ReadRelativeTo. I see where you assign a value to a constant but you aren't configuring the data acq task with it. Same for the OverwriteMode.
It also looks a little odd to me (though again, I don't know the API), that you create a Reader by passing in an "out_stream" rather than an "in_stream".
-Kevin P
02-26-2020 02:39 PM
Hello Kevin,
thank you for trying to help me and I'm sorry that I couldn't ask my question in a better, clearer way.
I just fixed the mistake about the "in_stram" that you pointed out, but it didn't solve my problem. What exactly do you mean by "you aren't configuring the data acq task" ? I thought I set those constants and then they apply to all my tasks. Now, that I think about it, it makes sense that those constants (and therefore the attributes) can be set individually for different tasks.
I thought that the following line, sets all tasks to "read most recent":
ni.constants.ReadRelativeTo = 10428 # read most recent sample
As that seems to be wrong, what's the correct syntax? I know that you don't know the API, so maybe someone else can help me here.
Regards,
Tim
02-26-2020 03:56 PM
Help, anyone who knows this API?
Meanwhile, here are some best guesses. I would *expect* you to need to set the OverwriteMode and ReadRelativeTo in a way very similar to the way you set the offset value. The settings would attach to one specific data acq task. Setting the value to some globally-applicable constant is not at all similar to the way one does this from LabVIEW. In fact, I'm a little concerned to see code that assigns values to things that identify themselves as ni constants.
In any event, my guesses at code mods are seen below in red
# ni.constants.OverwriteMode = 10252 # overwrite unread samples
# ni.constants.ReadRelativeTo = 10428 # read most recent sample
temp3=ni.Task()
temp3.ai_channels.add_ai_voltage_chan(Mod1 + "/ai1")
temp3.timing.cfg_samp_clk_timing(rate = sample_rate)
temp3.in_stream.offset = -1 # read relative to offset
# these next two lines are merely guesses at the syntax
temp3.in_stream.OverwriteMode = 10252 # overwrite unread samples in this task
temp3.in_stream.ReadRelativeTo = 10428 # overwrite unread samples in this task
temp3_reader = AnalogSingleChannelReader(temp3.in_stream)
temp3_reading = temp3_reader.read_one_sample()
-Kevin P