Measurement Studio for VB6

cancel
Showing results for 
Search instead for 
Did you mean: 

Unexpected flag change

Solved!
Go to solution

Hi,

 

I have a loop which runs until one of the tasks finishes. I try to make the chkSaveAOV and the chkSaveV equivalent and would use either of them.

However, I found when I have only the chkSaveV box checked, the AOVInFlag changes to True at the ***** commend, which cause a problem for the loop. This happens even though I'm not doing anything with any of the "Save AOV" function.

What is stranger, this doesn't happen the other way around. I mean, when I have only the chkSaveAOV box checked, the VInFlag wouldn't change during the run.

Could you please look at my code and see what's wrong with it? Thanks a lot!

 

    If chkSaveAOV Then DAQmxErrChk DAQmxStartTask(AOVInHandle)
    If chkSaveV Then DAQmxErrChk DAQmxStartTask(VInHandle)
    Label13.Caption = "Total bins: "
    Do
        DAQmxErrChk DAQmxIsTaskDone(PhotBinHandle, BinFlag)
        BinFlag = Not BinFlag
        If chkSaveAOV Then
            DAQmxErrChk DAQmxIsTaskDone(AOVInHandle, AOVInFlag)
            AOVInFlag = Not AOVInFlag
        Else
            AOVInFlag = False
        End If
        If chkSaveV Then
*****     DAQmxErrChk DAQmxIsTaskDone(VInHandle, VInFlag)
            VInFlag = Not VInFlag
        Else
            VInFlag = False
        End If
        DoEvents
    Loop While (BinFlag Or AOVInFlag Or VInFlag)

 

 

Best,

Yan

0 Kudos
Message 1 of 11
(10,596 Views)

Hi Yan,

 

I'm not seeing anything wrong with the code you posted. Is it possible that there may be a process on another thread running which may be changing this flag? Are you doing a continuous generation/acquisition task? Are you using any callback function?

S_Hong
National Instruments
Applications Engineer
0 Kudos
Message 2 of 11
(10,584 Views)

Thank you for replying me!

 

Yes. I am doing a continuous generation task. But I still don't understand why an unrelated flag would change due the running of this task. The entire code might be too bulky to post here. So I'm listing here all the commands related to this AOVInFlag. Could you please tell me what kind of thing might be affecting the value of AOVInFlag?

 

Thanks,

Yan

 

... ...

    Private AOVInFlag As Boolean

... ...

    Do
        DAQmxErrChk DAQmxIsTaskDone(PhotBinHandle, BinFlag)
        BinFlag = Not BinFlag
        If chkSaveV Then
            DAQmxErrChk DAQmxIsTaskDone(VInHandle, VInFlag)
            VInFlag = Not VInFlag
        Else
            VInFlag = False
        End If
        If chkSaveAOV Then
            DAQmxErrChk DAQmxIsTaskDone(AOVInHandle, AOVInFlag)
            AOVInFlag = Not AOVInFlag
        Else
            AOVInFlag = False
        End If
        DoEvents
    Loop While (BinFlag Or AOVInFlag Or VInFlag)

... ...

    If chkSaveAOV Then
        ReDim AOVInDat(1 To 2 * (SampsPerSec * AcqTime) + 1) As Double
        DAQmxReadAnalogF64 AOVInHandle, (SampsPerSec * AcqTime), 0#, DAQmx_Val_GroupByScanNumber, _
            AOVInDat(1), 2 * (SampsPerSec * AcqTime) + 1, VsampsPerChanRead, ByVal 0&
        If AOVInFlag Then
            DAQmxErrChk DAQmxStopTask(AOVInHandle)
            AOVInFlag = False
        End If
        DAQmxErrChk DAQmxClearTask(AOVInHandle)
    End If

0 Kudos
Message 3 of 11
(10,579 Views)

Hi Yan,

 

I'm still not sure as to why the AOVInFlag is getting asserted. Could we try striping down the code to its bare minimum to see if this problem still occurs. Hopefully, this will help us narrow down the problem. Could you then post this code online so we can have another look at this?

S_Hong
National Instruments
Applications Engineer
0 Kudos
Message 4 of 11
(10,557 Views)

OK. Here's the code. Thank you for looking at this messy code!

 


