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:
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)