Multifunction DAQ

cancel
Showing results for 
Search instead for 
Did you mean: 

VB Application hangs up during use of Digital Lines

Dear All,
I am using NI6009 USB Device and programming on VB 6.0. I am using a sample program from the examples given in the NI DAQMX base software. I need to display a textbox and increment a count everytime a signal is recieved as input. I have defined a text box for the same as well as a checkbox. The checkbox value changes to 0 and 1 everytime I pres a switch connected to give input to the device. However I am not able to display the Count value in the Textbox.
If I define a msgbox command to display the count, the count is properly displayed along with the textbox. But not so otherwise.

I am also not able to stop the program. It just hangs up and the application has to be closed forecefully ie CRTL+ALT+DEL. In some instances it crashes the OS itself I am using WinXP with SP2, VB6.0 with SP6
i am enclosing the code also.

'/*********************************************************************
'
' Visual Basic 6.0 Example Program:
'   ReadDigChan.frm
'
' Example Category:
'    DI
'
' Description:
'    This example demonstrates how to read values from one or more
'    digital input channels.
'
' Instructions for Running:
'    1. Select the digital lines on the DAQ device to be read.
'    Note: The array is sized for 8 lines, if using a different
'          amount of lines, change the number of elements in the
'          array to equal the number of lines chosen.
'
' Steps:
'    1. Create a task.
'    2. Create a Digital Input channel. Use one channel for all
'       lines.
'    3. Call the Start function to start the task.
'    4. Read the digital data.
'    5. Call the StopTask module to stop and clear the task.
'    6. Display an error if any.
'
' I/O Connections Overview:
'    Make sure your signal input terminals match the Lines I/O
'    Control. In this case wire your digital signals to the first
'    eight digital lines on your DAQ Device.
'
'*********************************************************************/

Option Explicit
    Private taskIsRunning As Boolean
    Private taskHandle As Long

Private Sub Command1_Click()
 DAQmxErrChk DAQmxStopTask(taskHandle)
   DAQmxErrChk DAQmxClearTask(taskHandle)
   startCommandButton.Enabled = True
   taskIsRunning = False
End Sub

Private Sub Command2_Click()
End
End Sub

Private Sub startCommandButton_Click()
    Dim sampsPerChanRead As Long
    Dim data As Long
    Dim numBytesPerSamp As Long
    Dim i As Integer
    Dim bufferSize As Long
    Dim channelList As String * 255
    Dim arraySizeInBytes As Long
    Dim readArray() As Byte
    Dim PCount, j, PCount1 As Integer
   
    PCount = 1
    j = 0
    On Error GoTo ErrorHandler
  
    startCommandButton.Enabled = False
    If ValidateControlValues Then
        startCommandButton.Enabled = True
        Exit Sub
    End If
   
    arraySizeInBytes = 100
    bufferSize = 1000
    '  Re-initialize a dynamic array to hold values read off the digital lines
    ReDim readArray(arraySizeInBytes)
   
    ' Create the DAQmx task.
'A:
    DAQmxErrChk DAQmxCreateTask(" ", taskHandle)
   
    'DAQmxErrChk (DAQmxCfgInputBuffer(taskHandle, 100000))
   
    ' Add a digital input channel to the task.
    DAQmxErrChk DAQmxCreateDIChan(taskHandle, digitalLinesTextBox.Text, "", DAQmx_Val_ChanForAllLines)
   
    ' Start the task running, and read from the digital lines.
     DAQmxErrChk DAQmxStartTask(taskHandle)
    
A:
   
     'DAQmxErrChk DAQmxReadDigitalLines(taskHandle, 1, 10#, _
        DAQmx_Val_GroupByChannel, readArray(0), arraySizeInBytes, _
        sampsPerChanRead, numBytesPerSamp, ByVal 0&)
   
    DAQmxErrChk (DAQmxReadDigitalU8(taskHandle, 1, 10#, True, readArray(0), arraySizeInBytes, sampsPerChanRead, ByVal 0&))
   
   
    'For i = 0 To 3
    '#########################
    If readArray(0) = 0 Then
                      
           
            'PCount1 = PCount - 1
            'MsgBox PCount1
            PCount = PCount + 1
           
        End If
   
    '#########################
   
      
        bitCheckBox(i) = readArray(i)
        Text1.Text = PCount
       
     'Next
   
    ' Display a window indicating the number of samples per channel read.
'    samplesPerChannelReadLabel.Caption = "Samples / Line Read = " & sampsPerChanRead
'    samplesPerChannelReadLabel.Visible = True
   
    ' All done!
    StopTask
      
    GoTo A
    Exit Sub
   
ErrorHandler:
    If taskIsRunning = True Then
        DAQmxStopTask taskHandle
        DAQmxClearTask taskHandle
        taskIsRunning = False
    End If
    startCommandButton.Enabled = True
    MsgBox Err.Number & " " & Err.Description, , "Error"
End Sub

Private Sub StopTask()
   'DAQmxErrChk DAQmxStopTask(taskHandle)
   'DAQmxErrChk DAQmxClearTask(taskHandle)
   'startCommandButton.Enabled = True
   'taskIsRunning = False
End Sub

Private Function ValidateControlValues()
    ValidateControlValues = 0
    If digitalLinesTextBox.Text = "" Then
        MsgBox "Please fill in all empty fields.", , "Error"
        ValidateControlValues = 1
    End If
End Function

Private Sub Form_Load()
    taskIsRunning = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If taskIsRunning = True Then
    StopTask
    End If
    End
End Sub

'********************************************************
0 Kudos
Message 1 of 5
(3,190 Views)
Here is the complete executable VB file. I am just modifying the example program and  trying to add a count of how many times the input is given.

Message Edited by perk_bud on 10-14-2006 09:19 AM

0 Kudos
Message 2 of 5
(3,178 Views)
Thanks for posting to the NI forums.  I suspect the cause of the hanging is the GoTo command in your code.  This command can allow for an infinite loop to occur and can hang up your system.  To place the read inside a loop I would recommend using a while loop instead of using a GoTo command.  As far as the issue with the text box, I suspect you need some delay inside the loop.  Without any delay the processor is not freed up to address other tasks including updating the text box.  Try placing a small delay in the loop and see if that allows the text box to update with the new values that you write to it.  Hopefully this helps.  Let me know if you have additional questions and good luck with your application.

Regards,

Neil S.
Applications Engineer
National Instruments
0 Kudos
Message 3 of 5
(3,152 Views)
Thanx a Bunch Neil S It is working fine now.Smiley Happy
0 Kudos
Message 4 of 5
(3,146 Views)

 

Dir sir !
please help me
i have 1 devide with 32 pin (input) how can i read all data of 32 pin.
example i can read this :Dev1/port0/line0:7
how can i read from Dev1/port0/line0:32 (read 32 pin)

im use vb6
Thank you so much.

0 Kudos
Message 5 of 5
(2,530 Views)