Option Explicit
    Const BuffSize = 20000000        'number of photons to store in buffer
    Const SampsPerSec = 40000       'Number of voltage reads/second
    Const DARate = 1600000          'Max number of D -> A conversions/channel/sec
    Private ReadPhotons As Variant
    Private VOut() As Double
    Private VCarrier() As Double
    Private Vtemp() As Double
    Private VMod() As Double
    Private VXoff() As Double
    Private VYoff() As Double
    Private VOutHandle As Long
    Private VOutFlag As Boolean
    Private SampFreq As Double
    Private SampsPerCarCyc As Double
    Private SampsPerModCyc As Double
    Private SampsPerXoffCyc As Double
   
    Private sampsPerChanWritten As Long
    Private sampsPerChanRead As Long
    Private VsampsPerChanRead As Long
    Private i As Double
    Private j As Integer
    Private k As Integer
    Private ErrVal As Long

    Private FocusFlag As Boolean
    Private AcqFlag As Boolean
    Private BinFlag As Boolean

    Dim CtrDat(1 To BuffSize) As Double
    Private PhotCtrHandle As Long
    Private PhotBinHandle As Long
    Private VInHandle As Long
    Private AOVInHandle As Long
    Private VInFlag As Boolean
    Private AOVInFlag As Boolean
    Private VInDat() As Double
    Private AOVInDat() As Double
    Private NumBins As Long
    Dim BinDat() As Double
    Private CPS As Double  'counts per second
   
    Private VoutCycHandle As Long
    Private VCycs As Long
    Dim VoutTimes() As Double
    Private VcycsRead As Long

 

