Avatar of Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Hankwembo Christopher,FCCA,FZICA,CIA,MAAT,B.A.Sc
Flag for Zambia asked on

Help to insert the CommFlush function in VBA serial port

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
    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![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
        Z = 1 + 1
    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

ProgrammingMicrosoft AccessVBA

Avatar of undefined
Last Comment
Fabrice Lambert

8/22/2022 - Mon
Fabrice Lambert

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck