From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

Asynchronous Conter Read

i have written a program in vb.net and would like to capture 14 different times.  I would like to do an asynchronous read for each counter because each counter will give me random times.  I will use the CounterReader.BeginReadSingleSampleDouble(AddressOf OnDataReady, Nothing).  I am having a problem with being able to distinguish one couter time from another.  Being that the timers may not always finish in the same order i will have to keep track of each time some how.  My question is how will the OnDataReady function know which time goes with which variable.  I know that i have to put an object in where "Nothing" is but i'm not sure on how to do that and then confirm that the correct times match up with the correct timer.
0 Kudos
Message 1 of 10
(4,133 Views)
I think you can check the virtual channel name to determine which counter channel has returned with a value. If I have read my documentation correctly, if you don't set the virtual name, then the physical name will also become the virtual name and so you should be able to test to that.

Could be wrong though as I can't consistently get a value back for my counters....

cm
0 Kudos
Message 2 of 10
(4,116 Views)

Joe,

What hardware are you using, and can you please elaborate on ’14 different times’. Does this mean you want to capture one counter 14 different times, a different counter each time for 14 times, etc?

 

You can find a good shipping example here: C:\Program Files\National Instruments\MeasurementStudioVS2005\DotNET\Examples\DAQmx\Counter\Count Digital Events\CountDigEventsBuffContinuous_ExtClk\vb

The counter data will be put into an array, which is obtained during the function callback of BeginReadSingleSampleDouble(). More information on how the counter data is put into this array can be found in the help file. For the program above, the counter data is obtained and then the callback function is called. Inside that callback function, it reads the counters again and calls itself. This makes it a continuous loop.

David L.
Systems Engineering
National Instruments
0 Kudos
Message 3 of 10
(4,117 Views)

I have 2 PCI-6602 boards and am using 14 different counters.  The problem is that counter 1 through 14 should be tripped in order, but if they are not or if one counter is not tripped at all then i would like to disregard the counter that is not tripped.  The program that i have written works if all the counters are tripped.  The problem is if one is missed.  If one is misssed then the response time of the program is extremly slow and unreliable.  I am using the BeginReadSampleDouble method of CounterReader and calling the method EndReadSingleSampleDouble() to receive the data and ends the read.  The problem is if one of the counters is not tripped then it never ends the read.  So what i do is wait in a loop to until at least one of the 14 counters is tripped and then i wait 2 seconds.  *I am not sure what the time is for each counter, but what is do know is that it will not take more than 0.75 second for the last counter to be tripped.  So by waiting 2 seconds i should have gotten all of the couters that were tripped, and if any are not tripped then i set the variable pertaining to that counter to -1.  After all the counter stopped and read for the times all of the task must be stopped and disposed of.  This is were the problem occurs.  I a counter is not tripped then when is gets to the routine where the task are stopped and disposed then it seems to lock up or take very long to occur.  So what i really need to know is how do i stop a BeginReadSingleSampleDouble or set a time out for it.  Below is the code. 

 

0 Kudos
Message 4 of 10
(4,084 Views)

Private Sub measureButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles measureButton.Click

Dim i As Integer

SaveLabel.Visible =

False

Try

measureButton.Enabled =

False

Call SetUpArray()

'setup timing parameters

Dim MinTime As Double = 0.000001 'this is the minimum time to count

Dim MaxTime As Double = 2 'this is the maximum time to wait for a signal

'Clear all times

For i = 1 To 14

Times(i) = 0

Next

'Create task, configure, and start each task. 1 channel per task

For i = 1 To 14

TaskNum(i) =

New Task

TaskNum(i).CIChannels.CreateTwoEdgeSeparationChannel(DeviceName(i), _

"", MinTime, MaxTime, CITwoEdgeSeparationFirstEdge.Rising, CITwoEdgeSeparationSecondEdge.Rising, _

CITwoEdgeSeparationUnits.Seconds)

TaskNum(i).Start()

Next i

For i = 1 To 14

StartRead(TaskNum(i), i)

