'/****************************************************************************
'    Copyright © 2005,2006 Agilent Technologies Inc. All rights
'    reserved.
'
' You have a royalty-free right to use, modify, reproduce and distribute
' the Sample Application Files (and/or any modified version) in any way
' you find useful, provided that you agree that Agilent has no
' warranty,  obligations or liability for any Sample Application Files.
'
' Agilent Technologies provides programming examples for illustration only,
' This sample program assumes that you are familiar with the programming
' language being demonstrated and the tools used to create and debug
' procedures. Agilent support engineers can help explain the
' functionality of Agilent software components and associated
' commands, but they will not modify these samples to provide added
' functionality or construct procedures to meet your specific needs.
'****************************************************************************/

' This program demonstrates how to receive digital data on the 34950A module
' and place it into memory (creating a Trace).  The data is received by this
' card and was transmitted from some other device (not shown).
'

Imports System
Imports Agilent.Agilent34980A.Interop
Imports Agilent.Agilent34950A.Interop
Imports Agilent.Agilent34945A.Interop

Module Module1

    Sub Main()

        Dim host As New Agilent.Agilent34980A.Interop.Agilent34980A
        
        Dim digIO As Agilent.Agilent34950A.Interop.IAgilent34950A

        Try
            Dim standardInitOptions As String
            Dim chanIsClosed As Boolean

            ' Setup IVI-defined initialization options
            standardInitOptions = "Cache=false, InterchangeCheck=false, QueryInstrStatus=true, RangeCheck=true, RecordCoercions=false, Simulate=false"

            ' Initialize DMM and clear status system/errors
            host.Initialize("TCPIP0::156.140.92.11::INSTR", False, True, standardInitOptions)

            host.Status.Clear()
            ' Select the 34951 Personality:
            digIO = host.Personality.Select("Agilent34950A")

            ' Configure receiver for Input, width = byte, threshold 2.5
            digIO.Digital.Input.Configure("1101", Agilent34950AChannelWidthEnum.Agilent34950AChannelWidthByte, 2.5)

            ' Set up handshaking
            digIO.Digital.Handshake.Configure("1101", Agilent34950AHandshakeModeEnum.Agilent34950AHandshakeModeSynchronous, 2.5, 5.0)
            digIO.Digital.Handshake.SetPolarity("1101", Agilent34950AHandshakeLineIDEnum.Agilent34950AHandshakeLineIDH2, Agilent34950APolarityEnum.Agilent34950APolarityInverted)

            ' NOTE: Polarity above may need to be "Inverted" if you are pacing the sender
            ' with the same clock the receiver is using -- that way, the rising edge can 
            ' cause an output on the sender, and the falling edge can start the "read";
            ' the time between edges allows the data to become "stable" before the read. 
            ' Unpredictable results occur if you attempt to use the same edge of a single clock
            ' source to both pace the "send" and to trigger the "read".

            ' set up receiver, rate is 500K, external source for the strobe
            digIO.Digital.Handshake.Rate("1101") = 500000.0
            digIO.Digital.Handshake.SyncStrobe("1101") = Agilent34950AHandshakeStrobeSourceEnum.Agilent34950AHandshakeStrobeSourceExternal
            digIO.Digital.Input.Memory.Clear("1101")

            ' Set sample count to 3000 
            digIO.Digital.Input.Memory.SampleCount("1101") = 3000

            ' Set up the sender (not shown here)

            ' Start the receiver first, then start the sender
            digIO.Digital.Input.Memory.Start("1101")

            ' Start the sender, he will pace the transfer

            ' After transfer complete, stop the receiver 
            digIO.Digital.Input.Memory.Stop("1101")

            ' Now read what was written into memory
            ' NOTE: You MUST Disable memory on the receiver before reading the contents
            digIO.Digital.Input.Memory.Enable("1101") = False

            ' Set a longer timeout before data fetch
            host.System.TimeoutMilliseconds = 8000

            ' declare array to hold data
            Dim dataArray() As Integer
            Dim numPoints As Integer
            ' See how much data was sent
            numPoints = digIO.Digital.Input.Memory.DataPoints("1101")

            If (numPoints > 0) Then
                dataArray = digIO.Digital.Input.Memory.ReadAllData("1101")
                Console.WriteLine("{0} data points were transmitted.", numPoints)
            Else
                Console.WriteLine("No data was transmitted.")
            End If

        Catch ex As Exception
            Dim errNum As Integer
            Dim errMssg As String

            Console.WriteLine(ex.Message)
            errNum = 5
            errMssg = ""
            While (errNum <> 0)
                host.Utility.ErrorQuery(errNum, errMssg)
                If (errNum <> 0) Then
                    Console.WriteLine("{0}", errMssg)
                End If
            End While

        End Try

        ' Close drivers
        host.Close()

        Console.WriteLine("Done - Press Enter to Exit")
        Console.ReadLine()

    End Sub

End Module
