‎03-23-2009 03:07 PM
Hi there, i'm trying to write a program in visual basic .net that writes the tension from 4 channel in a .txt file and since i'm a newbie i'm having a lot of problems with it 🙂
I tried to read the documentation and tried the nidaqmx examples but i wasn't able to finish it as i wanted to.
I'll post my code here
Imports System
Imports System.IO
Imports NationalInstruments.DAQmx
Public Class Form1
Private myTask1 As Task 'A new Task is created when the start button is pressed
Private analogInReader As AnalogMultiChannelReader
Private savedData As ArrayList
Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As Long) As Short
Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef X As Long) As Short
Sub startbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startbutton.Click
Dim minimumValueNumeric As Int32 = -10
Dim maximumValueNumeric As Int32 = 10
myTask1 = New Task()
myTask1.AIChannels.CreateVoltageChannel("dev1/ai0:3", "Voltage", _
CType(-1, AITerminalConfiguration), Convert.ToDouble(minimumValueNumeric), _
Convert.ToDouble(maximumValueNumeric), AIVoltageUnits.Volts)
myTask1.Timing.ConfigureSampleClock("", 1000, SampleClockActiveEdge.Rising, _
SampleQuantityMode.ContinuousSamples)
myTask1.Control(TaskAction.Verify)
analogInReader = New AnalogMultiChannelReader(myTask1.Stream)
Dim data() As Double = analogInReader.ReadSingleSample
Dim i As Int32 = 0
Dim countercicli As Int32 = 0
'''''''''' For f = 0 To NumericUpDown1.Value - 1
Dim Ctr1, Ctr2, Freq As Long
If data(0) >= 1 Then
i = i + 1
Using w As StreamWriter = File.AppendText("c:\TestFile.txt")
' Add some text to the file.
w.Write(i & " " & data(0).ToString("n4") & " " & data(1).ToString("n4") & " " & data(2).ToString("n4") & " " _
& data(3).ToString("n4") & " ")
countercicli = countercicli + 1
If countercicli = 10 Then
Dim hail As Double
QueryPerformanceFrequency(Freq)
QueryPerformanceCounter(Ctr2)
hail = ((Ctr2 - Ctr1) / (Freq / 1000)).ToString("n4") 'milliseconds
If hail >= 1000 Then
hail = 10
End If
w.WriteLine("timer: {0}", hail)
countercicli = 0
Ctr1 = Ctr2
End If
If countercicli <> 10 And countercicli <> 0 Then
w.WriteLine("timer: {0}", 0)
End If
End Using
End If
''''''''' Next
End Sub
End Class
Basically i'm trying to get a continuous write of the tension of the 4 channels (only if the 3rd channel voltage is higher then 1 volt, which is my trigger) in the TestFile.txt
Unfortunately for now i'm able to write only the first line and i have no idea how to make it continuous (if it's possible)
Any help will be appreciated, thanks in advance
‎03-26-2009 04:09 AM
Hello Newbie,
I can point you to a link in which is said where to find DAQmx VB.NET examples:
Text Based NI-DAQmx Data Acquisition Examples
http://zone.ni.com/devzone/cda/tut/p/id/6999#.NETAI
In the Analog Input section there is an example called "Continuously Acquire Voltage Samples - Internal Clock - Save to File" that is installed when you install the NI DAQmx library. You should be able to find this example at the following location:
C:\Documents and Settings\All Users\Shared Documents\National Instruments\NI-DAQ\Examples\
I hope this can help, best regards.