NI DAQ – VB 6 – Analog In and out

Academics, Web & Code No Comments

This is one of the pieces of code I wrote for National instruments – data acquisition device. Again… unformatted uncommented code. Sorry!!!

The need:

Some high end academic need. Basically a combination of analog inputs gives an analog out.

The Setup ( a lot of unedited text from my report):

The system consists of hardware inputs and outputs (analog) which are interfaced with the PC using the DAQ (NI USB 6008 module). Controlling the system is a software program written in Visual basic 6. The program interacts with a database(access) to read and write values.

The data acquisition system is a National Instruments USB-6008 that has 4 Analog inputs, 2 analog outputs and 12 digital I/O lines. The kit comes with NI-DAQmx driver software and LabVIEW Signal Express software. The system can be programmed using  ANSI C and visual basic 6 (VB 6). Ref. Appendix 1 for detailed specification sheet for the NI USB-6008 DAQ

The hardware consists of

  • Toggle Switch (DEV1/AI0)
  • Push buttons (DEV1/AI1, DEV1/AI2)

The buttons indicate operations (steps of a task)

  • LED lights in serial with resistor (DEV1/AO1, DEV1/AO0)

The lights indicate success or failure of task

The hardware is wired as follows:

NI DAQ

Wiring diagram

The HMI of the software integrates the following elements

1. Combo Box for giving inputs

2. Start button to start the process

3. Reset button to reset system and bring back system to start position

4. Status Box with label to read and display the current status of the system

5. Success and Failure Box to read and display the number of successes and failures of each task

6. Task box with Step 1, Step 2 and Step 3 boxes along with their success and failure indication boxes

The  program consists of the following basic steps

1. when the form is loaded the program interacts with the database and populates the combobox

2. When start button is pressed, it checks for input. If no input, it gives out an error message. If there is an input, it reads inputs and populates step 1, 2 and 3 content box corresponding to the given input. In addition, the reset button is activated and start button is deactivated

3. A message box appears which tells the user to do Step (sequentially from step 1- 3). After completion of the process, the message box is approved. In case a wrong process has been performed, an error message is displayed and failure light goes on. In addition, number of failure goes up by one. However, if a right process is performed the indication light turns green and the process repeats for the second step of the process

4. After successful completion of all the three steps, the success light goes on and success box is incremented by one.

5. When reset button is pressed the lights reset and program comes back to start position with start button activated and reset button deactivated.

Please refer appendix 2 for the program.

The database has one table called Input with the following columns

  1. Task
  2. Step 1
  3. Step 2
  4. Step 3

This task column is used to populate the list of the combo box. When start button is pressed, based on the given input, a row is selected and steps are loaded into the step content boxes.

The Code:

Option Explicit

Option Explicit
    Private Data As Double
    ' used to capture data from the input function
    Private taskHandle As Long
    Private taskHandle2 As Long
    'task handles to open a task to ineract with the NI USB-6008
    Private taskIsRunning As Boolean
    Private taskIsRunning2 As Boolean
    ' to check if the task is running
    Private ithasstarted As Boolean
    ' to check if input is given and start button is pressed
    Private Step1 As String
    Private Step2 As String
    Private Step3 As String
    ' Steps of a task
    Private Task As String
    ' Used to store input
    Dim ws As Workspace
    Dim mydb As Database
    Dim rs As Recordset
    ' to establish connection and work with a database
    Dim whichlight As String
    ' to store which light is burning
    Dim success As Integer
    Dim failure As Integer
    ' to increment success and failure

