AnsweredAssumed Answered

VB6 Timeout When Polling

Question asked by opticyclic on Jan 9, 2007
Latest reply on Jan 9, 2007 by Dr_joel
I have posted some VB6 code below that uses the NI GPIB modules.
My problem appears when a sweep on the PNA takes too long.
e.g. with low IF Bandwidth and high Averaging.

The sub wait_poll takes a timeout value as a parameter and polls until it is clear or the timeout is reached.
I suppose I could make the message box a question i.e. Continue Polling Yes/No and restart the timer.
However, I get the feeling that this isn't the right way to do it.
Sending 10s of 1000s of commands over the bus doesn't seem right.
Shouldn't I just be able to send the OPC command then sit there until it arrives?

I suppose the other thing with both of these methods is:
What happens if there is an undetected error and the OPC or poll is never completed?
Your program is stuck in an infinite loop.

Is there best practice for this?

Sub ana_writewait(buf As String, fail As Boolean)
    'Like ana_write, but waits until the instruction is done.

    Call ana_write("*CLS", fail)          'Clear status bytes
    Call wait_poll(True, 15)        'Check that serial poll is clear
    Call ana_write(buf & ";*OPC", fail)
    Call wait_poll(False, 120)       'Wait (max 120s) for SRQ (Service Request)
End Sub

Sub ana_write(buf As String, fail As Boolean)
    'PNA MUST have a ; at end of commands
    'Hopefully will be correct in code but put a check here anyway
    If Right&#40;buf, 1&#41; <> ";" Then
        buf = buf & ";"
    End If

    fail = False

    Call ibwrt&#40;ana_devicecode, buf&#41;
    If &#40;ibsta And EERR&#41; Then
        fail = True
    End If
End Sub

Private Sub wait_poll&#40;clear As Boolean, timeout As Long&#41;
    'Loops until the poll is clear if "clear"=true.
    'Loops until not clear if "clear"=false.
    'Displays a message if the timeout &#40;seconds&#41; is exceeded.
    Dim poll As Integer
    Dim tim As Long
    Dim delay_time As Long
    Dim expect As Integer

    If clear = True Then
        expect = 0

        tim = Timer
        Do
            Call ibrsp&#40;ana_devicecode, poll&#41;
            If poll = 0 And 0 = expect Then
                Exit Sub
            End If
            DoEvents
            delay_time = CDbl&#40;Timer&#41; - CDbl&#40;tim&#41;
            'Test for crossing midnight boundary
            If delay_time < 0 Then delay_time = delay_time + 86400
            If delay_time >= timeout Then
                MsgBox "GPIB Timeout encountered during VNA Polling", vbExclamation, progid
                Exit Sub
            End If
        Loop
    Else
        expect = 64

        tim = Timer
        Do
            Call ibrsp&#40;ana_devicecode, poll&#41;
            If poll And 64 = expect Then
                Exit Sub
            End If
            DoEvents
            delay_time = CDbl&#40;Timer&#41; - CDbl&#40;tim&#41;
            'Test for crossing midnight boundary
            If delay_time < 0 Then delay_time = delay_time + 86400
            If delay_time >= timeout Then
                MsgBox "GPIB Timeout encountered during VNA Polling", vbExclamation, progid
                Exit Sub
            End If
        Loop
    End If
End Sub

Outcomes