ps4000SetSigGenBuiltIn problem

Post your VB and VBA discussions here
Post Reply
joseph25
Newbie
Posts: 0
Joined: Tue Feb 18, 2014 10:48 am

ps4000SetSigGenBuiltIn problem

Post by joseph25 »

hi ,everybody
I am trying to interface the Picoscope 4262 unit through Excel macros running under Windows XP professional enivronment. While the data capture is working very well and I have been able to modify it to a certain extent for my application, I am finding it difficult to address the Signal Genrator part. For my application, I need to sweep the frequency and collect the profiles from the two channels. The following code is troubling me.

In the declaration part I have


Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll" (ByVal handle As Integer, ByVal offsetVoltage As Long, ByVal pkToPk As Long, ByVal waveType As Long, ByVal startFrequency As Single, ByVal stopFrequency As Single, ByVal incriment As Single, ByVal dwellTime As Single, ByVal sweepType As Long, ByVal operationType As short, ByVal shots As Long, ByVal triggerType As Integer, ByVal sweeps As Long, ByVal triggerType As Integer, ByVal triggerSource As Integer, ByVal extInThreshold As Single) As Integer


and in the generator setting part

Call ps4000SetSigGenBuiltIn(handle, 0, 1000000, 1, 1000, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0)
The macro generates the following error

Compile error: User defined type not defined.

I guess that the problem is the way I am declaring the TriggerType and TriggerSource Variables. I simply want to set these as Rising and Internal trigger. How do I go about it?



by
vijay

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: ps4000SetSigGenBuiltIn problem

Post by Martyn »

You have ByVal triggerType As Integer twice

Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll" (ByVal handle As Integer, ByVal offsetVoltage As Long, ByVal pkToPk As Long, ByVal waveType As Long, ByVal startFrequency As Single, ByVal stopFrequency As Single, ByVal incriment As Single, ByVal dwellTime As Single, ByVal sweepType As Long, ByVal operationType As short, ByVal shots As Long, ByVal triggerType As Integer, ByVal sweeps As Long, ByVal triggerType As Integer, ByVal triggerSource As Integer, ByVal extInThreshold As Single) As Integer
Martyn
Technical Support Manager

joseph25
Newbie
Posts: 0
Joined: Tue Feb 18, 2014 10:48 am

Re: ps4000SetSigGenBuiltIn problem

Post by joseph25 »

Hi as you found that......... ByVal triggerType As Integer was twice ,Now i changed as


Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll" (ByVal handle As Integer, ByVal offsetVoltage As Long, ByVal pkToPk As Long, ByVal waveType As Long, ByVal startFrequency As Single, ByVal stopFrequency As Single, ByVal incriment As Single, ByVal dwellTime As Single, ByVal sweepType As Long, ByVal operationType As short, ByVal shots As Long, ByVal sweeps As Long, ByVal triggerType As Integer, ByVal triggerSource As Integer, ByVal extInThreshold As Single) As Integer


But still i am getting same problem please give any other suggestions

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: ps4000SetSigGenBuiltIn problem

Post by Martyn »

Can you post your example code so that we can take a look and make necessary modifications
Martyn
Technical Support Manager

joseph25
Newbie
Posts: 0
Joined: Tue Feb 18, 2014 10:48 am

Re: ps4000SetSigGenBuiltIn problem

Post by joseph25 »

hi i am posting my code , please if you find any mistakes please correct it

Code: Select all

Type TRIGGER_CONDITION
channelA As Long
channelB As Long
channelC As Long
channelD As Long
external As Long
aux As Long
pulseWidthQualifier As Long
End Type


Declare Function ps4000OpenUnit Lib "ps4000.dll" (handle As Integer) As Integer
Declare Sub ps4000CloseUnit Lib "ps4000.dll" (ByVal handle As Integer)

'Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll" (ByVal handle As Integer, ByVal offsetVoltage As Long, ByVal pkToPk As ULong, ByVal waveType As short, ByVal startFrequency As Single, ByVal stopFrequency As Single, ByVal incriment As Single, ByVal dwellTime As Single, ByVal sweepType As Long, ByVal operationType As short, ByVal shots As Long, ByVal sweeps As Long, ByVal triggerType As Integer, ByVal triggerSource As Integer, ByVal extInThreshold As Single) As Integer
Declare Function ps4000GetUnitInfo Lib "ps4000.dll" (ByVal handle As Integer, ByVal str As String, ByVal lth As Integer, requiredSize As Integer, ByVal info As Integer) As Integer
'Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll" (ByVal handle As short, ByVal offsetVoltage As Long, ByVal pkToPk As Long, ByVal wavetype As short, ByVal startFrequency As float, ByVal stopFrequency As float, ByVal increment As float, ByVal dwellTime As float, SweepType As Integer, ByVal operationType As short, ByVal shots As Long, ByVal sweeps As Long, triggerType As SIGGEN_RISING, triggerSource As SIGGEN_NONE, ByVal extInThreshold As short) As Integer
Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll" (ByVal handle As Integer, ByVal offsetVoltage As Long, ByVal pkToPk As Long, ByVal waveType As short, ByVal startFrequency As Single, ByVal stopFrequency As Single, ByVal incriment As Single, ByVal dwellTime As Single, ByVal sweepType As Long, ByVal operationType As short, ByVal shots As ULong, ByVal sweeps As ULong, ByVal triggerType As Integer, ByVal triggerSource As Integer, ByVal extInThreshold As Single) As Integer
'Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll"   ( ByVal handle As integer,ByVal offsetVoltage As Long, Integer, ByVal pkToPk As Long , LongWord;waveType : smallint;startFrequency : Single;stopFrequency : Single;increment : Single;dwellTime : Single;sweepType : Integer;whiteNoise : smallint;shots : LongWord;sweeps : LongWord;triggerType : Integer;triggerSource : Integer;extInThreshold: smallint) : Integer;
Declare Function ps4000SetChannel Lib "ps4000.dll" (ByVal handle As Integer, ByVal channel As Integer, ByVal enabled As Integer, ByVal dc As Integer, ByVal range As Integer) As Integer
Declare Function ps4000GetTimebase Lib "ps4000.dll" (ByVal handle As Integer, ByVal timebase As Long, ByVal noSamples As Long, timeInterval As Long, ByVal oversample As Integer, maxSamples As Long, ByVal segment As Integer) As Integer
Declare Function ps4000SetDataBuffer Lib "ps4000.dll" (ByVal handle As Integer, ByVal channel As Integer, buffer As Integer, ByVal length As Long) As Integer
Declare Function ps4000GetValues Lib "ps4000.dll" (ByVal handle As Integer, ByVal startIndex As Long, numSamples As Long, ByVal downSampleRatio As Long, ByVal downsampleRatioMode As Integer, ByVal segmentIndex As Integer, overflow As Integer) As Integer
Declare Function ps4000Stop Lib "ps4000.dll" (ByVal handle As Integer) As Integer

Declare Function ps4000RunStreaming Lib "ps4000.dll" (ByVal handle As Integer, sampleInterval As Long, ByVal sampleIntervalTimeUnits As Integer, ByVal maxPreTriggerSamples As Long, ByVal maxPostPreTriggerSamples As Long, ByVal autoStop As Integer, ByVal downSampleRatio As Long, ByVal overviewBufferSize As Long) As Integer

Declare Function ps4000SetTriggerChannelConditions Lib "ps4000.dll" (ByVal handle As Integer, conditions As TRIGGER_CONDITION, ByVal nConditions As Integer) As Integer
Declare Function ps4000SetTriggerChannelDirections Lib "ps4000.dll" (ByVal handle As Integer, ByVal channelA As Long, ByVal channelB As Long, ByVal channelC As Long, ByVal channelD As Long, ByVal ext As Long, ByVal aux As Long) As Integer
Declare Function SetTriggerProperties Lib "ps4000wrap.dll" (ByVal handle As Integer, triggerChannelPropertiesArray As Long, ByVal nProperties As Integer, ByVal auxEnable As Integer, ByVal autoTrig As Long) As Integer

