Test and Measurement Forum

PS4000 - Problem showing values on screen

Post your .Net discussions here

PS4000 - Problem showing values on screen

Postby Stevo » Mon Mar 26, 2012 6:56 pm

Hi there,

I've been trying for weeks without success to get my program to stream values without crashing. Hope anybody can give me some direction! I apologize that I could not get the code to be formatted properly in this message.

Background:
Briefly, my main goal is to have a vb.net (visual basic) form which will continuously display data from my PS4226. I am using the PS4000.dll. The way it would display is basically obtain data from the scope as data points, and then output it to a picturebox so that it looks like an oscilloscope screen within my program. Since the user could look at the screen indefinitely, I assumed that STREAMING mode would be the correct choice. Currently my program is a simple form with two buttons. When the user presses the "go" button, it will start the thread for the scope to operate in streaming mode. The second button stops this procedure. Instead of a "picturebox", I am using a label (for debug purpose) which outputs the data. I am testing using a finger on the probe ie. I expect some sort of 60Hz signal.

The Problem:
I am able to have my label show the incoming data, but after several calls to my delegate (so that I can update my label on the main UI), I obtain a "FatalExecutionEngineError". I've searched that this error is usually in bad parameter declarations. I am hoping somebody could help me decipher where the problem could be. My best guess is in the parameters fed to getStreamingLatestValues. I've put a portion of my code below:


Code: Select all
Declare Function ps4000RunStreaming Lib "ps4000.dll" (ByVal handle As Short, ByRef sampleInterval As UInteger, ByVal sampleIntervalTimeUnits As Integer, ByVal maxPreTriggerSamples As UInteger, ByVal maxPostTriggerSamples As UInteger, ByVal autoStop As Short, ByVal downSampleRatio As UInteger, ByVal overviewBufferSize As UInteger) As Integer

Declare Function ps4000GetStreamingLatestValues Lib "ps4000.dll" (ByVal handle As Short, ByVal lpPs4000Ready As myDelCallback, ByRef pParameter As IntPtr) As Integer

' This is the callback for the picoscope when streaming has occurred and data is ready.
Public Delegate Sub myDelCallback(ByVal handle As Short, ByVal noOfSamples As Integer, ByVal startIndex As UInteger, ByVal overflow As Short, ByVal triggerAt As UInteger, ByVal triggered As Short, ByVal autoStop As Short)

' This is the delegate method to update UI
Public Delegate Sub setScopeInvoker(ByVal textValue As String)

   ' *****************************************************
   ' User clicks this button on UI to initiate the streaming process
   ' *****************************************************
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        CollectStream(UnitModel, TrigSettings)
        Dim tparam As New Thread(DirectCast(Sub() BackgroundProcess_getOscilloscopeData(UnitModel, "TEMP_STRING"), 
                                                                                                                                            ThreadStart))
        If tparam IsNot Nothing AndAlso tparam.IsAlive() Then
            tparam.Abort()
        End If
        tparam.Start()
    End Sub

   ' ******************************************************************
   ' This function begins calls the streaming code for picoscope
   ' ******************************************************************
   Private Sub BackgroundProcess_getOscilloscopeData(ByRef UnitModel As UNIT_MODEL, ByVal textCommand As String)
        StreamDataHandler(UnitModel)
    End Sub

    ' *****************************************************************
    ' Perform the actual code here
    ' *****************************************************************
