troubleshooting Question

Help to insert the CommFlush function in VBA serial port

Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.ScFlag for Zambia asked on
ProgrammingMicrosoft AccessVBA
1 Comment1 Solution28 ViewsLast Modified:
My final piece of VBA code in serial port programming is now to insert the CommFlush , this function I think is needed to ensure that all buffers remain clean once it is called. My question is where should I insert this function below in my main Ms Access VBA code:

lngStatus = CommFlush(intPortID)

Open in new window



intPortID = Forms!frmLogin!txtFinComPort.value
    ' Initialize Communications
    lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), "baud=115200 parity=N data=8 stop=1")
    If lngStatus <> 0 Then
        ' Handle error.
        lngStatus = CommGetError(strError)
        MsgBox "COM Error: " & strError
        Application.Quit
    End If
    
    ' Set modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, True)
    lngStatus = CommSetLine(intPortID, LINE_DTR, True)
    
    ' Write data to serial port.
    ' Build data packet to transmit (passing command code, and data to package)
    strData = BuildData(JsonConverter.ConvertToJson(transaction, Whitespace:=3))

    ' Send the data packet and check for error
    lngStatus = CommWrite(intPortID, strData)
    If lngStatus <> Len(strData) Then
        ' Handle error.
        On Error Resume Next
    End If
   
   
   
   Pause (5)
   If lngStatus < 0 Then
   'Handle error
   lngStatus = CommGetError(strError)
   MsgBox "COM Error: " & strError
   End If
       
    ' Read maximum of 2034 bytes from serial port.
    lngStatus = CommRead(intPortID, strData, 2036)
    If lngStatus <> Len(strData) Then
    'Handle error
    On Error Resume Next
    ElseIf lngStatus < 0 Then
        ' Handle error.
     On Error Resume Next
    End If
    
    Set rs = db.OpenRecordset("tblEfdReceipts", dbOpenDynaset, dbSeeChanges)
       
'Processing data from the string above
    Set db = CurrentDb
        
    Set json = JsonConverter.ParseJson(Chr(91) & Mid(strData, 8) & Chr(34) & "}" & Chr(93))
     
    'Process data.
    Z = 1
    For Each Details In json
        rs.AddNew
        rs![EsDTime] = CDate(Format$(Details("ESDTime"), "00/00/00 00:00:00"))
        rs![TerminalID] = Details("TerminalID")
        rs![InvoiceCode] = Details("InvoiceCode")
        rs![InvoiceNumber] = Details("InvoiceNumber")
        rs![FiscalCode] = Details("FiscalCode")
        rs![INVID] = Me.InvoiceID
        rs.Update
        Z = 1 + 1
    Next
   
    rs.Close
    Set rs = Nothing
    Set db = Nothing
    Set json = Nothing
    Set transaction = Nothing
    Set transactions = Nothing
    Set item = Nothing
    Set items = Nothing
    Set Tax = Nothing
    Set fld = Nothing
    Set root = Nothing
    ' Reset modem control lines.
    lngStatus = CommSetLine(intPortID, LINE_RTS, False)
    lngStatus = CommSetLine(intPortID, LINE_DTR, False)
           
    ' Close communications.
    Call CommClose(intPortID)

Open in new window

ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros