VB.Net 2013 Continuous Background Scaled Data example


This is a simple 32 bit console application demonstrating device discovery, AInScan functionality. It was created in Visual Studio 2013 so when opening in a newer environment you will get warning that can be ignored. If you get an error check the project references for a possible error on the MCCDAQ.DLL reference. If it's marked as a problem, delete it and re-add it. To add it, right mouse click the project and choose Add->Reference. Select Extensions and select MccDaq.

Below is the code, the full project is attach as a zip file.

'import statements help reduce line length and typing
Imports MccDaq
Imports MccDaq.Range
Imports MccDaq.ScanOptions
Imports MccDaq.FunctionType
Imports MccDaq.ErrorInfo.ErrorCode
Imports MccDaq.MccService


Module Module1

    Const PACKETSIZE As Integer = 256
    Const CHANNELCOUNT As Integer = 4
    Const LowChan As Integer = 0
    Const HighChan As Integer = 3
    Const SAMPLERATE As Integer = 1000 'desired sample rate
    Const BUFFERSIZE As Integer = CHANNELCOUNT * PACKETSIZE ' size of the buffer
    Const HALFBUFFER As Integer = BUFFERSIZE / 2 'size of half buffer


    'immediate trigger options
    Dim Opts As MccDaq.ScanOptions = Continuous + Background + ScaleData

    Dim CurrentCount As Integer = 0 'indicates total samples taken
    Dim CurrentIndex As Integer = 0 'points to the last scan read

    Dim Status As Short = 0

    Dim DAQ As MccBoard 'device object
    Dim buffer As IntPtr
    Dim ret As ErrorInfo
    Dim ReadLower As Boolean = True 'semaphore flag that prevents duplicate buffer reads


    Sub Main()

        Dim ret As MccDaq.ErrorInfo
        Dim bNum As Integer = -1
        Dim rate As Integer = SAMPLERATE
        Dim DevStr As String = "USB-1608G"
        bNum = GetBoardNum(DevStr)

        If bNum = -1 Then
            Console.WriteLine("No {0} detected!", DevStr)
            WaitForKey()
            End
        End If

        DAQ = Nothing
        DAQ = New MccDaq.MccBoard(bNum) 'get the devices' programming object
        If DAQ Is Nothing Then
            Console.WriteLine("board not found")
            WaitForKey()
            Return
        End If

        DAQ.AInputMode(AInputMode.SingleEnded)
        buffer = MccService.ScaledWinBufAllocEx(BUFFERSIZE)

        ret = DAQ.AInScan(LowChan, HighChan, BUFFERSIZE, rate, Bip10Volts, buffer, Opts)
        If ret.Value <> MccDaq.ErrorInfo.ErrorCode.NoErrors Then Stop

        '''''''''''''''''''' MAIN LOOP ''''''''''''''''''''''''''

        Do

            ret = DAQ.GetStatus(Status, CurrentCount, CurrentIndex, MccDaq.FunctionType.AiFunction)
            If ret.Value <> MccDaq.ErrorInfo.ErrorCode.NoErrors Then Stop

            'ping pong between low half and upper half
            If ((CurrentIndex >= HALFBUFFER) And (ReadLower = True)) Then

                'read the lower half and set Upper flag to zero so that next read is upper half
                Dim data(BUFFERSIZE) As Double
                ret = MccDaq.MccService.ScaledWinBufToArray(buffer, data, 0, HALFBUFFER)
                If ret.Value <> MccDaq.ErrorInfo.ErrorCode.NoErrors Then Stop

                ReadLower = False
                DisplayData(data, 1)

            ElseIf ((CurrentIndex < HALFBUFFER) And (ReadLower = False)) Then

                'read the upper half and set Upper flag to one so that next read is lower half
                Dim data(BUFFERSIZE) As Double
                ret = MccDaq.MccService.ScaledWinBufToArray(buffer, data, HALFBUFFER, HALFBUFFER)
                If ret.Value <> MccDaq.ErrorInfo.ErrorCode.NoErrors Then Stop

                ReadLower = True
                DisplayData(data, 1)

            End If
        Loop While (Not Console.KeyAvailable)

        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        DAQ.StopBackground(AiFunction)

        WaitForKey()
    End Sub






    'This function is dedicated to printing the program channels to the console screen
    'Helper function is dedicated to printing and writing to file the data
    Sub DisplayData(ByRef data() As Double, ByVal rows As Integer)

        Dim index As Integer = 0

        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim temp(CHANNELCOUNT) As Single
        Try

            For i = 0 To rows - 1

                Dim str As String = ""


                For j = 0 To CHANNELCOUNT - 1
                    str = str + data(j).ToString("0.0000") + vbTab
                    index += 1
                Next j
                str.Trim(vbTab)
                Console.WriteLine(str)
            Next i


        Catch e As Exception
            MsgBox(e.Message)
        End Try

    End Sub


    'helper function that waits for any key to be pressed
    Sub WaitForKey()
        Dim cki As ConsoleKeyInfo
        Console.WriteLine("Press any key to continue..." + vbCrLf)
        Do
            System.Threading.Thread.Sleep(200)
        Loop Until (Console.KeyAvailable)
        cki = Console.ReadKey()
        Console.WriteLine("")

    End Sub




    'Helper function uses a string identifier to locate the devices board number. If more than one device is present then
    'it returns the first one matching the identifier.
    Function GetBoardNum(ByVal devString As String) As Integer

        MccDaq.DaqDeviceManager.IgnoreInstaCal()
        Dim inventory() As MccDaq.DaqDeviceDescriptor
        Dim BoardNum As Integer
        Dim NumOfDevices As Integer = 0

        inventory = MccDaq.DaqDeviceManager.GetDaqDeviceInventory(DaqDeviceInterface.Any)
        NumOfDevices = inventory.Length()
        If NumOfDevices > 0 Then
            For BoardNum = 0 To NumOfDevices

                If inventory(BoardNum).ProductName.Contains(devString) Then
                    Dim daqBoard As MccDaq.MccBoard = MccDaq.DaqDeviceManager.CreateDaqDevice(BoardNum, inventory(BoardNum))
                    Console.WriteLine("Product Name:    {0}", inventory(BoardNum).ProductName)
                    Console.WriteLine("Device Type # :  {0}", inventory(BoardNum).ProductID)
                    Console.WriteLine("Serial # :       {0}", inventory(BoardNum).UniqueID)

                    Return BoardNum
                End If

            Next BoardNum
        End If
        Return -1
    End Function

End Module


Posted 9/7/2021 8:17:07 AM by Administrator
https://kb.mccdaq.com/KnowledgebaseArticle50847.aspx