06-12-2006 03:12 PM
06-14-2006 10:57 PM
06-15-2006 08:39 AM
06-15-2006 08:41 AM
' Instantiate enough tasks for the number of modules being used.
For TaskCount = 0 To NumberOfModules - 1Tasks(TaskCount) =
New Task("mod" & Trim(Str(TaskCount + 1)) & "task") NextTaskCount = 0
ChannelCount = 0
TasksChannelCount(TaskCount) = 0
Device = "SC1Mod1"
' Create task(s) until all of the channels have been accounted for. While ChannelCount < numChannels ' Create a task until the current channels device property is equal to that of ' a different SCXI module. While AIChannels(ChannelCount).Device = DeviceTasksChannelCount(TaskCount) += 1
With AIChannels(ChannelCount) ' Input channels are either a voltage channel or a thermocouple channel. Select Case AIChannels(ChannelCount).Type Case Is = "Temperature"Tasks(TaskCount).AIChannels.CreateThermocoupleChannel(.Device & "/" & _
.Channel, Trim(.Name), .Min, .Max, AIThermocoupleType.K, _
AITemperatureUnits.DegreesC)
Tasks(TaskCount).AIChannels(.Name).AutoZeroMode = AIAutoZeroMode.None
Tasks(TaskCount).AIChannels(.Name).LowpassEnable =
TrueTasks(TaskCount).AIChannels(.Name).LowpassCutoffFrequency = 4
Case Is = "Voltage"Tasks(TaskCount).AIChannels.CreateVoltageChannel(.Device & "/" & .Channel, _
.Name, AITerminalConfiguration.Differential, _
.Min, .Max, AIVoltageUnits.Volts)
Tasks(TaskCount).AIChannels(.Name).AutoZeroMode = AIAutoZeroMode.None
Tasks(TaskCount).AIChannels(.Name).LowpassEnable =
TrueTasks(TaskCount).AIChannels(.Name).LowpassCutoffFrequency = 4
Case ElseMsgBox("Error in channel configuration!", MsgBoxStyle.Critical, "Warning!")
frmD.txtMessage.Text = "Error in Channel Configuration. Abort." & vbNewLine _
& frmD.txtMessage.Text
Exit Sub End Select End WithChannelCount += 1
End While ' Tell the task to take 100 readings of each channel at 10 kS/sec on the rising edge of the clock.Tasks(TaskCount).Timing.ConfigureSampleClock("", 10000, SampleClockActiveEdge.Rising, _
SampleQuantityMode.FiniteSamples, 100)
Tasks(TaskCount).Control(TaskAction.Verify)
' Create a reader object for this task.reader(TaskCount) =
New AnalogMultiChannelReader(Tasks(TaskCount).Stream)TaskCount += 1
TasksChannelCount(TaskCount) = 0
Device = AIChannels(ChannelCount).Device
End While06-15-2006 08:42 AM
g = h = i = 0
For Count = 0 To NumberOfModules - 1Task1Data = reader(Count).ReadMultiSample(100) 'ERROR HAPPENS HERE
For h = i To i + TasksChannelCount(Count) - 1sum(h) = 0
Next For h = i To i + TasksChannelCount(Count) - 1 For g = 0 To 99sum(h) = sum(h) + Task1Data(h - i, g)
Next Nexti = h
Next For h = 0 To numChannels With AIChannels(h)average(h) = sum(h) / 100
value(h) = average(h) ^ 4 * .Quartic + _
average(h) ^ 3 * .Cubic + _
average(h) ^ 2 * .Quadratic + _
average(h) * .Linear + _
.Offset
End With Next Catch ex As ExceptionswError =
New StreamWriter(Application.StartupPath & "\Errors\" & RunNumber & "_" & "Error Log.txt", True)swError.WriteLine(TimeString & vbTab & "ReadData" & vbNewLine & ex.ToString & vbNewLine & vbNewLine)
swError.Close()
frmD.ledError.Value =
True End Try End If End Sub06-15-2006 10:55 AM
06-15-2006 02:44 PM
06-15-2006 04:14 PM
>>>If I dispose the task will I be able to read data without creating the task again?>>>
Actually no it will not. So you will not be able to use this approach if you want to use the tasks in a loop.
Looking more closely at your code, the only reason i see that you will get this error, is if your ReadMultiSample does *not* read all of the points.
The ConfigureSampleClock is setting up for a 100 point read. You appear to be reading a 100 points. So that seems good. Is there any case in which the data that is read back from the device does *not* read all of the points? Because that would cause this behaviour to occur.
Basically when you Read during a finite acquisition, the hardware resources get reserved (automatically - beneath the hood) , the data is read, and the hardware gets unreserved (automatically). However if the finite read returns without reading all of the points specified by the ConfigureSampleClock (irrespective of what the Read says) then the hardware is not unreserved.
So if you do something like
ConfigureSampleClock(....,...<snip>, 500) and then
myReader.ReadMultiSample(100) ' 500 points not read, so hardware is not unreserved..
myReader2.ReadMultiSample(100) <== Now this read on a different task will fail because of the reserved resource error.
Is it possible that this could be happenning in your code. Can you check the size of the returned data and make sure that it is always the complete set of points. It appears from the code posted that it should, but its not the entire code, so its possible..
You could also cheat by unreserving the hardware,(mytask.Control(TaskAction.Unreserve) )
but we should be taking care of the state transitions for you, so if its not working out then we would certainly like to know.
I'll now attach the sample code i used to quickly test this...and to try and simulate what you were doing..
06-15-2006 04:15 PM
mytask.AIChannels.CreateVoltageChannel(
"Dev1/ai0:1", "", AITerminalConfiguration.Differential, -10.0, 10.0, AIVoltageUnits.Volts)mytask2.AIChannels.CreateCurrentChannel(
"Dev1/ai2:3", "", AITerminalConfiguration.Differential, 0.0, 0.001, 100, AICurrentUnits.Amps)mytask.Timing.ConfigureSampleClock(
"", 1000, SampleClockActiveEdge.Rising, _SampleQuantityMode.FiniteSamples, 1000)
mytask2.Timing.ConfigureSampleClock(
"", 1000, SampleClockActiveEdge.Rising, _SampleQuantityMode.FiniteSamples, 1000)
mytask.Control(TaskAction.Verify)
mytask2.Control(TaskAction.Verify)
Try Dim myReader As AnalogMultiChannelReader = New AnalogMultiChannelReader(mytask.Stream) Dim myReader2 As AnalogMultiChannelReader = New AnalogMultiChannelReader(mytask2.Stream) Dim readPoints As Double(,) = myReader.ReadMultiSample(10) ' 10 points of a 1000, so next line will generate an exception, if i change this to 1000 it will work fine.. Dim readPoints2 As Double(,) = myReader2.ReadMultiSample(1000) Catch exp As ExceptionMessageBox.Show(exp.Message)
End Try06-15-2006 04:50 PM