📄 multidevicegenericsynctclk.frm
字号:
Begin VB.Label Label13
Caption = "Waveform"
Height = 255
Left = 3600
TabIndex = 17
Top = 120
Width = 1455
End
Begin VB.Label Label9
Caption = "Error Message:"
Height = 255
Left = 120
TabIndex = 13
Top = 4200
Width = 1095
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
' MultiDeviceGenericSyncTClk
'
' Recommended Input Signal: 500 kHz, 8 Vpp, sine wave
'
' Devices Supported: NI 5122, NI 5124, NI 5922
' This example demonstrates synchronizing two SMC based digitizers, with both
' devices configured using the same parameters.
'
' For more information about synchronizing devices, refer to the
' NI High-Speed Digitizers Help.
'
' For PCI devices, this example requires the digitizers to be connected
' with a RTSI cable. Also, you need to configure a RTSI cable in Measurement &
' Automation Explorer.
'
' PXI devices will automatically have the RTSI bus connected along the back
' plane of the PXI chassis. Therefore, no additional connections are necessary.
'
Option Explicit
Private Declare Function niTClk_ConfigureForHomogeneousTriggers Lib "niTClk.dll" Alias "_niTClk_ConfigureForHomogeneousTriggers@8" _
(ByVal sessionCount As Long, _
ByRef sessions As ViSession) As Long
Private Declare Function niTClk_Synchronize Lib "niTClk.dll" Alias "_niTClk_Synchronize@16" _
(ByVal sessionCount As Long, _
ByRef sessions As ViSession, _
ByVal minTClkPeriod As ViReal64) As Long
Private Declare Function niTClk_Initiate Lib "niTClk.dll" Alias "_niTClk_Initiate@8" _
(ByVal sessionCount As Long, _
ByRef sessions As ViSession) As Long
Private Declare Function niTClk_GetExtendedErrorInfo Lib "niTClk.dll" Alias "_niTClk_GetExtendedErrorInfo@8" _
(ByVal errorString As String, _
ByVal errorStringStize As Long) As Long
Dim stopFlag As ViBoolean
Dim errorSource As String
Dim errorcode As ViStatus
Dim errorval As ViStatus
Dim gTriggerType As ViInt32
Dim niScopeErr As ViBoolean
Dim tClkErr As ViBoolean
Private Sub Form_Load()
' Set up the trigger type combo box values
TriggerTypeComboBox.AddItem "Edge", 0
TriggerTypeComboBox.AddItem "Immediate", 1
TriggerTypeComboBox.ListIndex = 0
gTriggerType = 0
End Sub
' This procedure is executed when the Acquire button is clicked
Private Sub Acquire_Click()
Acquire.Enabled = False
Quit.Enabled = False
Dim errorDesc As String * MAX_ERROR_DESCRIPTION
Dim masterVi As ViSession
Dim slaveVi As ViSession
Dim numberOfSessions As ViInt32
numberOfSessions = 2
Dim sessions() As ViSession
ReDim sessions(0 To numberOfSessions)
' Variables used to get values from the GUI
Dim resourceName As String
Dim slaveResourceName As String
Dim verticalRange As ViReal64
Dim minSampleRate As ViReal64
Dim minRecordLength As ViInt32
Dim oldMinSampleRate As ViReal64
oldMinSampleRate = 0
Dim oldMinRecordLength As ViInt32
oldMinRecordLength = 0
Dim triggerType As ViInt32
' Default values used in this example
Dim channelName As String
channelName = "0"
Dim verticalOffset As ViReal64
verticalOffset = 0
Dim verticalCoupling As ViInt32
verticalCoupling = NISCOPE_VAL_DC
Dim probeAttenuation As ViReal64
probeAttenuation = 1
Dim enforceRealTime As ViBoolean
enforceRealTime = VI_TRUE
Dim refPosition As ViReal64
refPosition = 50
Dim timeout As ViReal64
timeout = 5
Dim numRec As ViInt32
numRec = 1
Dim numWaveform As ViInt32
numWaveform = 1
Dim actualRecordLength As ViInt32
' Waveforms
Dim masterWfmInfoPtr() As niScope_wfmInfo
Dim masterWaveformPtr() As ViReal64
Dim slaveWfmInfoPtr() As niScope_wfmInfo
Dim slaveWaveformPtr() As ViReal64
niScopeErr = False
tClkErr = False
' Obtain the resource name of the device from the user interface
resourceName = ResourceNameText.Text
slaveResourceName = SlaveResourceText.Text
' Open the NI-SCOPE instrument handle for the master
errorcode = niScope_init(resourceName, NISCOPE_VAL_FALSE, NISCOPE_VAL_FALSE, masterVi)
If (handleErr(errorcode, "niScope_init")) Then GoTo Error
' Open the slave instrument handle
errorcode = niScope_init(slaveResourceName, NISCOPE_VAL_FALSE, NISCOPE_VAL_FALSE, slaveVi)
If (handleErr(errorcode, "niScope_init")) Then GoTo Error
sessions(0) = masterVi
sessions(1) = slaveVi
' Loop until the stop flag is set
stopFlag = VI_FALSE
While (stopFlag = VI_FALSE)
' Obtain the necessary parameters from the user interface
errorcode = GetParametersFromGUI(verticalRange, minSampleRate, minRecordLength, triggerType)
' Configure the master vertical parameters
errorcode = niScope_ConfigureVertical(masterVi, channelName, verticalRange, verticalOffset, _
verticalCoupling, probeAttenuation, NISCOPE_VAL_TRUE)
If (handleErr(errorcode, "niScope_ConfigureVertical")) Then GoTo Error
' Configure the master horizontal parameters
errorcode = niScope_ConfigureHorizontalTiming(masterVi, minSampleRate, minRecordLength, refPosition, numRec, enforceRealTime)
If (handleErr(errorcode, "niScope_ConfigureHorizontalTiming")) Then GoTo Error
' Configure the slave vertical parameters
errorcode = niScope_ConfigureVertical(slaveVi, channelName, verticalRange, verticalOffset, _
verticalCoupling, probeAttenuation, NISCOPE_VAL_TRUE)
If (handleErr(errorcode, "niScope_ConfigureVertical")) Then GoTo Error
' Configure the slave horizontal parameters
errorcode = niScope_ConfigureHorizontalTiming(slaveVi, minSampleRate, minRecordLength, refPosition, numRec, enforceRealTime)
If (handleErr(errorcode, "niScope_ConfigureHorizontalTiming")) Then GoTo Error
' Configure the master trigger type
Select Case triggerType
Case 0: ' Edge Trigger
errorcode = niScope_ConfigureTriggerEdge(masterVi, channelName, 0, NISCOPE_VAL_POSITIVE, _
NISCOPE_VAL_DC, 0, 0)
If (handleErr(errorcode, "niScope_ConfigureTriggerEdge")) Then GoTo Error
Case 1: ' Immediate
errorcode = niScope_ConfigureTriggerImmediate(masterVi)
If (handleErr(errorcode, "niScope_ConfigureTriggerImmediate")) Then GoTo Error
End Select
' Let TClk configure the device appropriately
errorcode = niTClk_ConfigureForHomogeneousTriggers(numberOfSessions, sessions(0))
If (handleTClkErr(errorcode, "niTClk_ConfigureForHomogeneousTriggers")) Then GoTo Error
' Synchronize the devices something timing related changes
If (minSampleRate <> oldMinSampleRate) Then
errorcode = niTClk_Synchronize(numberOfSessions, sessions(0), 0#)
If (handleTClkErr(errorcode, "niTClk_Synchronize")) Then GoTo Error
End If
' Initiate the acquisition through TClk
errorcode = niTClk_Initiate(numberOfSessions, sessions(0))
If (handleTClkErr(errorcode, "niTClk_Initiate")) Then GoTo Error
' Find out the current record length for the master
errorcode = niScope_ActualRecordLength(masterVi, actualRecordLength)
If (handleErr(errorcode, "niScope_ActualRecordLength")) Then GoTo Error
' Resize the master waveforms
ReDim masterWfmInfoPtr(numWaveform)
ReDim masterWaveformPtr((numWaveform * actualRecordLength) - 1)
' Fetch the data from the master
errorcode = niScope_Fetch(masterVi, channelName, timeout, actualRecordLength, _
masterWaveformPtr(0), masterWfmInfoPtr(0))
If (handleErr(errorcode, "niScope_Fetch")) Then GoTo Error
' Find out the current record length for the slave
errorcode = niScope_ActualRecordLength(slaveVi, actualRecordLength)
If (handleErr(errorcode, "niScope_ActualRecordLength")) Then GoTo Error
' Resize the slave waveforms
ReDim slaveWfmInfoPtr(numWaveform)
ReDim slaveWaveformPtr((numWaveform * actualRecordLength) - 1)
' Fetch the data
errorcode = niScope_Fetch(slaveVi, channelName, timeout, actualRecordLength, _
slaveWaveformPtr(0), slaveWfmInfoPtr(0))
If (handleErr(errorcode, "niScope_Fetch")) Then GoTo Error
' Remember old values
oldMinRecordLength = minRecordLength
oldMinSampleRate = minSampleRate
'Scale plot correctly and plot waveform data
acquisitionGraph.Axes(1).Maximum = (masterWfmInfoPtr(0).actualSamples * masterWfmInfoPtr(0).xIncrement) + masterWfmInfoPtr(0).relativeInitialX
acquisitionGraph.Axes(1).Minimum = masterWfmInfoPtr(0).relativeInitialX - masterWfmInfoPtr(0).xIncrement
acquisitionGraph.Plots.Item(1).PlotY masterWaveformPtr, masterWfmInfoPtr(0).relativeInitialX, masterWfmInfoPtr(0).xIncrement
acquisitionGraph.Plots.Item(2).PlotY slaveWaveformPtr, slaveWfmInfoPtr(0).relativeInitialX, slaveWfmInfoPtr(0).xIncrement
acquisitionGraph.Cursors(1).SetPosition 0, 0
DoEvents
Wend
Error:
' Intrepret the error code.
If (errorcode <> VI_SUCCESS) Then
If (niScopeErr) Then
errorcode = niScope_errorHandler(masterVi, errorval, errorSource, errorDesc)
End If
If (tClkErr) Then
errorcode = niTClk_GetExtendedErrorInfo(errorDesc, 4000)
End If
Else
errorDesc = "Acquisition Succesful!"
End If
' Display the error message
ErrorMessageTextBox.Text = errorDesc
' Close the NI-SCOPE instrument handle if it was opened successfully
If (masterVi) Then
niScope_close (masterVi)
End If
If (slaveVi) Then
niScope_close (slaveVi)
End If
Acquire.Enabled = True
Quit.Enabled = True
End Sub
' This function handles the error codes from NI-SCOPE function calls. Its
' main purpose is to allow the name of function call that may generate a
' warning (values > 0) to be preserved.
Function handleErr(errorcode As ViStatus, fname As String) As Boolean
If (errorcode < 0) Then
errorval = errorcode
errorSource = fname
handleErr = True
niScopeErr = True
ElseIf (errorval = 0) Then
errorval = errorcode
errorSource = fname
handleErr = False
End If
End Function
' This function handles the error codes from NI-SCOPE function calls. Its
' main purpose is to allow the name of function call that may generate a
' warning (values > 0) to be preserved.
Function handleTClkErr(errorcode As ViStatus, fname As String) As Boolean
If (errorcode < 0) Then
errorval = errorcode
errorSource = fname
handleTClkErr = True
tClkErr = True
ElseIf (errorval = 0) Then
errorval = errorcode
errorSource = fname
handleTClkErr = False
End If
End Function
' This function gets all necesary parameters from the GUI. It gets the values
' from the component works num edits and from the text boxes
Function GetParametersFromGUI(verticalRange As ViReal64, minSampleRate As ViReal64, minRecordLength As ViInt32, _
triggerType As ViInt32)
verticalRange = VerticalRangeNumEdit.Value
minSampleRate = MinSampleRateNumEdit.Value
minRecordLength = MinRecordLengthNumEdit.Value
triggerType = gTriggerType
GetParametersFromGUI = 0
End Function
' Called to terminate the program
Private Sub Quit_Click()
stopFlag = VI_TRUE
End
End Sub
' Called when stop button is pressed
Private Sub Stop_Click()
stopFlag = VI_TRUE
End Sub
' Trigger type combo box was clicked
Private Sub TriggerTypeComboBox_Click()
gTriggerType = TriggerTypeComboBox.ListIndex
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -