Comm32 Logo
Home Button  Buy Button 

Topic:   OnComm Event - Read Input Buffer

By: Joe DaviesPosted on: Jan 25 2019 at 01:35:56 PM
Hi there can you check my code and let me know if it is the correct way to read in the entire input from a barcode scanner device connected to comm port 10, I want to read the 6 digit number in one go , not number by number until the input buffer is empty.

Dim WithEvents SComm1 As SCommLib.SComm

Sub OpenPort()
Set SComm1 = CreateObject("SCommLib.SComm")
If Not SComm1.PortOpen Then
SComm1.CommPort = 10
SComm1.Settings = "9600,n,8,1"
SComm1.SThreshold = 1
SComm1.PortOpen = True
End If
End Sub

Private Sub SComm1_OnComm()
Dim strBarcode As String
strBarcode = SComm1.Read
strBarcode = Trim$(strBarcode)
debug.print strBarcode
End Sub


By: GuestPosted on: Jan 29 2019 at 10:17:04 AM
The problem with RS232 is that it is slow - from the computer processor's point of view it is painfully slow.

So, when you respond to the OnComm event it's likely that only one or two characters have arrived already and if you read the receiv buffer you'll just get those characters.

In theory you need to hang round waiting for the rest of the data. But hanging round is not good code.

You need to create a string at the top of your module - outside of any function. lets call that sting sBuffer

When you respond to the OnComm event do something like

sBuffer = sBuffer & SComm1.Read

Then look at sBuffer.
If sBuffer contains whole barcode packet then handle it and clear sBuffer and exit.
If sBuffer does NOT contain your whole barcode message then do not clear sBuffer. Just exit the ONComm event. You'll soon get another oncomm event and you just keep responding to OnComm and adding rx data to sBuffer till you get the whole packet.

Reply - add a comment to this topic.

You may enter letters, numbers and standard punctuation only. HTML and other scripts/tags will be rejected.

Topic:- OnComm Event - Read Input Buffer

Enter the numbers.

Your name here is optional