Private Sub Command1_Click()
Label1.Caption = success
Label2.Caption = failure
ithasstarted = True
Label6.Caption = "Process has started"
If Combo1.Text = "" Then
MsgBox ("please give an input")
ithasstarted = False
End If
If ithasstarted Then
Command2.Enabled = True
Command1.Enabled = False
Task = Combo1.Text
Set rs = mydb.OpenRecordset("Input")
Do While Not rs.EOF
If Task = rs.Fields("Task") Then
Step1 = rs.Fields("Step 1")
Step1Holder.Caption = rs.Fields("Step 1")
Step2 = rs.Fields("Step 2")
Step2Holder.Caption = rs.Fields("Step 2")
Step3 = rs.Fields("Step 3")
Step3Holder.Caption = rs.Fields("Step 3")
Exit Do
Else
rs.MoveNext
End If
Loop
rs.Close
Set rs = Nothing
If Task = "Task 1" Then
Label6.Caption = "Task 1 has started excution"
tell (Step1)
execute1 (Step1)
If execute1(Step1) = True Then
tell (Step2)
execute2 (Step2)
If execute2(Step2) = True Then
tell (Step3)
execute3 (Step3)
If execute3(Step3) = True Then
LightUp ("Dev1/ao0")
Label6.Caption = "Task 1 has completed Successfully"
success = success + 1
Label1.Caption = success
End If
End If
End If
End If
If Task = "Task 2" Then
Label6.Caption = "Task 2 has started excution"
tell (Step1)
execute1 (Step1)
If execute1(Step1) = True Then
tell (Step2)
execute2 (Step2)
If execute2(Step2) = True Then
tell (Step3)
execute3 (Step3)
If execute3(Step3) = True Then
LightUp ("Dev1/ao0")
Label6.Caption = "Task 2 has completed Successfully"
success = success + 1
Label1.Caption = success
End If
End If
End If
End If
If Task = "Task 3" Then
Label6.Caption = "Task 3 has started excution"
tell (Step1)
execute1 (Step1)
If execute1(Step1) = True Then
tell (Step2)
execute2 (Step2)
If execute2(Step2) = True Then
tell (Step3)
execute3 (Step3)
If execute3(Step3) = True Then
LightUp ("Dev1/ao0")
Label6.Caption = "Task 3 has completed Successfully"
success = success + 1
Label1.Caption = success
End If
End If
End If
End If
End If
End Sub

Private Function tell(stp1)
Dim stp As String
stp = stp1
MsgBox ("Switch on " & stp)
End Function

Private Function execute1(stp1)
Dim stp As String
stp = stp1
Dim value1 As Integer
Label6.Caption = "Switch on " & stp
value1 = GetInput(stp)
If value1 >= 1 Then
execute1 = True
Shape1.FillColor = &HFF00&
Else
Label6.Caption = "Error. Please reset"
LightUp ("Dev1/ao1")
failure = failure + 1
Label2.Caption = failure
End If
End Function

Private Function execute2(stp2)
Dim stp As String
stp = stp2
Dim value1 As Integer
Label6.Caption = "Switch on " & stp
value1 = GetInput(stp)
If value1 >= 1 Then
execute2 = True
Shape2.FillColor = &HFF00&
Else
Label6.Caption = "Error. Please reset"
LightUp ("Dev1/ao1")
failure = failure + 1
Label2.Caption = failure
End If
End Function

Private Function execute3(stp3)
Dim stp As String
stp = stp3
Dim value1 As Integer
Label6.Caption = "Switch on " & stp
value1 = GetInput(stp)
If value1 >= 1 Then
execute3 = True
Shape3.FillColor = &HFF00&
Else
Label6.Caption = "Error. Please reset"
LightUp ("Dev1/ao1")
failure = failure + 1
Label2.Caption = failure
End If
End Function