Next (i)

Dim GotSomeTimes As Boolean = False

Do Until GotSomeTimes

DoEvents()

For i = 1 To 14

If Times(i) <> 0 Then GotSomeTimes = True

Next

Loop

Time1 = Now.Second

Delay = 2

If Time1 >= 60 - Delay Then Time1 = 0

Do Until Now.Second >= Time1 + Delay

DoEvents()

Loop

'If a screen is not tripped then return (-1)

For i = 1 To 14

If Times(i) = 0 Then

Times(i) = -1

Dim ReadIn As String

End If

Next

'display values

'Error Handeling sequence

Catch

Dim exception As DaqException

MessageBox.Show(Exception.Message)

Finally

'this will end all task

For i = 1 To 14

TaskNum(i).Stop()

TaskNum(i).Dispose()

Next i

measureButton.Enabled =

True

End Try

End Sub

Public Sub StartRead(ByVal t As Task, ByVal i As Integer)

counterInReader(i) =

New CounterReader(t.Stream)

Dim handle as IAsyncResult

handle = counterInReader(i).BeginReadSingleSampleDouble(AddressOf OnDataReady, i)

End Sub

Public Sub OnDataReady(ByVal x As IAsyncResult)

Dim ReadIn As String

ReadIn = x.AsyncState

'this is the value of the second parameter for function BeginReadSingleSampleDouble

'This is how the function distinguishes between async. responses.

Times(ReadIn) = counterInReader(ReadIn).EndReadSingleSampleDouble(x)

End Sub

Sub SetUpArray()

End Class

     

0 Kudos
Message 5 of 10
(4,080 Views)

You can use a timeout in order to prevent the program from waiting a long time until you get a tripped timer. The timeout property is set by getting a Stream object from your Task, and then setting the Timeout property of Stream.

David L.
Systems Engineering
National Instruments
Message 6 of 10
(4,073 Views)

David,

 

Thank you for your help it was useful.  It works much better but is inconsistent.  Sometimes the program will get stuck on the task.stop if a time is not received, but the task.stream.timeout should have taken care of that.  I have a question. The help file says "Gets or sets the amount of time in milliseconds to wait for reads or writes to complete." but it doesn't say when the timeout start to count.  Is it when the read starts or when the timeout value is set? 

 

0 Kudos
Message 7 of 10
(4,056 Views)

Joe,

The counter timeout is started when the BeginReadSingleSampleDouble() is called. One recommended change to your code, I would highly recommend putting the New CounterReader(t.Stream) in the upper while loop, as putting it in the bottom while loop can affect the timing and contribute to the errors you are seeing.

David L.
Systems Engineering
National Instruments
Message 8 of 10
(4,028 Views)

David,

Thank you for the info.

Unfortunatley putting the New CounterReader(t.Stream) in the upper while loop did not help.  The program is still unpredictable so i went back to just doing a regual real time stream of the task and setting a timeout for the stream.  This will allow me to get all of the time if they are good, and if i miss one or more then it will return -1 for the first counter missed and whatever counter is after the first missed counter will also return -1.  This way i will atleast know where the first problem occured.  I will utilize the program like this for now but i am hoping to be able to get consistent reads by way of asynchronous.  The way i am doing this is that i have written a program into a basic stamp chip which spits out 14 different pulses at a know interval.  I then feed these pulses into the PCI-6602 boards.  I have no problems at all when i read all the times (14) in using the real time read ReadSingleSampleDouble(), but when i use the async. read then sometimes i miss some readings, even when all the times(14) are tripped.  This is to inconsistent for me to use so I would like to know if i can contact you or somebody else at National Istruments to talk to over the phone about this problem in more detail. 

Thank you       

0 Kudos
Message 9 of 10
(4,008 Views)

Joe,

If you would like phone support, you may call 1-866-IEEE-488 (433-3488). Our sales team will help in getting you the best support for this issue.

David L.
Systems Engineering
National Instruments
0 Kudos
Message 10 of 10
(4,003 Views)