Declare Function RunBlock Lib "ps4000wrap.dll" (ByVal handle As Integer, ByVal noPreTriggerSamples As Long, ByVal noPostTriggerSamples As Long, ByVal timebase As Long, ByVal oversample As Integer, ByVal segmentIndex As Integer) As Integer
Declare Function GetStreamingLatestValues Lib "ps4000wrap.dll" (ByVal handle As Integer) As Integer
Declare Function AvailableData Lib "ps4000wrap.dll" (ByVal handle As Integer, ByRef startIndex As Long) As Long
Declare Function AutoStopped Lib "ps4000wrap.dll" (ByVal handle As Integer) As Integer
Declare Function IsReady Lib "ps4000wrap.dll" (ByVal handle As Integer) As Integer

Declare Sub Sleep Lib "kernel32.dll" (ByVal time As Long)

Dim handle As Integer
Dim status As Integer
Dim S As String * 255

Sub GetData()
Load frmComm
    Dim overflow As Integer
    Dim max_samples As Long
    Dim time_interval As Long
    Dim required As Integer
    Dim timebase As Integer
    Dim fre As Integer
    Dim triggerChannelPropertiesArray(0 To 5) As Long
    Dim waveType As Long
    Dim offsetVoltage As Long
    Dim pkToPk As Long
    Dim sweeps As Long
    Dim startFrequency As Single
    Dim sweepType As Integer
    Dim stopFrequency As Single
    Dim increment As Single
     
'*********************************************************************
' User entries
    
    Prefil = "D:\vk\Harmonic_data_petrov\7CB" 'Directory+running filename
    'freq = Cells(29, 8) 'Give the applied frequency in Hz
    voltage = Cells(30, 8) 'Give the applied voltage in mV
    
 ' SigGen variables
 'offsetVoltage = 0
 'pkToPk = 1
 'waveType = "Sine"
 'startFrequency = 1
 'stopFrequency = 100
 'increment = 1
 
'*********************************************************************
    For kk = 1 To 3 ' Data acquisition Loop starts here
    Filenam = Prefil + LTrim(str(kk)) + ".txt"
    
