Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I change duty cycle on the fly using DAQmx and VB.NET?

I tried to use code that was taken from C#, but it didn't work.
 

Public Sub SFI(ByVal PDC As Single, ByVal Channel As String)

Dim writer As CounterSingleChannelWriter

writer =

New CounterSingleChannelWriter(Tasks(1).Stream)

If SFIOn Then

writer.WriteSingleSample(

True, New CODataFrequency(1, PDC))

Else

Try

Tasks(1) =

New Task("COtask")

Tasks(1).COChannels.CreatePulseChannelFrequency(Channel, "ContinuousPulseTrain", _

COPulseFrequencyUnits.Hertz, COPulseIdleState.Low, 0.0, 1, PDC)

Tasks(1).Timing.ConfigureImplicit(SampleQuantityMode.ContinuousSamples)

Tasks(1).Start()

SFIOn =

True

Catch ex As System.Exception

MessageBox.Show(ex.Message)

Finally

Tasks(1).Dispose()

End Try

End If

End Sub

 

 

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 1 of 12
(6,093 Views)

Nevermind.  I had to pull the Tasks(1).Dispose out of the Finally part of the try catch statement.

 

As in....get rid of it so that there was a task to update....

Message Edited by SCXI and MS 2k3-VB.NET on 11-09-2005 02:23 PM

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 2 of 12
(6,090 Views)
Well.  I got the code to work but I am getting an error.  It is throwing an exception that says I must complete one full cycle before updating the duty cycle.  My frequency is 40 Hz and I am changing the duty cycle at 1 Hz.  How is it not making a complete cycle?  How do I get around this?
 
Thanks.
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 3 of 12
(6,051 Views)
Hello SCXI and MS 2k3-VB.NET,

Here is an article discussing the error you are getting.

It is also important to note than when you are changing the Duty Cycle property, you also have to write a new value to the Frequency property even if you are not changing the frequency.  I modified a shipping example installed by the DAQmx driver in order to do this.  The example program is located here:

C:\Program Files\National Instruments\MeasurementStudioVS2003\DotNET\Examples\DAQmx\Counter\Generate Pulse\GenDigPulseTrain_Continuous\Vb

I added the following two lines of code as lines 339 and 340 (in the Private Sub statusCheckTimer_Tick function):

myTask.COChannels.All.PulseDutyCycle = Convert.ToDouble(dutyCycleTextBox.Text)
myTask.COChannels.All.PulseFrequency = Convert.ToDouble(frequencyTextBox.Text)

I was able to change the duty cycle of the pulse train on the fly.

*Note: If you accidentally type a 0 into the frequencyTextBox (or any other invalid value), you will keep getting errors.  I just did this as a quick test, so you probably want to change the implementation to suit your own needs.

Message Edited by ELee [DE] on 12-01-2005 03:09 PM

Eric
DE For Life!
0 Kudos
Message 4 of 12
(6,028 Views)
Looks like I goofed and enter the period instead of the frequency.  I had .025 which was too low of a frequency to make it through it's cycle in 1 second.  Your method for changing the frequency is a little different that the one I had found.  Who should I use?
 

Tasks(16).COChannels.All.PulseDutyCycle = Convert.ToDouble(PDC)

Tasks(16).COChannels.All.PulseFrequency = Convert.ToDouble(40)

of

ctrWriter.WriteSingleSample(True, New CODataFrequency(1, PDC))

 

Thanks.

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 5 of 12
(6,020 Views)
Both approaches are valid and will accomplish the same results...so you can use either one! Smiley Very Happy
Eric
DE For Life!
0 Kudos
Message 6 of 12
(6,019 Views)
Actually, you should use the Write methods rather than the properties. The properties continue to work, but they are a little quirky to use since there is an order dependency with them. Setting the Frequency property causes the values of both properties to be applied to the signal, so if you set Frequency, then Duty Cycle, you get something different than if you set Duty Cycle, then Frequency. It was this sort of oddity that we attempted to fix by introducing methods to modify the pulse parameters.

-- Chris
DAQmx R&D, NI
0 Kudos
Message 7 of 12
(5,993 Views)
Thanks.
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 8 of 12
(5,993 Views)

I am having problems with this again and it is setting my duty cycle to 100%.  This is a problem when I am injecting fuel into an exhaust system!  I set a loop at the beginning of the subroutine similar to this:

subclock = now.millisecond
do
  
while now.millisecond - subclock < 50

ctrWriter.WriteSingleSample(True, New CODataFrequency(1, PDC))

Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 9 of 12
(5,889 Views)
The frequency I am using is 40 Hz, not 1 Hz.
Programming Data Acquisition and Control in Measurement Studio and Labwindows/CVI
0 Kudos
Message 10 of 12
(5,885 Views)