Private Function GetInput(stp2)
    Dim value As Long
    Dim sampsPerChanRead As Long
    Dim Id As String
    Id = stp2
    On Error GoTo ErrorHandler
    '  Create the DAQmx task.
    DAQmxErrChk DAQmxCreateTask("", taskHandle)
    taskIsRunning = True
    '  Add an analog input channel to the task.
    DAQmxErrChk DAQmxCreateAIVoltageChan(taskHandle, stp2, "", _
                    DAQmx_Val_Cfg_Default, "0", "5", _
                    DAQmx_Val_VoltageUnits1_Volts, "")
    '  Read in one sample from the analog input channel.
    DAQmxErrChk DAQmxReadAnalogScalarF64(taskHandle, 10#, Data, ByVal 0&)
    '  Display the acquired sample on the user interface
    value = CStr(Data)
    GetInput = value
    '  Call the StopTask module to stop the DAQmx task.
    StopTask
    Exit Function
ErrorHandler:
    If taskIsRunning = True Then
        DAQmxStopTask taskHandle
        DAQmxClearTask taskHandle
        taskIsRunning = False
    End If
    MsgBox "Error:" & Err.Number & " " & Err.Description, , "Error"
End Function
Private Function LightUp(stp2)
    Dim errorCode As Long
    Dim errorString As String
    Dim numChannels As Long
    Dim Id As String
    Id = stp2
    whichlight = Id
    On Error GoTo ErrorHandler
    '  Create the DAQmx task.
    DAQmxErrChk DAQmxCreateTask("", taskHandle2)
    taskIsRunning2 = True
    '  Add an analog output channel to the task and check to see whether there is only ONE task.
    DAQmxErrChk DAQmxCreateAOVoltageChan(taskHandle2, Id, "", "0", "5", DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxGetTaskNumChans(taskHandle2, numChannels)
    If numChannels > 1 Then
        errorCode = -1
        errorString = "Please specify only one channel."
        Err.Raise errorCode, , errorString
    End If
    If taskIsRunning2 = True Then
    DAQmxErrChk DAQmxWriteAnalogScalarF64(taskHandle2, True, 10, 5, ByVal 0&)
    End If
Exit Function
ErrorHandler:
    If taskIsRunning = True Then
        DAQmxStopTask taskHandle2
        DAQmxClearTask taskHandle2
        taskIsRunning = False
    End If
    MsgBox "Error:" & Err.Number & " " & Err.Description, , "Error"
End Function

Private Function LightDown(stp2)
    Dim errorCode As Long
    Dim errorString As String
    Dim numChannels As Long
    Dim Id As String
    Id = stp2
    On Error GoTo ErrorHandler
    '  Create the DAQmx task.
    DAQmxErrChk DAQmxCreateTask("", taskHandle2)
    taskIsRunning2 = True
    '  Add an analog output channel to the task and check to see whether there is only ONE task.
    DAQmxErrChk DAQmxCreateAOVoltageChan(taskHandle2, Id, "", "0", "5", DAQmx_Val_VoltageUnits2_Volts, "")
    DAQmxErrChk DAQmxGetTaskNumChans(taskHandle2, numChannels)
    If numChannels > 1 Then
        errorCode = -1
        errorString = "Please specify only one channel."
        Err.Raise errorCode, , errorString
    End If
        If taskIsRunning2 = True Then
    DAQmxErrChk DAQmxWriteAnalogScalarF64(taskHandle2, True, 10, 0, ByVal 0&)
    End If
    Exit Function
ErrorHandler:
    If taskIsRunning = True Then
        DAQmxStopTask taskHandle2
        DAQmxClearTask taskHandle2
        taskIsRunning = False
    End If
    MsgBox "Error:" & Err.Number & " " & Err.Description, , "Error"
End Function

Private Sub StopTask()
    '  All done!
    DAQmxErrChk DAQmxStopTask(taskHandle)
    DAQmxErrChk DAQmxClearTask(taskHandle)
    taskIsRunning = False
End Sub
Private Sub StopTask2()
    '  All done!
    DAQmxErrChk DAQmxStopTask(taskHandle2)
    DAQmxErrChk DAQmxClearTask(taskHandle2)
    taskIsRunning2 = False
End Sub

Private Sub Command2_Click()
Command1.Enabled = True
LightDown (whichlight)
Shape1.FillColor = &H80FF&
Shape2.FillColor = &H80FF&
Shape3.FillColor = &H80FF&
Combo1.Refresh
ithasstarted = False
If ithasstarted = False Then
Command2.Enabled = False
End If
End Sub

Private Sub Form_Load()
    taskIsRunning = False
    Command2.Enabled = False
    Set ws = DBEngine.Workspaces(0)
    Set mydb = ws.OpenDatabase("C:\Users\Nikhil\Documents\Data1.mdb")
    Set rs = mydb.OpenRecordset("Input")
    Do While Not rs.EOF
    Combo1.AddItem rs.Fields("Task")
    rs.MoveNext
    Loop
   rs.Close
   Set rs = Nothing
End Sub

Private Sub Form_Unload(Cancel As Integer)
    '  Quit the program.
    If taskIsRunning = True Then
        StopTask
    End If
     If taskIsRunning2 = True Then
        StopTask2
    End If
    LightDown (whichlight)
    End
End Sub

Leave a Reply

© 2009, Nikhil Hullur | Entries RSS Comments RSS