Topic: OnComm Event - Read Input Buffer
|By: Joe Davies||Posted 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
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
Private Sub SComm1_OnComm()
Dim strBarcode As String
strBarcode = SComm1.Read
strBarcode = Trim$(strBarcode)
|By: Guest||Posted 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.