Private Sub Run_Click() Dim dblValue As Double Dim lngError As Long Dim lngErrorAddress As Long Dim strErrorAddress As String Dim strError As String * LJM_MAX_NAME_SIZE Dim ScansPerRead As Long Dim NumAddresses As Long Dim aScanList() As Long Dim ScanRate As Double Dim ErrorAddress As Long Dim aValues(5120) As Double Dim sineFrequency As Double Dim sineAmplitude As Double Dim theta As Double Dim strScanListNames() As String Dim lngTypes() As Long Dim lngScanList() As Long Dim lngLoopCnt As Long Dim lngTotalLoops As Long Dim dblData() As Double Dim lngDeviceScanBacklog As Long Dim lngLJMScanBacklog As Long Dim streamOutBufferSize0 As Long On Error GoTo ErrorMessage output.Text = "LJM Library Version = " lngError = LJM_ReadLibraryConfigS("LJM_LIBRARY_VERSION", dblValue) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" output.Text = output.Text & Str$(dblValue) & vbCrLf & vbCrLf 'Open the first found device. lngError = LJM_OpenS("ANY", "ANY", "ANY", lngHandle) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" 'Stop any previous streams just in case. Ignore error code. LJM_eStreamStop (lngHandle) 'Display the device information from the handle displayHandleInfo (lngHandle) ' Start the stream immediately lngError = LJM_eWriteName(lngHandle, "STREAM_TRIGGER_INDEX", 0) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" ' Internal crystal for clock source lngError = LJM_eWriteName(lngHandle, "STREAM_CLOCK_SOURCE", 0) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" ' Set stream resolution index to 0 lngError = LJM_eWriteName(lngHandle, "STREAM_RESOLUTION_INDEX", 0) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" ' Set stream settling in microseconds lngError = LJM_eWriteName(lngHandle, "STREAM_SETTLING_US", 0) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" ' Set AIN_ALL_RANGE lngError = LJM_eWriteName(lngHandle, "AIN_ALL_RANGE", 0) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" ' Set AIN_ALL_NEGATIVE_CH lngError = LJM_eWriteName(lngHandle, "AIN_ALL_NEGATIVE_CH", LJM_GND) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" ' Set Stream Out 0 target to DAC0 lngError = LJM_eWriteName(lngHandle, "STREAM_OUT0_TARGET", 1000) 'DAC0 If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" streamOutBufferSize0 = 16384 lngError = LJM_eWriteName(lngHandle, "STREAM_OUT0_BUFFER_SIZE", streamOutBufferSize0) '256 samples in buffer If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" lngError = LJM_eWriteName(lngHandle, "STREAM_OUT0_ENABLE", 1) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" ' ' Load up DAC0 with values ' ScanRate = 50000 ScansPerRead = streamOutBufferSize0 / 4 theta = 0 sineFrequency = 10 sineAmplitude = 2 Dim sw As StopWatch Set sw = New StopWatch Dim elapsed As Double sw.start Call generate_sine(sineFrequency, ScanRate, sineAmplitude, theta, aValues, streamOutBufferSize0 / 4) elapsed = sw.ElapsedMilliseconds Debug.Print "generate_sine Code for " & Str(streamOutBufferSize0 / 4) & " took " & Str(elapsed) & " ms" lngError = LJM_eWriteNameArray(lngHandle, "STREAM_OUT0_BUFFER_F32", streamOutBufferSize0 / 4, aValues(0), ErrorAddress) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" lngError = LJM_eWriteName(lngHandle, "STREAM_OUT0_LOOP_SIZE", ScansPerRead) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" lngError = LJM_eWriteName(lngHandle, "STREAM_OUT0_SET_LOOP", 1) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" NumAddresses = 2 ReDim aScanList(NumAddresses - 1) aScanList(0) = 0 aScanList(1) = 4800 lngError = LJM_eStreamStart(lngHandle, ScansPerRead, NumAddresses, aScanList(0), ScanRate) If lngError <> LJME_NOERROR Then Erase aScanList Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" End If lngLoopCnt = 0 lngTotalLoops = 40 ReDim dblData(ScansPerRead * NumAddresses - 1) ' # of samples per LJM_eStreamRead is lngScansPerRead * lngNumAddrs Do While lngLoopCnt < lngTotalLoops sw.start lngError = LJM_eStreamRead(lngHandle, dblData(0), lngDeviceScanBacklog, lngLJMScanBacklog) Debug.Print " LJM_eStreamRead took " & Format(sw.ElapsedMilliseconds, "0.00") & " ms" If lngError <> 0 Then Err.Raise lngError + 50000 lngError = LJM_eReadName(lngHandle, "STREAM_OUT0_BUFFER_STATUS", dblValue) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" Debug.Print " LJM_eReadName took " & Format(sw.ElapsedMilliseconds, "0.00") & " ms" If (dblValue > 0) Then Call generate_sine(sineFrequency, ScanRate, sineAmplitude, theta, aValues, Int(dblValue)) Debug.Print " generate_sine took " & Format(sw.ElapsedMilliseconds, "0.00") & " ms" lngError = LJM_eWriteNameArray(lngHandle, "STREAM_OUT0_BUFFER_F32", dblValue, aValues(0), ErrorAddress) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" Debug.Print " LJM_eWriteNameArray took " & Format(sw.ElapsedMilliseconds, "0.00") & " ms" lngError = LJM_eWriteName(lngHandle, "STREAM_OUT0_SET_LOOP", 1) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" End If lngLoopCnt = lngLoopCnt + 1 Debug.Print Str(dblValue) & " samples " & Format(Str(dblValue * 1000 / ScanRate), "0.00") & " ms took " & Format(sw.ElapsedMilliseconds, "0.00") & " ms" DoEvents Loop LJM_eStreamStop (lngHandle) lngError = LJM_eWriteName(lngHandle, "DAC0", 0) If lngError <> LJME_NOERROR Then Err.Raise lngError + 50000 'Adding 50000 makes sure Labjack errors are recognized by VB as "user-defined" LJM_CloseAll Exit Sub ErrorMessage: If Err.Number > 50000 Then 'LabJack error If lngErrorAddress < 0 Then strErrorAddress = " at address " & Str$(lngErrorAddress) LJM_ErrorToString Err.Number - 50000, strError output.Text = output.Text & vbCrLf & "LJM Error #" & Str$(Err.Number - 50000) & " " & strError & strErrorAddress MsgBox "LJM Error #" & Str$(Err.Number - 50000) & " " & strError & strErrorAddress ElseIf Err.Number > 0 Then 'VB error output.Text = output.Text & vbCrLf & "VB error: Error #" & Str$(Err.Number) MsgBox "VB error: Error #" & Str$(Err.Number) End If LJM_CloseAll output.Text = output.Text & vbCrLf & vbCrLf & "Done" Exit Sub End Sub