Sub StreamDataHandler(ByRef UnitModel As UNIT_MODEL)
Dim status As Short
        Dim buffers(99) As Short           ' Using different buffer lengths to see if I could fix the problem               
        Dim no_of_samples As Integer   ' Only using a single channel, thus not using a 2D array
        Dim i As Integer = 0
        Dim max_samples As Integer
        Dim time_interval As Integer
        Dim timebase As Integer

        no_of_samples = 100
        status = ps4000SetDataBuffer(UnitModel.handle, chan, buffers(0), no_of_samples)
       g_autoStop = False

       'OBTAIN AN APPROPRIATE TIME BASE
       timebase = 0       'try the fastest sample rate first, and increase timebase value until the call succeeds
       While (ps4000GetTimebase(UnitModel.handle, timebase, no_of_samples, time_interval, 1, max_samples, 0) <>   
                                                                                                                                                             PICO_OK)
            timebase = timebase + 1
       End While

      Dim p As New IntPtr

      ' BEGIN STREAMING FROM SCOPE, IS MY SAMPLEINTERVAL APPROPRIATE FOR A 60HZ SIGNAL?
      sampleInterval = 1
      status = ps4000RunStreaming(UnitModel.handle, sampleInterval, TimeUnits.PS4000_MS, 0, no_of_samples, False, 1,
                                                                                                                                                           no_of_samples)

       While g_autoStop = 0
                System.Threading.Thread.Sleep(100)
                g_ready = False
                status = ps4000GetStreamingLatestValues(UnitModel.handle, AddressOf sayHello, p)
               
                If g_ready And g_sampleCount > 0 Then ' Can be ready and have no data, if autostop has fired
                      totalSamples += g_sampleCount
                     ' OUTPUT WHATEVER WAS GRABBED FROM EACH SUCCESSFUL CALL TO GETSTREAMINGLATEST..
                      For k As Integer = 0 To g_sampleCount - 1
                          test = adc_to_mv(buffers(k), UnitModel.channelSettings(chan).range)
                          [b]showScopeValue(test.ToString) ' DELEGATE FUNCTION TO UPDATE UI
                          ' NOTE: This is where the problem occurs.. after totalsamples > 1000, i get the fatalerror.
                          ' This occurs when runstreaming and getstreaminglatestvalues are both successful.
                          ' I suspect it has to do with the no_of_samples, or the sampleInterval I am passing in as parameters to   
                          ' the runstreaming function[/b]
                    Next
                End if
      End While
      Call ps4000Stop(UnitModel.handle)
End Sub

' *************************************************************************
' This procedure is called to update the main UI safely             
' *************************************************************************
Public Sub showScopeValue(ByVal textValue As String)
        If Me.lb_scopeValue.InvokeRequired Then
            Dim del As New setScopeInvoker(AddressOf showScopeValue)
            Me.lb_scopeValue.Invoke(del, textValue)
        Else
            Me.lb_scopeValue.Text = textValue.ToString
        End If

    End Sub

Thank you!
Stevo
Stevo
Newbie
 
Posts: 0
Joined: Mon Mar 26, 2012 6:12 pm

Re: PS4000 - Problem showing values on screen

Postby Hitesh » Thu Mar 29, 2012 11:19 am

Hi Stevo,

Looking at the function declarations in your code, I would recommend referring to the ps4000.xls file containing VBA code in the SDK which shows how to capture streaming data instead of using callback functions in your code. Which version of the SDK are you using?

The VBA code makes use of the following functions for streaming from the ps4000wrap.dll :

Code: Select all
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


You will have to port this code over to VB6 - please let me know if this resolves the issue.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: PS4000 - Problem showing values on screen

Postby Stevo » Mon Apr 02, 2012 10:06 pm

Hi Hitesh,

I tried using the ps4000wrap.dll as well and subsequently removed my callback function and used the isReady method. The problem still showed up. I wonder if it has sometime to do with incorrect time base values which causes some form of timing issue with collecting data.

In any case, I tried reverting to the block mode implementation and I am able to get a continuous looking set of data on the screen. My guess is that in blockmode I will be missing some data in between cycles because of the tiny overhead in stopping and restarting the get data methods. However, I am not too concerned with missing data at the moment so I think this should suffice!

Thanks,

Stevo
Stevo
Newbie
 
Posts: 0
Joined: Mon Mar 26, 2012 6:12 pm

Re: PS4000 - Problem showing values on screen

Postby Hitesh » Tue Apr 03, 2012 1:05 pm

Hi Stevo,

Looking through your code again, I've noticed that you call the ps4000getTimebase() function - this isn't necessary for using the scope in Streaming mode.

Also, I noticed that the autoStop parameter is set to 'False' (0) - have you tried setting it to 1/True?

Your sample interval of 1ms should be ok for a 60Hz signal as you probably want to be sampling at least 5 times the frequency of the signal which would be 3.33ms (300Hz).

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2058
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire


Return to .Net Languages (C#, VB.Net, .)

Who is online

Users browsing this forum: No registered users and 0 guests