'Clear the previous data, if any
    range("A4").Select
    range(Selection, Selection.End(xlDown)).Select
    range("A4:D5003").Select
    Selection.ClearContents
    range("A4").Select
 'handle = ps4000_open_unit()
    
    
    status = ps4000OpenUnit(handle)
  
    If handle = 0 Then
        MsgBox "Unit not opened", vbOKOnly, "Error Message"
        Exit Sub
    End If
  
   ' For i = 0 To 5 Step 1
    '    j = ps4000GetUnitInfo(handle, S, 255, required, i)
    '    Cells(18 + i, "G").Value = S
        
   ' Next i
 'waveType = Sin
 'Call ps4000SetSigGenBuiltIn(handle, offsetVoltage, pkToPk, waveType, startFrequency, stopFrequency, increment, 0, "UP", "OP_NONE", 0, 1, "RISING", "NONE", 0)
  'Call ps4000SetSigGenBuiltIn(handle, 0, 1000000, 1, 1000, 1000, 0, 0, 0, 0, 0, 0, 0, 0, 0)
    Call ps4000SetChannel(handle, 0, 1, 0, 10)
    Call ps4000SetChannel(handle, 1, 1, 0, 10)
    
    Cells(17, "G").Value = "PS4000 opened"
    
    'find the maximum number of samples, the time interval (in time_units),
    'the most suitable time units, and the maximum oversample at the current

    
    'Use the value of the applied Frequency to calculate time interval (timebase) of data points
    
    
    no_of_samples = 2500
    timebase = 1 / (no_of_samples * 0.0000001)  ' The smallest time interval is 100 ns
    fre = 1 / (timebase * no_of_samples * 0.0000001)
    ReDim values_a(no_of_samples) As Integer
    ReDim values_b(no_of_samples) As Integer
    
    status = ps4000GetTimebase(handle, timebase, no_of_samples, time_interval, 1, max_samples, 0)
    
    Call ps4000SetDataBuffer(handle, 0, values_a(0), no_of_samples)
    Call ps4000SetDataBuffer(handle, 1, values_b(0), no_of_samples)
    
    
    'Set up the trigger structures
    
    Dim triggerConditions As TRIGGER_CONDITION

    triggerConditions.channelA = 1
    triggerConditions.channelB = 0
    triggerConditions.channelC = 0
    triggerConditions.channelD = 0
    triggerConditions.external = 0
    triggerConditions.aux = 0
    triggerConditions.pulseWidthQualifier = 0
    
   
   triggerChannelPropertiesArray(0) = 0         'thresholdUpper
   triggerChannelPropertiesArray(1) = 1000      'thresholdUpperHysteresis;
   triggerChannelPropertiesArray(2) = 0         'thresholdLower
   triggerChannelPropertiesArray(3) = 1000      'thresholdUpperHysteresis;
   triggerChannelPropertiesArray(4) = 0         'channel
   triggerChannelPropertiesArray(5) = 0         'thresholdMode
      
    'Set trigger
    Call ps4000SetTriggerChannelConditions(handle, triggerConditions, 1)
    Call ps4000SetTriggerChannelDirections(handle, 2, 2, 0, 0, 0, 0)
    Call SetTriggerProperties(handle, triggerChannelPropertiesArray(0), 1, 1, 1000)

    status = RunBlock(handle, 0, no_of_samples, timebase, 0, 0)
    
    While IsReady(handle) = 0
        Sleep (0)
    Wend
    
    Call ps4000Stop(handle)
    
    ' once the ready flag is set, the data has been captured and is ready for download using get values
    status = ps4000GetValues(handle, 0, no_of_samples, 1, 0, 0, 0)
    
    For i = 0 To (no_of_samples - 1)
      Cells(i + 4, "A").Value = time_interval * i * 0.0000001 ' multiplication by 1e-7 is to convert the time (which is in units of 100ns) to seconds
     ' Cells(i + 4, "B").Value = values_a(i)
      Cells(i + 4, "B").Value = (values_a(i) / 32767) * 20000  'Channel A on the scope, normally the applied field
      'Cells(i + 4, "D").Value = values_b(i)
      Cells(i + 4, "E").Value = (values_b(i) / 32767) * 20000   'Channel B on the scope, normally the sample response
    Next i
    Call Setmett
Call getMett
    
    ps4000CloseUnit (handle)
