Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I read multiple channels with .NET

Hi everyone,

 

I'd like to know how can I read multiple channels at the same time in VB2010. I'm using an example of NI:

Imports NationalInstruments.DAQmx

Public Class MainForm
    Inherits System.Windows.Forms.Form
---------------------------

    Private myTask As Task  'A new task is created when the start button is pressed
    Private reader As AnalogMultiChannelReader

    Private dataColumn As DataColumn()             'Channels of Data
    Private dataTable As DataTable = New DataTable 'Table to Display Data


    Private Sub startButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        startButton.Enabled = False

        Try
            ' Create a new task
            myTask = New Task()

            ' Initialize local variables
            Dim sampleRate As Double = Convert.ToDouble(rateNumeric.Value)
            Dim rangeMin As Double = Convert.ToDouble(minimumValueNumeric.Value)
            Dim rangeMax As Double = Convert.ToDouble(maximumValueNumeric.Value)
            Dim samplesPerChan As Int32 = Convert.ToInt32(samplesPerChanNumeric.Value)

            ' Create a channel
            myTask.AIChannels.CreateVoltageChannel(physicalChannelComboBox.Text, "", AITerminalConfiguration.Rse, rangeMin, rangeMax, AIVoltageUnits.Volts)

            ' Configure timing specs
            myTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, samplesPerChan)

            ' Verify the task
            myTask.Control(TaskAction.Verify)
            reader = New AnalogMultiChannelReader(myTask.Stream)

            ' Prepare the table for data
            InitializeDataTable(myTask.AIChannels, dataTable)
            acquisitionDataGrid.DataSource = dataTable

            ' Read the data
            Dim data() As AnalogWaveform(Of Double) = reader.ReadWaveform(samplesPerChan)

            dataToDataTable(data, dataTable)
        Catch ex As DaqException
            MessageBox.Show(ex.Message)
        Finally
            myTask.Dispose()
            startButton.Enabled = True
        End Try
    End Sub

    Private Sub dataToDataTable(ByVal sourceArray As AnalogWaveform(Of Double)(), ByRef dataTable As DataTable)
        ' Iterate over channels
        Dim currentLineIndex As Integer = 0
        For Each waveform As AnalogWaveform(Of Double) In sourceArray
            Dim dataCount As Integer = 0
            If waveform.Samples.Count < 10 Then
                dataCount = waveform.Samples.Count
            Else
                dataCount = 10
            End If
            For sample As Integer = 0 To (dataCount - 1)
                dataTable.Rows(sample)(currentLineIndex) = waveform.Samples(sample).Value
            Next
            currentLineIndex += 1
        Next
    End Sub
    
    Public Sub InitializeDataTable(ByVal channelCollection As AIChannelCollection, ByRef data As dataTable)
        Dim numOfChannels As Int16 = channelCollection.Count
        data.Rows.Clear()
        data.Columns.Clear()
        dataColumn = New DataColumn(numOfChannels) {}
        Dim numOfRows As Int16 = 10
        Dim currentChannelIndex As Int16 = 0
        Dim currentDataIndex As Int16 = 0

        For currentChannelIndex = 0 To (numOfChannels - 1)
            dataColumn(currentChannelIndex) = New DataColumn
            dataColumn(currentChannelIndex).DataType = System.Type.GetType("System.Double")
            dataColumn(currentChannelIndex).ColumnName = channelCollection(currentChannelIndex).PhysicalName
        Next

        data.Columns.AddRange(dataColumn)

        For currentDataIndex = 0 To (numOfRows - 1)
            Dim rowArr As Object() = New Object(numOfChannels - 1) {}
            data.Rows.Add(rowArr)
        Next
    End Sub
End Class

0 Kudos
Message 1 of 1
(5,188 Views)