Quantcast
Channel: Counter/Timer topics
Viewing all articles
Browse latest Browse all 1271

Edge counting using nicaiu.dll

$
0
0

I am trying to create an edge counter by directly calling the functions in nicaiu.dll from VB.NET. I am able to read the counter value OK but the counts always return back to the original value set when I created the channel. For example, I create the counter task using DAQmxCreateCICountEdgesChan and set the initial count to 100000. I then read the counter using DAQmxReadCounterScalarU32. The count changes when I turn the encoder wired to the counter channel but returns to 100000 after the encoder stops moving.

The wiring is correct because this works correctly using the Counter I/O Test Panel in NI-MAX. The count increases as I turn the encoder and never resets when the encoder stops moving.

Eventually I will need to get this working using DAQmxCreateCILinEncoderChan but I'm seeing the same behavior when I use that channel type. For now, I'm using DAQmxCreateCICountEdgesChan since it is more straight-forward and mimics the function in the NI-MAX Test Panel.

I have made this work using the DAQmx .NET API. However, there are errors when my application runs on a computer with a different version of NI-DAQmx than what was used when the application was compiled. Calling the DLL directly seems to eliminate the problems with different DAQmx versions.

 

Any idea what I am missing?

 

 

Public Class Form1

    Private Declare Function DAQmxCreateTask Lib "nicaiu.dll" (ByVal TaskName As String, ByRef NITaskHandle As IntPtr) As Integer
    Private Declare Function DAQmxCreateCICountEdgesChan Lib "nicaiu.dll" (ByVal TaskHandle As IntPtr, ByVal PhysicalChannel As String, ByVal NameToAssignToChannel As String, ByVal Edge As Integer, ByVal InitialCount As UInteger, ByVal CountDirection As Integer) As Integer
    Private Declare Function DAQmxReadCounterScalarU32 Lib "nicaiu.dll" (ByVal TaskHandle As IntPtr, ByVal Timeout As Double, ByRef ReadVal As UInteger, ByVal Reserved As IntPtr) As Integer

    Private Const DAQmx_Val_Rising = 10280
    Private Const DAQmx_Val_Falling = 10171

    Private Const DAQmx_Val_CountUp = 10128
    Private Const DAQmx_Val_CountDown = 10124
    Private Const DAQmx_Val_ExtControlled = 10326

    Private TaskHandle As IntPtr

    Private Const STARTCOUNT = 100000
    Private Const DEVICECHAN = "DEV1/CTR0"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim ErrVal As Integer

        ErrVal = DAQmxCreateTask("ENCODER", TaskHandle)
        Debug.WriteLineIf(ErrVal <> 0, "Error " & ErrVal.ToString)

        ErrVal = DAQmxCreateCICountEdgesChan(TaskHandle, DEVICECHAN, "ENCODER", DAQmx_Val_Rising, STARTCOUNT, DAQmx_Val_CountUp)
        Debug.WriteLineIf(ErrVal <> 0, "Error " & ErrVal.ToString)

    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        Dim ErrVal As Integer
        Dim InVal As UInteger

        ErrVal = DAQmxReadCounterScalarU32(TaskHandle, 0, InVal, Nothing)
        Debug.WriteLineIf(ErrVal <> 0, "Error " & ErrVal.ToString)

        Label1.Text = InVal.ToString

    End Sub

End Class

 


Viewing all articles
Browse latest Browse all 1271

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>