'Call Expofilter  ' Smoothenes the sample response using Exponential smoothing routine (Excel's Data Analysis package)
    
 Call WritFile(ByVal Filenam)
    
 Next kk
 Sleep (5)
End Sub
'Sub Expofilter()
'Exponential smoothing
 'Application.Run "ATPVBAEN.XLAM!Expon", ActiveSheet.range("$E$4:$E$2503"), _
        ActiveSheet.range("$c$4:$c$2503"), 0.7, False, False, False
'End Sub

Sub WritFile(ByVal Filenam)
'

' Write data (From Picoscope and Mettler) to a tab limited text file at a path defined by user
    
    range("A4").Select
    range(Selection, Selection.End(xlDown)).Select
    range("A4:D2503").Select
    Selection.Copy
   ' Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    '    :=False, Transpose:=False
    range("A1").Select
    Sheets.Add After:=Sheets(Sheets.Count)
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    'ActiveSheet.Paste
  'Save File
    ActiveWorkbook.SaveAs Filename:=Filenam, FileFormat:=xlText, _
        CreateBackup:=False
Application.DisplayAlerts = False
    ActiveWindow.SelectedSheets.Delete
Sheets("Sheet1").Select
Application.DisplayAlerts = True

End Sub


Sub Setmett()
Dim outp As String
Dim buffer As String
frmComm.rs232.CommPort = 2 'Com2
frmComm.rs232.Settings = "9600,N,8,1"
frmComm.rs232.RThreshold = 13
frmComm.rs232.PortOpen = True

outp = "RL 1" + vbCrLf
frmComm.rs232.Output = outp
 
 outp = "RO 8000" + vbCrLf
frmComm.rs232.Output = outp

 outp = "RD 2" + vbCrLf
frmComm.rs232.Output = outp

  outp = "RM 1" + vbCrLf
frmComm.rs232.Output = outp
'If frmComm.rs232.inputBufferCount > 0 Then
'buffer = frmComm.rs232.Input
'End If
frmComm.rs232.PortOpen = False
End Sub
Sub getMett()
'Read Data
Dim buffer As String


 frmComm.rs232.PortOpen = True

 outp = "RD 2" + vbCrLf
frmComm.rs232.Output = outp
Sleep (1000)

buffer = frmComm.rs232.Input

'With RM 8000 mode declared above temp will be in K and has 2 decimal place resolution
'sd = Val(Left(Right(buffer, 14), 5)) / 10 - 273.15
Call convtemp(ByVal buffer, xpos)
sd = Val(Mid(buffer, xpos, 5)) / 10 - 273.15
'MsgBox (buffer)
'MsgBox (sd)
Cells(4, 4) = sd
Cells(25, 7) = "Temperature ="
Cells(26, 7) = sd
frmComm.rs232.PortOpen = False
End Sub
Sub convtemp(ByVal buffer, xpos)
For kx = 1 To Len(buffer)
    If Mid(buffer, kx, 1) = "X" Then
    xpos = kx
    End If
Next kx
xpos = xpos + 1
End Sub
Sub StreamingData()
    Dim time_interval As Long
    Dim startIndex As Long
        
    status = ps4000OpenUnit(handle)
  
    If handle = 0 Then
        MsgBox "Unit not opened", vbOKOnly, "Error Message"
        Exit Sub
    End If
    
    Call ps4000SetChannel(handle, 0, 1, 0, 10)
    Call ps4000SetChannel(handle, 1, 1, 0, 10)
    
    Cells(17, "G").Value = "PS4000 opened"
       
    Dim no_of_samples As Integer
    no_of_samples = 5000

    ReDim values_a(no_of_samples) As Integer
    ReDim values_b(no_of_samples) As Integer
    
    Call ps4000SetDataBuffer(handle, 0, values_a(0), no_of_samples)
    Call ps4000SetDataBuffer(handle, 1, values_b(0), no_of_samples)
    
    time_interval = 1000000 '(ns as time units = 2 below)
    ' setting autostop to 1 means it will auto-stop once it has (pre + post) samples
    ' the overview buffer needs to be set to the same size as the data buffer
    ' if the number of samples requested (pre + post) is > overview, it will wrap
    ' after streaming is completed, all the data can be (re)collected using ps4000GetValues
    status = ps4000RunStreaming(handle, time_interval, 2, 0, no_of_samples, 1, 1, no_of_samples)
    
    Dim prev As Integer
    Dim newSamples As Long
    Dim autoStop As Integer
    
    prev = 0
    startIndex = 0
    
    Do
        Do
            status = GetStreamingLatestValues(handle)
            Sleep (100)
        Loop While IsReady(handle) = 0
        
        autoStop = AutoStopped(handle)
        newSamples = AvailableData(handle, startIndex)
        For i = prev To prev + newSamples
          Cells(i + 4, "A").Value = time_interval * i
          Cells(i + 4, "B").Value = values_a(i)
          Cells(i + 4, "C").Value = (values_a(i) / 32767) * 20000
        Next i
        prev = prev + newSamples
    Loop While autoStop = 0
    
    Call ps4000Stop(handle)
    ps4000CloseUnit (handle)
End Sub

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: ps4000SetSigGenBuiltIn problem

Post by Martyn »

Try

Code: Select all

Declare Function ps4000SetSigGenBuiltIn Lib "ps4000.dll" (ByVal handle As Integer, ByVal offsetVoltage As Long, ByVal pkToPk As Long, ByVal waveType As Integer, ByVal startFrequency As Single, ByVal stopFrequency As Single, ByVal incriment As Single, ByVal dwellTime As Single, ByVal sweepType As Long, ByVal operationType As Integer, ByVal shots As Long, ByVal sweeps As Long, ByVal triggerType As Integer, ByVal triggerSource As Integer, ByVal extInThreshold As Integer) As Integer
Martyn
Technical Support Manager

Post Reply