Implement Barcode Scanner

Colin

New member
Joined
Jul 21, 2011
Messages
2
Programming Experience
Beginner
Hi,

I need to implement a USB barcode scanner into a VB project. With no code directed at the scanner, it works when a textbox is selected. I need it to put the numbers in the textbox whether the textbox is selected or not (as long as the form is open). I would also like it to save the numbers to a variable.

All help is appreciated.
 
Ok most barcode scanners act like a keyboard. Sure it fits nice in your hand, and has 2 trigger buttons.
The old ones used to be serial port ones. To get them working you would plug it in the serial port, open up hyperterminal and test to see what baud rate etc 8,n,1 you got.
Then read the port.

The newer ones behave just like a usb keyboard, point, click and it puts text out. try it - open notepad, make it have the focus and click the gun at the barcodes.
Now - this will not work when the form does not have focus.

Your goal -

"it works when a textbox is selected. I need it to put the numbers in the textbox whether the textbox is selected or not"

We have a problem here -
What you need to do is trigger event to move the characters one by one to the textbox when it does not have focus, but how do you know when to stop doing this?
What I did on my application when i started scanning was to enable = false all other text boxes on the form, move focus to the textbox then after the scan with the gun re-enable them.
That way only one text box was able to receive text on my form.

For the record this text box can be of the screen or hidden by a logo - the user does not need to see it - it just needs to be enabled. Somewhere.
 
Ok most barcode scanners act like a keyboard. Sure it fits nice in your hand, and has 2 trigger buttons.
The old ones used to be serial port ones. To get them working you would plug it in the serial port, open up hyperterminal and test to see what baud rate etc 8,n,1 you got.
Then read the port.

The newer ones behave just like a usb keyboard, point, click and it puts text out. try it - open notepad, make it have the focus and click the gun at the barcodes.
Now - this will not work when the form does not have focus.

Your goal -

"it works when a textbox is selected. I need it to put the numbers in the textbox whether the textbox is selected or not"

We have a problem here -
What you need to do is trigger event to move the characters one by one to the textbox when it does not have focus, but how do you know when to stop doing this?
What I did on my application when i started scanning was to enable = false all other text boxes on the form, move focus to the textbox then after the scan with the gun re-enable them.
That way only one text box was able to receive text on my form.

For the record this text box can be of the screen or hidden by a logo - the user does not need to see it - it just needs to be enabled. Somewhere.

There is only one textbox on the specific form. The form also contains 12 buttons for all numbers, backspace and enter. The idea is that the person can enter the number by the buttons or scan a barcode to enter the number.

Say the '5' button has focus, when I scan the barcode a '5' is added to the textbox. It is acting as a mouse if the textbox is not focused. This is my main problem.
 
A button does not receive text so you may want to consider a different way of going about that specific task. Also remember the Tag property of controls can be useful to store any object inside. These scanners literally act like a keyboard getting one to act like a mouse would require some sort of interaction with the Windows API.
 
Colin, that's what my program was like. I had a timer and ever 500 ms I set the focus to the text box. That way every time the user does a form command presses a button etc, takes the focus away, it is reset to the text box.
 
VB.NET:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'button 1
        TextBox1.Text = TextBox1.Text + CStr(1)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        TextBox1.Focus()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'button 2
        TextBox1.Text = TextBox1.Text + CStr(2)
    End Sub
End Class

That way when you activate the barcode scanner it will populate the text box.
 
Say the '5' button has focus, when I scan the barcode a '5' is added to the textbox. It is acting as a mouse if the textbox is not focused. This is my main problem.

When you press the gun trigger it sends out the bar code as text. If no text box is there it just 'goes nowhere' - windows discards it. After sending the text, it sends a carriage return. This whole procedure happens so fast you probably just see a mouse click like behaviour.

The gun trigger is a carriage return "/r", vbCr. Its acting as a keyboard, not a mouse. This is what causes your 'highlighted' button to be pressed. The solution - every 500 ms move the focus to the text box.
 
Last edited:
Back
Top