convert typedef void to vb.net

Post your .Net discussions here
Post Reply
TimOster
Newbie
Posts: 0
Joined: Mon Dec 01, 2014 5:36 pm

convert typedef void to vb.net

Post by TimOster »

In the function ps2000_get_streaming_last_values, there is a Datatype "GetOverviewBuffersMaxMin" that has to be defined in the vb code.

In the C code .h file I found:

typedef void (PREF4 *GetOverviewBuffersMaxMin)
(
int16_t **overviewBuffers,
int16_t overflow,
uint32_t triggeredAt,
int16_t triggered,
int16_t auto_stop,
uint32_t nValues
);

However I don't understand how to put this into the vb code like I did with "typedef enum".

Anyone done this?

Thanks.

Tim

Karunen
Advanced User
Advanced User
Posts: 194
Joined: Thu Nov 21, 2013 9:22 am

Re: convert typedef void to vb.net

Post by Karunen »

Hi Tim,

This is the callback which allows the device to stream data fast.

Unfortunately VB .NET does not support unsafe code

int16_t **overviewBuffers -> means a dynamic 2D array which is unsafe code

Below is a short example that shows how to stream data data at micro second sample intervals, but as the data is unmanaged I was only able to implement this on channel A.

Code: Select all

Imports System.Runtime.InteropServices
Imports System

Module PS2000

    'Driver Function calls
    Declare Function ps2000_open_unit Lib "ps2000.dll" () As Short
    Declare Function ps2000_get_unit_info Lib "ps2000.dll" (ByVal handle As Short, ByVal info As String, ByVal stringLength As Short, ByVal line As Short) As Short
    Declare Function ps2000_set_channel Lib "ps2000.dll" (ByVal handle As Short, ByVal channel As Short, ByVal enabled As Short, ByVal dc As Short, ByVal range As Short) As Short
    Declare Function ps2000_set_trigger Lib "ps2000.dll" (ByVal handle As Short, ByVal source As Short, ByVal threshold As Short, ByVal direction As Short, ByVal delay As Short, ByVal auto_trigger_ms As Short) As Short
    Declare Function ps2000_run_streaming_ns Lib "ps2000.dll" (ByVal handle As Short, ByVal sample_interval As UInteger, ByVal time_units As Integer, ByVal max_samples As UInteger, ByVal auto_stop As Short, ByVal noOfSamplesPerAggregate As UInteger, overview_buffer_size As UInteger) As Short
    Declare Function ps2000_get_values Lib "ps2000.dll" (ByVal handle As Short, ByVal buffer_a As Short, ByVal buffer_b As Short, ByVal buffer_c As Short, ByVal buffer_d As Short, ByVal overflow As Short, ByVal no_of_samples As Long) As Short
    Declare Function ps2000_get_streaming_last_values Lib "ps2000.dll" (ByVal handl As Short, ByVal lpGetOverviewBuffersMaxMin As my_get_overview_buffers) As Short
    Declare Function ps2000_close_unit Lib "ps2000.dll" (ByVal handle As Short) As Short

    'Creates callback delegate
    Public Delegate Sub my_get_overview_buffers(ByRef Overviewbuffer As IntPtr, ByVal overflow As Short, ByVal triggeredAt As UInteger, ByVal triggered As Short, ByVal autoStop As Short, ByVal nValues As UInteger)
    Public ps2000StreamingCallBack As my_get_overview_buffers

    Public overviewbuffersize As UInteger = 50000
    Public appBuffer(199999) As Short
    Public _AutoStop As Boolean
    Public _nValues As UInteger
    Public _TotalNSamples As UInteger
    Public overviewbuffersafe(overviewbuffersize) As Short

    Sub Main()
        Dim handle As Short
        Dim status As Short
        Dim file As IO.StreamWriter

        handle = ps2000_open_unit       'Opens unit and gets handle to control unit

        If handle = 0 Then 'if device is not openned then do not run the rest of the code
            End
        End If


        status = ps2000_set_channel(handle, 0, 1, 1, 7) 'handle, channel(A), enable(1), voltage range(+/-2V)
        status = ps2000_set_channel(handle, 1, 0, 1, 7) 'handle, channel(B), enable(0), voltage range(+/-2V) Channel B must be off

        'status = ps2000_set_trigger(handle, 0, 8, 0, 0, 1)   'set trigger source = Chan A, threshold = 8bits, direction= rising, delay = 0, auto_trigger_ms = 1

        _AutoStop = 0
        _TotalNSamples = 0

        ps2000StreamingCallBack = New my_get_overview_buffers(AddressOf StreamingCallBack) 'links function to callback of driver

        status = ps2000_run_streaming_ns(handle, 1, 3, 100000, 1, 1, overviewbuffersize) ' Starts device streaming

        While (Not _AutoStop)

            ps2000_get_streaming_last_values(handle, ps2000StreamingCallBack) 'trys to pull data from device

        End While

        file = My.Computer.FileSystem.OpenTextFileWriter("stream.txt", False)
        For i As Integer = 0 To _TotalNSamples - 1

            file.WriteLine(appBuffer(i))

        Next

        file.Close()

        Console.WriteLine()
        ps2000_close_unit(handle)

    End Sub

    Public Sub StreamingCallBack(ByRef Overviewbuffer As IntPtr, ByVal overflow As Short, ByVal triggeredAt As UInteger, ByVal triggered As Short, ByVal autoStop As Short, ByVal nValues As UInteger)

        Dim count As Integer = 0

        _AutoStop = Convert.ToBoolean(autoStop)
        _nValues = nValues

        If Not nValues = 0 Then 'if no values are pulled from device return to main thread
            Marshal.Copy(Overviewbuffer, overviewbuffersafe, 0, _nValues)

            Try

                Array.Copy(overviewbuffersafe, 0, appBuffer, _TotalNSamples, _nValues)

            Catch ex As Exception

                Console.WriteLine("AppBuffer overflowing please resize")

            End Try

        End If

        _TotalNSamples += _nValues
    End Sub

End Module

You way wish to investigative using C# or looking at the way VB .NET handle unmanaged code.

Kind Regards,
Karunen

Technical Specialist
Pico Technology

TimOster
Newbie
Posts: 0
Joined: Mon Dec 01, 2014 5:36 pm

Re: convert typedef void to vb.net

Post by TimOster »

Thank you. We take a look at the code.

I really have no choice. We write all our in-house test programs in vb.net. That allows any COG Engineer to write the test code, rather than rely on our software engineers...who have too much to do anyway.

Thanks again,

Tim

Karunen
Advanced User
Advanced User
Posts: 194
Joined: Thu Nov 21, 2013 9:22 am

Re: convert typedef void to vb.net

Post by Karunen »

Hi Tim,

If you email support@picotech.com I can send you the C# code which shows how to use the call back to get data from the device.

Kind Regards,
Karunen

Technical Specialist
Pico Technology

TimOster
Newbie
Posts: 0
Joined: Mon Dec 01, 2014 5:36 pm

Re: convert typedef void to vb.net

Post by TimOster »

I'm not trying to be offensive, but C# is of no use to me.

Tim

Karunen
Advanced User
Advanced User
Posts: 194
Joined: Thu Nov 21, 2013 9:22 am

Re: convert typedef void to vb.net

Post by Karunen »

Hi Tim,

I see you have commented on http://www.picotech.com/support/topic4486.html if you are not using ps2000_run_streaming_ns you do not need to use the streaming callback.

Kind Regards,
Karunen

Technical Specialist
Pico Technology

Post Reply