We help IT Professionals succeed at work.

How to implement a sleep sub in a serial port

I need to implement or add a sleep sub routine in Ms Access between the commwrite & commread so that I'm sure that all the data received from the gadget is read and received into my database. Now below is my draft sub routine see how to improve it. The gadget writes all the invoices without problems see attached screen, the only issue here is receiving that why the suggestion for a sleep sub:

Dim strData  as string
Dim strDataComplete as string

        strDataComplete = ""
        Do While True ' Loop until serial port is read
            Sleep 1000
            lngStatus = CommRead(intPortID, strData, 100000) 'For 100000 is the max data length you want read, change to suite
            Sleep 1000
            If lngStatus = 0 Then
               'Keep polling to see if serial has data
            ElseIf lngStatus <> 0 Then
                strDataComplete =  strData 'Put data pulled from serial port here 
            End If
            If lngStatus = 0 And strDataComplete <> "" Then
                Exit Do    ' Exit loop when all data is read
            End If

Open in new window

Watch Question

John TsioumprisSoftware & Systems Engineer
Distinguished Expert 2019

Test your restores, not your backups...
Expert of the Year 2019
Top Expert 2016
Here's an example routine that you could add and use to pause for some number of seconds.

Sub Test()
    ' Pause 5 seconds
    Pause 5
End Sub

Public Sub Pause(NumberOfSeconds As Variant)
    On Error GoTo Err_Pause

    Dim PauseTime As Variant, start As Variant

    PauseTime = NumberOfSeconds
    start = Timer
    Do While Timer < start + PauseTime

    Exit Sub

    MsgBox Err.Number & " - " & Err.Description, vbCritical, "Pause()"
    Resume Exit_Pause

End Sub

Open in new window