Private Sub Form_Load()
    DAQmxErrChk DAQmxResetDevice("Dev1")
    i = 0
    VOutFlag = False
    FocusFlag = False
    AcqFlag = False
    SampsPerModCyc = 200       'For analog voltage out
    DAQmxErrChk DAQmxCreateTask("", VOutHandle)
    DAQmxErrChk DAQmxCreateAOVoltageChan(VOutHandle, "Dev1/ao0", "", -10, 10, DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxWriteAnalogScalarF64(VOutHandle, True, 10#, XOff.Value, ByVal 0&)
    DAQmxErrChk DAQmxStopTask(VOutHandle)
    DAQmxErrChk DAQmxClearTask(VOutHandle)
   
    DAQmxErrChk DAQmxCreateTask("", VOutHandle)
    DAQmxErrChk DAQmxCreateAOVoltageChan(VOutHandle, "Dev1/ao1", "", -10, 10, DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxWriteAnalogScalarF64(VOutHandle, True, 10#, YOff.Value, ByVal 0&)
    DAQmxErrChk DAQmxStopTask(VOutHandle)
    DAQmxErrChk DAQmxClearTask(VOutHandle)
End Sub

 

Private Sub cmdFocus_Click()
cmdAcquire.Enabled = True
cmdStop.Enabled = True
cmdFocus.Enabled = False
FocusFlag = True

DAQmxErrChk DAQmxCreateTask("", PhotBinHandle)
DAQmxErrChk DAQmxCreateCIFreqChan(PhotBinHandle, "Dev1/ctr1", "", 1, _
    2000000, DAQmx_Val_FrequencyUnits3_Hz, DAQmx_Val_Edge1_Falling, _
    DAQmx_Val_CounterFrequencyMethod_HighFreq2Ctr, FocBinSize.Value / 1000, 4, "")
DAQmxErrChk DAQmxCfgImplicitTiming(PhotBinHandle, DAQmx_Val_AcquisitionType_FiniteSamps, _
    BuffSize)
DAQmxErrChk DAQmxSetCIFreqTerm(PhotBinHandle, "Dev1/Ctr1", "/Dev1/PFI0")
DAQmxSetCIDupCountPrevent PhotBinHandle, "Dev1/Ctr1", True
   
DAQmxErrChk DAQmxStartTask(PhotBinHandle)
Do
    DAQmxReadCounterScalarF64 PhotBinHandle, -1#, CPS, ByVal 0&
    CWGraph1.ChartY CPS
    DoEvents
Loop While FocusFlag

End Sub

 

0 Kudos
Message 5 of 11
(10,549 Views)

Private Sub cmdAcquire_Click()
cmdAcquire.Enabled = False
cmdFocus.Enabled = False
cmdStop.Enabled = True
cmdVStart.Enabled = False
cmdVStop.Enabled = False
chkSaveV.Enabled = False
chkSaveAOV.Enabled = False

AcqFlag = True

If FocusFlag Then StopFocus
Label6.Caption = "Acquiring"

If chkSaveV Then
    DAQmxErrChk DAQmxCreateTask("", VInHandle)
    DAQmxErrChk DAQmxCreateAIVoltageChan(VInHandle, "Dev1/ai0:1", "", _
    DAQmx_Val_InputTermCfg_Diff, -10, 10, DAQmx_Val_VoltageUnits1_Volts, "")
    DAQmxErrChk DAQmxCfgSampClkTiming(VInHandle, "OnboardClock", SampsPerSec, DAQmx_Val_Rising, _
        DAQmx_Val_AcquisitionType_FiniteSamps, CLng(SampsPerSec * AcqTime))
End If

If chkSaveAOV Then
    DAQmxErrChk DAQmxCreateTask("", AOVInHandle)
    DAQmxErrChk DAQmxCreateAIVoltageChan(AOVInHandle, "Dev1/ai2", "", _
    DAQmx_Val_InputTermCfg_Diff, -10, 10, DAQmx_Val_VoltageUnits1_Volts, "")
    DAQmxErrChk DAQmxCfgSampClkTiming(AOVInHandle, "OnboardClock", SampsPerSec, DAQmx_Val_Rising, _
        DAQmx_Val_AcquisitionType_FiniteSamps, CLng(SampsPerSec * AcqTime))
End If

If optTimeTagged Then
    DAQmxErrChk DAQmxCreateTask("", PhotCtrHandle)
    DAQmxErrChk DAQmxCreateCICountEdgesChan(PhotCtrHandle, "Dev1/Ctr0", "", _
        DAQmx_Val_Edge1_Rising, 0, DAQmx_Val_CountDirection1_CountUp)
    DAQmxErrChk DAQmxCfgSampClkTiming(PhotCtrHandle, "/Dev1/PFI0", 5000000, _
        DAQmx_Val_Rising, DAQmx_Val_AcquisitionType_FiniteSamps, BuffSize)
    DAQmxErrChk DAQmxSetCICountEdgesTerm(PhotCtrHandle, "Dev1/Ctr0", "/Dev1/80MHzTimebase")
    DAQmxSetCIDupCountPrevent PhotCtrHandle, "Dev1/Ctr0", True
    DAQmxErrChk DAQmxStartTask(PhotCtrHandle)
    If VOutFlag Then
        VCycs = (AcqTime.Value * Freq.Value) + 1
        DAQmxErrChk DAQmxCreateTask("", VoutCycHandle)
        DAQmxErrChk DAQmxCreateCICountEdgesChan(VoutCycHandle, "Dev1/Ctr1", "", _
            DAQmx_Val_Edge1_Rising, 0, DAQmx_Val_CountDirection1_CountUp)
        DAQmxErrChk DAQmxCfgSampClkTiming(VoutCycHandle, "/Dev1/PFI1", 5000000, _
            DAQmx_Val_Rising, DAQmx_Val_AcquisitionType_FiniteSamps, VCycs)
        DAQmxErrChk DAQmxSetCICountEdgesTerm(VoutCycHandle, "Dev1/Ctr1", "/Dev1/PFI3")
        DAQmxErrChk DAQmxStartTask(VoutCycHandle)
       
        Label17.Caption = "Voltage cycles: "
    End If

    Timer1.Interval = AcqTime.Value * 1000
    Timer1.Enabled = True

    Label13.Caption = "Total photons: "
Else
    NumBins = AcqTime.Value * 1000 / AcqBinSize.Value
    DAQmxErrChk DAQmxCreateTask("", PhotBinHandle)
'*******************************************
' The functions DAQmxSetCICountEdgesTerm and DAQmxSetCIFreqTerm
' do not appear anywhere in the documentation.  But they can be found in
' C:\Program Files\National Instruments\NI-DAQ\DAQmx ANSI C Dev\include\NIDAQmx.h
' and need to be called to set the proper connections
'*******************************************
    DAQmxErrChk DAQmxCreateCIFreqChan(PhotBinHandle, "Dev1/Ctr1", "", 1, _
        2000000, DAQmx_Val_FrequencyUnits3_Hz, DAQmx_Val_Edge1_Falling, _
        DAQmx_Val_CounterFrequencyMethod_HighFreq2Ctr, AcqBinSize.Value / 1000, 4, "")
    DAQmxErrChk DAQmxCfgImplicitTiming(PhotBinHandle, DAQmx_Val_AcquisitionType_FiniteSamps, _
        NumBins + 1)
    DAQmxErrChk DAQmxSetCIFreqTerm(PhotBinHandle, "Dev1/Ctr1", "/Dev1/PFI0")
   
    DAQmxSetCIDupCountPrevent PhotBinHandle, "Dev1/Ctr1", True
   
    DAQmxErrChk DAQmxStartTask(PhotBinHandle)
    If chkSaveAOV Then DAQmxErrChk DAQmxStartTask(AOVInHandle)
    If chkSaveV Then DAQmxErrChk DAQmxStartTask(VInHandle)
    Label13.Caption = "Total bins: "
    Do
        DAQmxErrChk DAQmxIsTaskDone(PhotBinHandle, BinFlag)
        BinFlag = Not BinFlag
        If chkSaveV Then
            DAQmxErrChk DAQmxIsTaskDone(VInHandle, VInFlag)
            VInFlag = Not VInFlag
        Else
            VInFlag = False
        End If
        If chkSaveAOV Then
            DAQmxErrChk DAQmxIsTaskDone(AOVInHandle, AOVInFlag)
            AOVInFlag = Not AOVInFlag
        Else
            AOVInFlag = False
        End If
        DoEvents
    Loop While (BinFlag Or AOVInFlag Or VInFlag)
    cmdStop_Click
End If
End Sub

 

Private Sub Timer1_Timer()
Timer1.Enabled = False
cmdStop_Click
End Sub

 

Private Sub cmdStop_Click()
chkSaveV.Enabled = True
chkSaveAOV.Enabled = True
cmdStop.Enabled = False

If AcqFlag Then
    StopAcq
End If
If FocusFlag Then StopFocus

cmdFocus.Enabled = True
cmdAcquire.Enabled = True
If VOutFlag Then
    cmdVStop.Enabled = True
Else
    cmdVStart.Enabled = True
End If
End Sub

 

Private Sub StopFocus()
FocusFlag = False
DAQmxErrChk DAQmxStopTask(PhotBinHandle)
DAQmxErrChk DAQmxClearTask(PhotBinHandle)
End Sub

0 Kudos
Message 6 of 11
(10,548 Views)

Private Sub StopAcq()
AcqFlag = False
Label6.Caption = ""
If optTimeTagged Then
    Timer1.Enabled = False
    DAQmxReadCounterF64 PhotCtrHandle, BuffSize, 0#, CtrDat(1), BuffSize, _
        sampsPerChanRead, ByVal 0&
    DAQmxErrChk DAQmxStopTask(PhotCtrHandle)
    DAQmxErrChk DAQmxClearTask(PhotCtrHandle)
    Label13.Caption = "Total Photons: " & sampsPerChanRead
   
    If VOutFlag Then
        ReDim VoutTimes(1 To VCycs) As Double
        DAQmxReadCounterF64 VoutCycHandle, VCycs, 0#, VoutTimes(1), VCycs, VcycsRead, ByVal 0&
        DAQmxErrChk DAQmxStopTask(VoutCycHandle)
        DAQmxErrChk DAQmxClearTask(VoutCycHandle)
        Label17.Caption = "Voltage cycles: " & VcycsRead
    End If
Else
    ReDim BinDat(1 To NumBins + 1) As Double
    DAQmxReadCounterF64 PhotBinHandle, NumBins + 1, 0#, BinDat(1), NumBins + 1, _
        sampsPerChanRead, ByVal 0&
    If BinFlag Then
        DAQmxErrChk DAQmxStopTask(PhotBinHandle)
        BinFlag = False
    End If
    DAQmxErrChk DAQmxClearTask(PhotBinHandle)
    Label13.Caption = "Bins Collected: " & sampsPerChanRead
   
    If chkSaveAOV Then
        ReDim AOVInDat(1 To 2 * (SampsPerSec * AcqTime) + 1) As Double
        DAQmxReadAnalogF64 AOVInHandle, (SampsPerSec * AcqTime), 0#, DAQmx_Val_GroupByScanNumber, _
            AOVInDat(1), 2 * (SampsPerSec * AcqTime) + 1, VsampsPerChanRead, ByVal 0&
        If AOVInFlag Then
            DAQmxErrChk DAQmxStopTask(AOVInHandle)
            AOVInFlag = False
        End If
        DAQmxErrChk DAQmxClearTask(AOVInHandle)
    End If
   
    If chkSaveV Then
        ReDim VInDat(1 To 2 * (SampsPerSec * AcqTime) + 1) As Double
        DAQmxReadAnalogF64 VInHandle, (SampsPerSec * AcqTime), 0#, DAQmx_Val_GroupByScanNumber, _
            VInDat(1), 2 * (SampsPerSec * AcqTime) + 1, VsampsPerChanRead, ByVal 0&
        If VInFlag Then
            DAQmxErrChk DAQmxStopTask(VInHandle)
            VInFlag = False
        End If
        DAQmxErrChk DAQmxClearTask(VInHandle)
    End If
End If

frmSave.Show
End Sub

 

Public Sub cmdSave()
Label7.Caption = "saving"
If optTimeTagged Then
    Open Path.Text & "\" & Filename.Text & "_tt_header.txt" For Output As #1
Else
    Open Path.Text & "\" & Filename.Text & "_bin_header.txt" For Output As #1
End If
Print #1, Date & ", " & Format(Time, "hh:mm:ss AMPM")
Print #1, "Yan *****"
Print #1, "******** University"

If optTimeTagged Then
    Print #1, "Time-tagging of photons with 100 ns resolution."
    Print #1, "40 kHz AOM rotation --> 250 counts/rotation, phase-locked to time-tagging clock."
    Print #1, sampsPerChanRead & " total photons detected."
    Print #1, "Duration of experiment: " & (CtrDat(sampsPerChanRead) - CtrDat(1)) / 10000000# & " s."
    Print #1, "Intervals between successive photons given in units of 100 ns."
Else
    Print #1, "Binning of photons in bins of " & AcqBinSize.Value & " ms."
    Print #1, sampsPerChanRead & " bins acquired over " & AcqTime.Value & " s."
End If
Print #1,
Print #1, "Carrier: "
If OptSine Then
    Print #1, "Sine Wave."
ElseIf OptSquare Then
    Print #1, "Square Wave."
Else
    Print #1, "Triangle Wave."
End If
Print #1, Freq.Value & " Hz"
Print #1, Phase.Value & " Degrees"
Print #1, "Vx: " & XAmp.Value & " V"
Print #1, "Vy: " & YAmp.Value & " V"
Print #1, "AM Modulation: "
If OptAMSine Then
    Print #1, "Sine Wave."
ElseIf OptAMSquare Then
    Print #1, "Square Wave."
Else
    Print #1, "Triangle Wave."
End If
Print #1, "Frequency: " & Freq.Value / AMPer.Value & " Hz"
Print #1, "% modulation: " & AMAmp
Print #1, "Offset:"
Print #1, "Vx: " & XOff.Value & " V"
Print #1, "Vx Modulation Amplitude:" & XoffAmp.Value & "V"
Print #1, "Vy: " & YOff.Value & " V"
Print #1, "Vy Modulation Amplitude:" & YoffAmp.Value & "V"
Print #1, "Offset voltage Modulation Frequency:" & Freq.Value / XoffPer.Value & "Hz"
Print #1, "Lockin: "
Print #1, "Amplitude: " & LockinAmp.Value & " V"
Print #1, "Delay: " & LockinPhase.Value & " data points"
If chkSaveV Then
    Print #1,
    Print #1, "Voltage recorded at 40,000 Hz"
    Print #1, "Voltage samples read: " & VsampsPerChanRead
End If
If chkSaveAOV Then
    Print #1,
    Print #1, "AOM driving voltage recorded at 40,000 Hz"
    Print #1, "AOM driving voltage read: " & VsampsPerChanRead
End If
Close #1

If optTimeTagged Then
    Open Path.Text & "\" & Filename.Text & "_tt.txt" For Output As #2
    For i = 2 To sampsPerChanRead
        Write #2, CtrDat(i) - CtrDat(i - 1)
    Next i
Else
    Open Path.Text & "\" & Filename.Text & "_bin.txt" For Output As #2
    For i = 2 To sampsPerChanRead
        Write #2, BinDat(i) * AcqBinSize.Value / 1000
    Next i
End If
Close #2
If chkSaveV Then
    Open Path.Text & "\" & Filename.Text & "_Vin.txt" For Output As #4
    For i = 1 To VsampsPerChanRead
        Print #4, Format(VInDat((2 * i) - 1), "##.000") & ", " & Format(VInDat(2 * i), "##.000")
    Next i
    Close #4
End If
If chkSaveAOV Then
    Open Path.Text & "\" & Filename.Text & "_AOVin.txt" For Output As #5
    For i = 1 To VsampsPerChanRead
        Print #5, Format(AOVInDat(i), "##.000")
    Next i
    Close #5
End If
Label7.Caption = ""
End Sub

0 Kudos
Message 7 of 11
(10,547 Views)

Private Sub cntMax_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If cntMax.Value > cntMin.Value Then
    CWGraph1.Axes(2).Maximum = cntMax.Value
Else
    cntMin.Value = cntMax.Value - 1
    CWGraph1.Axes(2).SetMinMax cntMin.Value, cntMax.Value
End If
End Sub

 

Private Sub cntmin_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If cntMin.Value < cntMax.Value Then
    CWGraph1.Axes(2).Minimum = cntMin.Value
Else
    cntMax.Value = cntMin.Value + 1
    CWGraph1.Axes(2).SetMinMax cntMin.Value, cntMax.Value
End If
End Sub

 

Private Sub cmdvstart_Click()

    Dim errorCode As Long
    Dim errorString As String
    Dim numchannels As Long
   
    On Error GoTo ErrorHandler
    'On Error Resume Next

    cmdVStart.Enabled = False
    cmdVStop.Enabled = True
   
    '  Create the DAQmx task.
    DAQmxErrChk DAQmxCreateTask("", VOutHandle)
       
    '  Add an analog output channel to the task
    DAQmxErrChk DAQmxCreateAOVoltageChan(VOutHandle, "Dev1/ao0:3", "", _
        -10#, 10#, DAQmx_Val_VoltageUnits2_Volts, "")
       
    'set to run only using onboard fifo (Essential for freqs > 28 kHz)
    DAQmxErrChk DAQmxSetAOUseOnlyOnBrdMem(VOutHandle, "Dev1/ao0:3", True)
   
    UpdateWaveform
Exit Sub
           
ErrorHandler:
    If VOutFlag Then
        DAQmxStopTask VOutHandle
        DAQmxClearTask VOutHandle
        VOutFlag = False
    End If

    cmdVStart.Enabled = True
    cmdVStop.Enabled = False
   
    MsgBox "Error: " & Err.Number & " " & Err.Description, , "Error"
End Sub

 

Private Sub SetSampsPerCycle()
If Freq.Value > 8000 Then
    SampsPerCarCyc = Fix(0.8 * DARate / Freq.Value)
Else
    SampsPerCarCyc = 160
End If
SampsPerModCyc = SampsPerCarCyc * AMPer
SampsPerXoffCyc = SampsPerCarCyc * XoffPer
LockinPhase.Maximum = SampsPerModCyc
End Sub

 

Private Sub cmdVStop_Click()
StopVOut
End Sub

 

Private Sub UpdateWaveform()

SetSampsPerCycle
ReDim VOut(0 To 3, 0 To SampsPerXoffCyc - 1)
ReDim VCarrier(0 To 2, 0 To SampsPerCarCyc - 1)
ReDim VMod(0 To SampsPerModCyc - 1)
ReDim VXoff(0 To SampsPerXoffCyc - 1)
ReDim VYoff(0 To SampsPerXoffCyc - 1)
ReDim Vtemp(0 To SampsPerModCyc - 1)

SampFreq = SampsPerCarCyc * Freq.Value

'  Gather data points for a sine, square or triangle wave base.
If OptSine Then
    MakeSine
ElseIf OptSquare Then
    MakeSquare
ElseIf OptTri Then
    MakeTri
End If
'  Make a 90 degree phase-shifted copy of the carrier
For i = 0 To (SampsPerCarCyc - 1)
    j = CInt((i + SampsPerCarCyc / 4) Mod SampsPerCarCyc)
    VCarrier(1, i) = VCarrier(0, j)
Next i

'  Make the modulation signal
If OptAMSine Then
    MakeAMSine
ElseIf OptAMSquare Then
    MakeAMSquare
ElseIf OptAMTri Then
    MakeAMTri
End If

'  Make the X offset modulation signal
MakeXoffSquare
MakeYoffSquare

'put the signals together, with appropriate phase shifts
For i = 0 To SampsPerXoffCyc - 1
    'Here are the AOM drive signals
    j = CInt(i + Phase * SampsPerCarCyc / 360)
    VOut(0, i) = XAmp * VCarrier(0, i Mod SampsPerCarCyc) * VMod(i Mod SampsPerModCyc) + VXoff(i)
    VOut(1, i) = YAmp * VCarrier(2, i Mod SampsPerCarCyc) * VMod(j Mod SampsPerModCyc) + VYoff(j Mod SampsPerXoffCyc)
   
    'Here are the lockin signals
    k = i + SampsPerModCyc - LockinPhase
    VOut(2, i) = LockinAmp * VCarrier(0, k Mod SampsPerCarCyc) * VMod(k Mod SampsPerModCyc)
    VOut(3, i) = LockinAmp * VCarrier(1, k Mod SampsPerCarCyc) * VMod(k Mod SampsPerModCyc)
Next
If VOutFlag Then DAQmxErrChk DAQmxStopTask(VOutHandle)
'  Configure task for continuous waveform generation.
DAQmxErrChk DAQmxCfgSampClkTiming(VOutHandle, "", SampFreq, _
    DAQmx_Val_Falling, DAQmx_Val_AcquisitionType_ContSamps, CLng(SampsPerXoffCyc))

'  Write to the desired channel(s).
DAQmxErrChk DAQmxWriteAnalogF64(VOutHandle, SampsPerXoffCyc, True, 10#, _
    DAQmx_Val_GroupByScanNumber, VOut(0, 0), sampsPerChanWritten, ByVal 0&)
Label5.Caption = "Samples/channel: " & sampsPerChanWritten

'DAQmxErrChk DAQmxStartTask(VOutHandle)
VOutFlag = True
End Sub


Private Sub StopVOut()
    '  Stop the DAQmx task.
    DAQmxErrChk DAQmxStopTask(VOutHandle)
    DAQmxErrChk DAQmxClearTask(VOutHandle)
    VOutFlag = False
    cmdVStop.Enabled = False
    cmdVStart.Enabled = True
   
    'write zeros to the three output channels
    DAQmxErrChk DAQmxCreateTask("", VOutHandle)
    DAQmxErrChk DAQmxCreateAOVoltageChan(VOutHandle, "Dev1/ao0", "", -10, 10, DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxWriteAnalogScalarF64(VOutHandle, True, 10#, XOff.Value, ByVal 0&)
    DAQmxErrChk DAQmxStopTask(VOutHandle)
    DAQmxErrChk DAQmxClearTask(VOutHandle)
   
    DAQmxErrChk DAQmxCreateTask("", VOutHandle)
    DAQmxErrChk DAQmxCreateAOVoltageChan(VOutHandle, "Dev1/ao1", "", -10, 10, DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxWriteAnalogScalarF64(VOutHandle, True, 10#, YOff.Value, ByVal 0&)
    DAQmxErrChk DAQmxStopTask(VOutHandle)
    DAQmxErrChk DAQmxClearTask(VOutHandle)
   
    DAQmxErrChk DAQmxCreateTask("", VOutHandle)
    DAQmxErrChk DAQmxCreateAOVoltageChan(VOutHandle, "Dev1/ao2", "", -10, 10, DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxWriteAnalogScalarF64(VOutHandle, True, 10#, 0#, ByVal 0&)
    DAQmxErrChk DAQmxStopTask(VOutHandle)
    DAQmxErrChk DAQmxClearTask(VOutHandle)
   
    DAQmxErrChk DAQmxCreateTask("", VOutHandle)
    DAQmxErrChk DAQmxCreateAOVoltageChan(VOutHandle, "Dev1/ao3", "", -10, 10, DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxWriteAnalogScalarF64(VOutHandle, True, 10#, 0#, ByVal 0&)
    DAQmxErrChk DAQmxStopTask(VOutHandle)
    DAQmxErrChk DAQmxClearTask(VOutHandle)
End Sub

 

'  Initialize the data array for sine wave creation
Private Sub MakeSine()
    Const Pi As Double = 3.1415926535
    For i = 0 To (SampsPerCarCyc - 1)
        VCarrier(0, i) = Sin((i * 2 * Pi) / SampsPerCarCyc)
        VCarrier(2, i) = Sin(((i + SampsPerCarCyc / 4 + Phase * SampsPerCarCyc / 360) * 2 * Pi) / SampsPerCarCyc)
    Next i
End Sub

 

'  Initialize the data array for square wave creation
Private Sub MakeSquare()
    For i = 0 To (SampsPerCarCyc - 1)
        VCarrier(0, i) = Sgn(0.01 + Sin((i * 2 * 3.14) / SampsPerCarCyc))
        VCarrier(2, i) = Sgn(0.01 + Sin(((i + Phase * SampsPerCarCyc / 360) * 2 * Pi) / SampsPerCarCyc))
    Next i
End Sub

0 Kudos
Message 8 of 11
(10,546 Views)

' Initialize the data array for triangle wave creation
Private Sub MakeTri()
    For i = 0 To (SampsPerCarCyc - 1)
        If i < SampsPerCarCyc / 2 Then
            VCarrier(0, i) = (4 * i / SampsPerCarCyc) - 1
        Else
            VCarrier(0, i) = 3 - 4 * i / SampsPerCarCyc
        End If
     Next i
    For i = 0 To (SampsPerCarCyc - 1)
        If (i + Phase * SampsPerCarCyc / 360) < SampsPerCarCyc / 2 Then
            VCarrier(2, i) = (4 * (i + Phase * SampsPerCarCyc / 360) / SampsPerCarCyc) - 1
        Else
            VCarrier(2, i) = 3 - 4 * (i + Phase * SampsPerCarCyc / 360) / SampsPerCarCyc
        End If
    Next i
End Sub

 

'  Initialize the data array for sine wave creation
Private Sub MakeAMSine()
    Const Pi As Double = 3.1415926535
    For i = 0 To (SampsPerModCyc - 1)
        VMod(i) = 1 + (AMAmp / 100) * Sin((i * 2 * Pi) / SampsPerModCyc)
    Next i
End Sub

 

'  Initialize the data array for square wave creation
Private Sub MakeAMSquare()
    Const Pi As Double = 3.1415926535
    For i = 0 To (SampsPerModCyc - 1)
        VMod(i) = 1 + (AMAmp / 100) * Sgn(0.01 + Sin((i * 2 * Pi) / SampsPerModCyc))
    Next i
End Sub

 

' Initialize the data array for triangle wave creation
Private Sub MakeAMTri()
    For i = 0 To (SampsPerModCyc - 1)
        If i < SampsPerModCyc / 2 Then
            VMod(i) = 1 + (AMAmp / 100) * ((4 * i / SampsPerModCyc) - 1)
        Else
            VMod(i) = 1 + (AMAmp / 100) * (3 - 4 * i / SampsPerModCyc)
        End If
    Next i
End Sub

 

Private Sub MakeXoffSquare()
    For i = 0 To (SampsPerXoffCyc - 1)
        If i < SampsPerXoffCyc / 2 Then
            VXoff(i) = XOff.Value + XoffAmp.Value
        Else
            VXoff(i) = XOff.Value - XoffAmp.Value
        End If
    Next i
End Sub

 

Private Sub MakeYoffSquare()
    For i = 0 To (SampsPerXoffCyc - 1)
        If i < SampsPerXoffCyc / 2 Then
            VYoff(i) = YOff.Value + YoffAmp.Value
        Else
            VYoff(i) = YOff.Value - YoffAmp.Value
        End If
    Next i
End Sub


Private Sub Form_Unload(Cancel As Integer)
  '  Quit the program.
    If VOutFlag Then
        StopVOut
    End If
    End
End Sub
   
Private Sub OptSine_Click()
    OptSquare.Value = False
    OptTri.Value = False
    If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub OptSquare_Click()
    OptSine.Value = False
    OptTri.Value = False
    If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub OptTri_Click()
    OptSine.Value = False
    OptSquare.Value = False
    If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub OptAMSine_Click()
    OptAMSquare.Value = False
    OptAMTri.Value = False
    If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub OptAMSquare_Click()
    OptAMSine.Value = False
    OptAMTri.Value = False
    If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub OptAMTri_Click()
    OptAMSine.Value = False
    OptAMSquare.Value = False
    If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub Freq_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub Phase_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
    If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub XAmp_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub YAmp_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub AMPer_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub AMAmp_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub XOff_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub XoffAmp_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub XoffPer_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub


Private Sub YOff_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub YOffAmp_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub LockinAmp_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub LockinPhase_ValueChanged(Value As Variant, PreviousValue As Variant, ByVal OutOfRange As Boolean)
If VOutFlag Then UpdateWaveform
End Sub

 

Private Sub optBinned_Click()
optTimeTagged.Value = 0
optBinned.Value = 1
End Sub

 

Private Sub optTimeTagged_Click()
optBinned.Value = 0
optTimeTagged.Value = 1
End Sub

0 Kudos
Message 9 of 11
(10,545 Views)
Solution
Accepted by topic author yanjiang

Hi Yan,

 

I have tried striping down your code to simply two analog input tasks with the chksave and chksaveAOV, but did not observe the behavior you are describing. Could you perhaps try doing the same and seeing if this problem persists. If you do not wish to troubleshoot the code, maybe we can make some modifications in the hopes of working around this problem. My recommendation is that since you will only be running one of the two input tasks at any given time, you can all task related functions such as creation, configuration, operation and cleanup in one large if statement rather than having multiple if statement checks for each stage.

 

There must be something that we are overlooking, but I believe that this suggestion may help work around the problem, not mentioning the fact the code will also look a lot cleaner. Smiley Happy 

S_Hong
National Instruments
Applications Engineer
Message 10 of 11
(10,500 Views)