Digital I/O

cancel
Showing results for 
Search instead for 
Did you mean: 

Clearing read buffer after Digital Change Event USB 6229

I am using the Digital Change Event to read 2 limit switches on a motor. Because of how my application might be shut down, I have two goals for these switches:
1. Trigger an event when a switch is depressed or released (be able to determine which switch was pressed and if the transition was, high->low or low->high)
2. Read the current value of the switches at startup and at key points during the program

Using the following code, I can do most of number 1., but I can't do any of number 2 without the thread hanging until I press the switch to trigger the event.

I am having one main problem - when the switch is pressed, it bounces. This bouncing fills up the buffer or leaves remnants that are picked up by future change detections. For example, from the code below, you can see that I am counting every time a switch press sample is False. When I press the Pinch Switch, that counter will increment 2-5 times. If I then press the Release Switch, the Pinch count increments 2-5 times. If I keep pressing the Release Switch, the Pinch count will continue to increment until I have pressed the Release Switch ~10 times. Then the Release count will start incrementing regardless of which switch I press. If I continue to press the switches, eventually the system does not respond to switch presses.

Is there a way to clear out the Digital Read buffer after a read? I have also tried DigitalEventTask.Timing.SampleClockRate = 1-1000  with no effect.

  DigitalEventTask = New Task()
  DigitalEventTask.Stream.Timeout = -1
  DigitalEventTask.DIChannels.CreateChannel("MultiIO/port0/line0:1", "", ChannelLineGrouping.OneChannelForAllLines)
  DigitalEventTask.Timing.ConfigureChangeDetection("", "MultiIO/port0/line0:1", SampleQuantityMode.ContinuousSamples, 1000)
  DigitalEventTask.SynchronizeCallbacks = True
  AddHandler DigitalEventTask.DigitalChangeDetection, AddressOf DigitalChange_Event
  DigitalEventReader = New DigitalSingleChannelReader(DigitalEventTask.Stream)
  DigitalEventTask.Start()


Private Sub DigitalChange_Event(ByVal sender As Object, ByVal e As DigitalChangeDetectionEventArgs)
  Dim offset as Integer

  Try

            DigitalSamples = DigitalEventReader.ReadSingleSampleMultiLine()
 
            If DigitalSamples(0) = False Then

                Diag1PinchLED.Value = False
                Pinch1Button.Text = Pinch1Button.Text + 1

            ElseIf DigitalSamples(0) = True Then

                Diag1PinchLED.Value = True

            End If


        If DigitalSamples(1) = False Then

                Diag1ReleaseLED.Value = False
                Release1Button.Text = Release1Button.Text + 1

            ElseIf DigitalSamples(1) = True Then

                Diag1ReleaseLED.Value = True

            End If

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try


System Setup:
 USB-6229
 Debounce circuit - 10K pullup resistor with 1.0uF cap on the moment switch to ground
 VB .NET 2005 Measurement Studio 8.0.1

Thanks for your help!

Jon B.
 

0 Kudos
Message 1 of 3
(3,519 Views)

Hi Jon,


There is no property to clear the buffer, but this can be done by stopping and starting the task. The reason for the thread hanging is that your timeout is set to -1, which means no timeout. Since this is the case, the program will wait until a trigger is pressed to continue along with code. Change the timeout to an actual number, and the infinite hang will go away.

David L.
Systems Engineering
National Instruments
0 Kudos
Message 2 of 3
(3,511 Views)

I only program with LabVIEW and can't help with your specific syntax.  Here's the kind of approach I *think* you'll need:

1. At beginning of program, create a regular DI task that reads those bits once to determine your initial states.  Store this "most recent state" info in a static variable.  Clear the task.

2. Now you can create the change detection task for those bits.

3. Every time you detect any changes and read the state of the digital bits, refresh the "most recent state" variable.

4. Anytime you're simply curious about the state of those bits, but there aren't any changes available in the buffer, query your "most recent state" variable.  It holds the values at the most recent transition.

At least under LabVIEW, there are also ways to query old data directly out of the data acq buffer.  There are properties you set like "Offset=-1" and "RelativeTo=MostRecentSample".  This is probably possible for you to do with DAQmx calls, but I don't know the text syntax for it.

-Kevin P.

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 3 of 3
(3,507 Views)