02-10-2014 03:53 PM
Thank you for your help. We are using a USB-6212 BNC and want to trigger our analog data collection with our laser the produces a 25 us TTL +5 V pulse. Each time the laser fires I want to collect analog data. I called tech support and they said I need to do Analog Input with Hardware Trigger. I have code to do analog input, no problem. I do not know how to do this triggering. Can you send me a sample of code to do this? I am using VB.NET 4.5 Visual Basic 2012. Thank you. Tony
02-11-2014 03:40 PM
Hi IdealVac,
A good place to start for everything Measurement Studio is:
http://www.ni.com/white-paper/14430/en/
As for examples, they are located in the start menu at
All Programs->National Instruments->NI-DAQ->Text-Based Code Support->DotNet4.5 Examples.
From there, navigate to Analog In->Measure Voltage, and there are a bunch of examples. I think I would start with "AcqVoltageSamples_IntClkDigRef"
Perhaps somebody else has worked with these more and could share their thoughts too.
02-12-2014 12:24 AM
Hi James,
Thank you for your help. I am a little behind the "8 ball" here and could use some code examples. All I need is a couple sub routines for hardware triggering. That is, I want my VB.Net 2012 software to stand still until the USB-6212 revcieves a pulse from by laser (TTL 5 V, 25 us wide pulses, at 10 Hz). I need the hardware to send a signal to my subroutine to collect data. My laser fires 10 times a second and my software needs to be trigger after each laser shot. I think that this will have to be done with event and callback handling but I am not an expert.
I looked at your suggestion
C:\Users\Public\Documents\National Instruments\NI-DAQ\Examples\DotNET4.5\Analog In\Measure Voltage\AcqVoltageSamples_IntClkDigRef
and it does work once to collect data when a TTL trigger arrives at PFI0. Do not know how to modify so that I get a callback each time I get a laser TTL pulse at PFI0?
Question, what simple pair of text based VB.Net 4.5 sub routines would hardware monitor for a trigger pulse on PFI0 and each time a pulse arrives it runs a second sub routine that flash a little LED like light on my mainform? If I had this I could finish my project.
Thank you very much for your help. Tony
******* Sample code from AcqVoltageSamples_IntClkDigRef ****************************
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, "", CType(-1, AITerminalConfiguration), rangeMin, rangeMax, AIVoltageUnits.Volts)
'Configure Timing Specs
myTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, samplesPerChan)
' Configure Reference Trigger
myTask.Triggers.ReferenceTrigger.ConfigureDigitalEdgeTrigger(referenceTriggerSourceTextBox.Text, referenceEdge, preTriggerNumeric.Value)
' Verify the Task
myTask.Control(TaskAction.Verify)
reader = New AnalogMultiChannelReader(myTask.Stream)
' Use SynchronizeCallbacks to specify that the object
' marshals callbacks across threads appropriately.
reader.SynchronizeCallbacks = True
reader.BeginReadWaveform(samplesPerChan, AddressOf myCallback, Nothing)
' Prepare the table for Data
InitializeDataTable(myTask.AIChannels, dataTable)
acquisitionDataGrid.DataSource = dataTable
Catch e1 As DaqException
MessageBox.Show(e1.Message)
myTask.Dispose()
startButton.Enabled = True
End Try
End Sub
Private Sub myCallback(ByVal ar As IAsyncResult)
Try
' Read the available data from the channels
Dim data() As AnalogWaveform(Of Double) = reader.EndReadWaveform(ar)
' Plot your data here
dataToDataTable(data, DataTable)
Catch e1 As DaqException
MessageBox.Show(e1.Message)
Finally
startButton.Enabled = True
myTask.Dispose()
End Try
End Sub
02-13-2014 10:58 AM
Got it to work! I am not sure this is the best way but it runs like I want it to. Basically, I want my data collection myCallback(ByVal ar AsIAsyncResult) analog input voltage measurement sub routine code to stay at rest until a digital input TTL trigger pulses from by laser (occurs at 10 Hz, +5 V, 25 us wide) is received at the hardware USB-6212 PFI1/P1.1 port. At that time, the runallthetime() calls my myCallback(ByVal ar AsIAsyncResult) sub routine and data collection is executed. The trick is to have the myCallback(ByVal ar AsIAsyncResult) sub loop back to the runallthetime() hardware trigger sub. See below. This makes a continuous running software that will collect 1 point of AI0 data every time a trigger is detected on PFI1/P1.1.
Private Sub startButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click startButton.Enabled =False runallthetime() EndSub
PrivateSub myCallback(ByVal ar AsIAsyncResult) Try 'The variable data is an Array (x,y), it will be get (1) data point for each Virtual Channel below in 'myTask.AIChannels.CreateVoltageChannel("Dev1/ai0, Dev1/ai1, Dev1/ai2, Dev1/ai3", data = reader.ReadMultiSample(1) TextBox3.Text =CStr(data(0, 0)) Finally myTask.Dispose() EndTry runallthetime() 'Hardware Trigger CallBack EndSub
PrivateSub runallthetime() Try 'Create a new task myTask = NewTask() 'Create a virtual channel - physicalChannelComboBox.Text myTask.AIChannels.CreateVoltageChannel("Dev1/ai0, Dev1/ai1, Dev1/ai2, Dev1/ai3", "", AITerminalConfiguration.Rse, Convert.ToDouble("-10"), Convert.ToDouble("10"), AIVoltageUnits.Volts)
'Configure Timing Specs 'myTask.Timing.ConfigureSampleClock("", 100, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, ' samplesPerChan) myTask.Timing.ConfigureSampleClock("", 100, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, 10)
'Simple Start Tigger Function - Has Two Parameters (Channel = /Dev1/PFI0 - PFI7, 'DigitalEdgeReferenceTriggerEdge.Rising or Falling myTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger("/Dev1/PFI1", DigitalEdgeStartTriggerEdge.Rising)
' Verify the Task myTask.Control(TaskAction.Verify) reader = NewAnalogMultiChannelReader(myTask.Stream)
' Use SynchronizeCallbacks to specify that the object ' marshals callbacks across threads appropriately. reader.SynchronizeCallbacks =True reader.BeginReadWaveform(1, AddressOf myCallback, Nothing)
Catch e1 AsDaqException startButton.Enabled =True EndTry EndSub
02-13-2014 11:03 AM
Private Sub startButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
startButton.Enabled = False
runallthetime()
End Sub
Private Sub myCallback(ByVal ar As IAsyncResult)
Try
'The variable data is an Array (x,y), it will be get (1) data point for each Virtual Channel below in myTask.AIChannels.CreateVoltageChannel("Dev1/ai0, Dev1/ai1, Dev1/ai2, Dev1/ai3",
data = reader.ReadMultiSample(1)
TextBox3.Text = CStr(data(0, 0))
Finally
myTask.Dispose()
End Try
runallthetime()
End Sub
Private Sub runallthetime()
Try
'Create a new task
myTask = New Task()
'Create a virtual channel - physicalChannelComboBox.Text
myTask.AIChannels.CreateVoltageChannel("Dev1/ai0, Dev1/ai1, Dev1/ai2, Dev1/ai3", "", AITerminalConfiguration.Rse, Convert.ToDouble("-10"), Convert.ToDouble("10"), AIVoltageUnits.Volts)
'Configure Timing Specs
'myTask.Timing.ConfigureSampleClock("", 100, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, samplesPerChan)
myTask.Timing.ConfigureSampleClock("", 100, SampleClockActiveEdge.Rising, SampleQuantityMode.FiniteSamples, 10)
'Simple Start Tigger Function - Has Two Parameters (Channel = /Dev1/PFI0 - PFI7,DigitalEdgeReferenceTriggerEdge.Rising or Falling
myTask.Triggers.StartTrigger.ConfigureDigitalEdgeTrigger("/Dev1/PFI1", DigitalEdgeStartTriggerEdge.Rising)
' Verify the Task
myTask.Control(TaskAction.Verify)
reader = New AnalogMultiChannelReader(myTask.Stream)
' Use SynchronizeCallbacks to specify that the object
' marshals callbacks across threads appropriately.
reader.SynchronizeCallbacks = True
reader.BeginReadWaveform(1, AddressOf myCallback, Nothing)
Catch e1 As DaqException
startButton.Enabled = True
End Try
End Sub
02-13-2014 11:17 AM
Sorry, the two above codes are the same. I first pasted the code in the text editor section. The one directly above is correct, i used the insert code function :-). Thanks.