VBA, how can I clear the COM buffer

vbauser00

New Member
Joined
Sep 16, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I'm using the command
Open COM2:115200,N,8,1 for Binary Access Read Write As #1

Before reading using the Get #1, , byte_variable_name I would like to clear what's in the buffer. Excel crashes if I read too many bytes that doesn't exist in the buffer.

Would the Seek command help me? Seek requires a starting location, which I don't know without knowing the length of data in the buffer.

Any ideas would be helpful.
Thanks.
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Untested, maybe:
VBA Code:
    Dim buffer() As Byte

    Open "COM2:115200,N,8,1" For Binary Access Read As #1
    If LOF(1) > 0 Then
        ReDim buffer(0 To LOF(1) - 1)
        Get #1, , buffer
    End If
    Close #1
 
Upvote 0
Untested, maybe:
VBA Code:
    Dim buffer() As Byte

    Open "COM2:115200,N,8,1" For Binary Access Read As #1
    If LOF(1) > 0 Then
        ReDim buffer(0 To LOF(1) - 1)
        Get #1, , buffer
    End If
    Close #1


Thanks for your reply.
LOF(1) is always returning a '0' for me. Returns same value when the buffer has data vs no data.

I can close the port and re-open to clear the buffer. But trying to avoid that as a Band-Aid fix.
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top