Read data from Barcode Reader using VB.NET

ramana_mca03
ramana_mca03 used Ask the Experts™
on
I am developing windows application using VB.NET 2005

its simple application like

reading Barcodes using Barcode Reader
once i read barcode, the data is comming and displaying Textbox is working fine...
BUT
what i want to do i need to capture the Barcode data before comming to form for some validation purpose...


So how can we get Barcode data from Barcode Reader or Human Interface Device...

so which event will fire on form once data is comming to Form??


thanks
Ramana
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Fernando SotoRetired
Distinguished Expert 2017

Commented:
If you post the manufacture of the barcode reader and model number I will try to do some research on it to see how it can be done.

Author

Commented:
Hi FernandoSoto:

here is the link of my Barcode Reder using to read barcodes.


http://www.symbol.com/ls3408er

waiting for your answer...

thanks
Ramana
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
On the "Data Sheet" tab in the link you provided it has this section:

    Interfaces Supported:  RS232, Keyboard Wedge, Wand Emulation, Scanner Emulation, IBM 468X/469X, USB and Synapse

If you are using the "Keyboard Wedge" option then there isn't really a way to "query" the scanner as it simply sends the barcode as normal keyboard input.

If you hook up the scanner using the RS232 option then you can query it directly...but you have to write lower level software to do so.  You can also buy third party software to handle this for you:
http://www.taltech.com/products/interface.htm
http://www.taltech.com/products/bcwedge.html
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Ramana;

What interface are you using?

    RS-232 , USB , Scanner Emulation Interface

Fernando

Author

Commented:
Hi FernandoSoto:

I am using USB.
I plugged my Barcode Reader to one of my computer USB port..

thanks
Ramana
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
From the link I gave above:

USB Output

Bar code scanners that have a USB output can work one of two ways.
The first is that they can work exactly like a Keyboard Wedge scanner and the second is that they can work exactly like a RS232 serial output scanner.

When you purchase a bar code scanner that has a USB output, you normally will also need to specify which interface you want - either "USB Keyboard Wedge" or "USB RS232 serial port". (Some bar code scanners are only available with the USB Keyboard Wedge option



Which mode are you working ?...

Author

Commented:
Hi Idle mind:

i think i am using "USB RS232 serial port".
once i scan the barcode i am getting that data and displaying Textbox working fine...
but i dont want to dispaly that data in to directly my Textbox..
Before comming in to Textbox i need do some validation like whether that basr code is correct or not like that....

how can we get that data..??

thanks
Ramana
Fernando SotoRetired
Distinguished Expert 2017

Commented:
What code do you use to get the data into the text box?

Author

Commented:
Hi FernandoSoto:
I am not using any code to get data in to Textbox..
Its automatically process.the data is comming through the Barcode Scanner (or) Reader and putting in to our system...

The thing is
Once i use my Barcode Reader to use for read Barcodes its read that barcode and put it in to Textbox(Not only Textbox where the cursor blinks on the screen.Suppose if you open notepad in our system
and use the Barcode Scanner and press scan button the data is showing in to Notepad.What i am trying to say here the Scan data is displaying where the cursor blinks..)

So the data is comming properrly .but i need to Capture that data before dispalying in to textbox or other control...

how can we do...?
please help me..its very arjunt...


thanks
Ramana






Go here
http://www.symbol.com/category.php?category=31
and try to get their SDK

you will have to use their SDK to capture that data.

They should have an interrupt being sent back to the OS that you can handle in your app and get the data that way.

hope that helps
-smit.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You said:

    "Suppose if you open notepad in our system and use the Barcode Scanner and press scan button the data is showing in to Notepad."

That REALLY sounds like you are using the "USB Keyboard Wedge" mode...which requires no additional software to be installed...and doesn't allow you to do what you like.

Author

Commented:
Hi Idle Mind:
This is same device i am using for Scan:

http://www.symbol.com/ls3408er

i am not sure whether thats USB or Keyboard Wedge because this is the first time i am working on Barcode Readers...

Just i need capture the Scan data and  store in to Variable... thats it...

how can we do this...??

thanks
Ramana








try getting the value from the textbox based on the onchange event
else, use their sdk to handle the events/interrups from the barcode scanner

Author

Commented:
Hi ullfindsmit:

I tryied their sdk but i dont find any usefull tips for me...
can you provide any other sample code...please


thnaks
Ramana
i tried searching on their documents but was unsuccessfull, but try their support forums
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Ramana;

So it sounds like the scanner is acting like a keyboard input device. If that is the case then this should work. Set the Forms KeyPreview property to True. Then add a Forms KeyPress event handler as shown below. Each time a character is set to the TextBox with the focus the Forms KeyPress event gets it first. Then do what test you need on the character. If you do not want the character to go to the TextBox set the e.Handled property to True other wise leave it alone and let the handler goto completion.

    Private Sub Form1_KeyPress(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If e.KeyChar <> "Character to be tested" Or e.KeyChar <> "Character to be tested" Then
            ' Send the data to the TextBox that has focus
        Else
            ' Do what I need to with the character and do not send to the textbox
            e.Handled = True
        End If

    End Sub

See if that does what you need.

Let me know.
Fernando

Author

Commented:
Hi FernandoSoto:

Thanks for your time effort....
the above code is working good...
But problem is e.KeyChar is giving only first letter in my Barcode

suppose my barcode has 9 letters but e.Keychar is giving only first letter(B) of that barcode

Sample Barcode:
BCSPT0157

so how can we get the whole Barcode??

thanks
Ramana
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Can you post the code the way you placed it into your solution.

Author

Commented:
Private Sub frmHID_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If e.KeyChar <> "BCSPT0157" Or e.KeyChar <> "BCSPT0158" Then
            TextBox1.Text = e.KeyChar
              Else
             e.Handled = True
        End If

    End Sub
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Hi Ramana;

The Forms KeyPress event handler receives only one character at a time so you will need to do something like the following.

    ' Class level variables
    ' Holds the barcode while it is being read.
    Private Barcode As StringBuilder
    ' Holds the barcodes that you donot want in the text box
    Private InvalidBarcode As Hashtable
    ' Current status of the Forms KeyPress event mode
    Private Scanning As Boolean

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Create a StringBuilder to hold barcode being read in to the system
        Barcode = New StringBuilder()
        ' Create the hash table to hold barcodes you do not want to process
        InvalidBarcode = New Hashtable()
        ' Populate the hash table
        InvalidBarcode.Add("BCSPT0157", Nothing)
        InvalidBarcode.Add("BCSPT0158", Nothing)
        ' Set scan mode to not scanning
        Scanning = False

    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        ' If in scan mode process the characters from the scanner. This is needed because if we did not
        ' then other parts of the application would not get keyboard input. If not in scan mode just exit.
        If Scanning Then
            ' Place the character just read into the StringBuffer
            Barcode.Append(e.KeyChar)
            ' Check to see if we have read the complete barcode
            If Barcode.Length = 9 Then
                ' The complete barcode was read in. Check to see if the code we read in is one
                ' to be processed.
                If InvalidBarcode.Contains(Barcode.ToString()) Then
                    ' The bar code is one of the ones we do not want to process
                    TextBox1.Text = "Invalid Barcode"
                Else
                    ' Is a valid barcode so send it to the text box.
                    TextBox1.Text = Barcode.ToString()
                End If
                ' Clear the StringBuffer to get ready for the next barcode.
                Barcode.Length = 0
            End If
            ' When in this mode do not send the characters read to the text box.
            e.Handled = True
        End If

    End Sub


    Private Sub btnScanning_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnScanning.Click

        ' This button places the application in scan mode or regular mode.
        If btnScanning.Text = "Scan" Then
            Scanning = True
            btnScanning.Text = "Stop"
        Else
            Scanning = False
            btnScanning.Text = "Scan"
        End If
        ' Reset the StringBuffer for the next scan
        Barcode.Length = 0

    End Sub

Fernando

Author

Commented:
Hi FernandoSoto:
I really thanks for your  time effort to slove this problem

once i placed your code in to my form it's getting error
****  Private Barcode As StringBuilder

which namespace can i Import for String Builder?


Thanks
Ramana
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Yes as Idle_Mind stated the namespace is System.Text so add this line to the top of your code file

Imports System.Text

Author

Commented:
Hi FernandoSoto:

Now the code is working good...
but actually that is not the requirmnet...
I need to get that data directly in to my class
is it possible..?

here i am posting the code for
detecting Human Interface Device(HID) or Barcode Reader in to our system
so can we read data directly from the device....?

Public Shared Function GetList() As Dictionary(Of String, String)
        Dim controllerSearcher As New ManagementObjectSearcher("Select * from Win32_USBControllerDevice")
        Dim controllerResults As ManagementObjectCollection = controllerSearcher.Get()

        Dim list As New Dictionary(Of String, String)

        For Each controllerObject As ManagementObject In controllerResults

            Dim deviceName As String = controllerObject("Dependent").ToString.Replace(Chr(34), "")
            Dim device As String = deviceName.Substring(deviceName.IndexOf("=") + 1)

            Dim deviceSearcher As New ManagementObjectSearcher("Select * From Win32_PnPEntity Where DeviceID = '" & device & "'")
            Dim deviceResults As ManagementObjectCollection = deviceSearcher.Get()

            For Each deviceObject As ManagementObject In deviceResults

                Dim deviceID As String = deviceObject("DeviceID").ToString().ToUpper()
                Dim description As String = deviceObject("Description").ToString()

                list.Add(deviceID, description)
            Next deviceObject

        Next controllerObject
        Return list
    End Function



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mylist As Dictionary(Of String, String) = GetList()
        Dim I As Integer = 0
        For Each mykey As String In mylist.Keys
            If mylist.Item(mykey) = "USB Human Interface Device" Then
                MessageBox.Show("HID Found in your System.")
            End If
            I = I + 1
        Next

    End Sub


so how can we read data directly from Barcode Reader in to our class (bcz i am planning to implement a class for this Barcode Validation) ??

thanks
Ramana
Retired
Distinguished Expert 2017
Commented:
Hi Ramana;

To your question, "so can we read data directly from the device....?"

By using this code we are intercepting the characters sent from the scanner. This means that we need to instruct the form to process all key strokes. When the Form is processing the key strokes from the scanner anything typed into text boxes will not show up in the text boxes because the Forms KeyPress event handler will tell the system that the character has been processed and do not do anything with it. This also means that while scanning in info if you press any keys on the keyboard those characters will be inserted into the barcode giving a invalid barcode. This is the reason for the button, btnScanning, I placed in the code. If the buttons Text property has the word Scan then you are in normal mode and keys pressed on the keyboard will be displayed in the text box that has the focus. If the button has the word Stop you are in scanning bar codes from the scanner.

I made some small changes to the code to fit your needs. In Form1_KeyPress event I made comments to where you can call the validating event you will create.

    ' Class level variables
    ' Holds the barcode while it is being read.
    Private Barcode As StringBuilder
    ' Current status of the Forms KeyPress event mode
    Private Scanning As Boolean

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ' Create a StringBuilder to hold barcode being read in to the system
        Barcode = New StringBuilder()
        ' Set scan mode to not scanning
        Scanning = False

    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        ' If in scan mode process the characters from the scanner. This is needed
        ' because if we did not then other parts of the application would not get
        ' keyboard input. If not in scan mode just exit.
        If Scanning Then
            ' Place the character just read into the StringBuffer
            Barcode.Append(e.KeyChar)
            ' Check to see if we have read the complete barcode
            If Barcode.Length = 9 Then
                ' The complete barcode was read in.

                ' At this point the variable Barcode holds the 9 character code that
                ' was read in from the scanner. NOTE all scaned in barcodes must be
                ' the same size in length for this function to work correctly. If the
                ' length of the string sent in by the scanner is not 9 then make the
                ' correction in the above if statement.
                ' At this point you can pass the scaned code into a validating
                ' function by pass the scaned code as a string as follows
                ' Barcode.ToString()

                ' Clear the StringBuffer to get ready for the next barcode.
                Barcode.Length = 0
            End If
            ' When in this mode do not send the characters read to the text box.
            e.Handled = True
        End If

    End Sub


    Private Sub btnScanning_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnScanning.Click

        ' This button places the application in scan mode or regular mode.
        If btnScanning.Text = "Scan" Then
            Scanning = True
            btnScanning.Text = "Stop"
        Else
            Scanning = False
            btnScanning.Text = "Scan"
        End If
        ' Reset the StringBuffer for the next scan
        Barcode.Length = 0

    End Sub

Fernando
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
If you don't want to intercept each keystroke and "build" the barcode incrementally,  then you need to use a DIFFERENT mode (such as RS232) on your scanner...

Author

Commented:
Hi FernandoSoto:
I  understood your validation thing

but i need the data directly in to my class from Barcode Reader

like
Public Class BarcodeValidation

Dim myBarcode as String=BarcodeReder.Getdata()

End Class

so it is possible to get data directly from Reader....??

Thanks
Ramana

Author

Commented:
Hi Idle_Mind:

Yes your right.I dont want to intercept each keystroke and "build" the barcode incrementally...
How can we get data  from DIFFERENT mode (such as RS232) ???
can you provifde some sample code...it would really help for me....

Thanks
Ramana
Fernando SotoRetired
Distinguished Expert 2017

Commented:
For you to be able to do something like this:

Dim myBarcode as String=BarcodeReder.Getdata()

You will need to do something like what Idle_Mind stated and reposted here, "If you don't want to intercept each keystroke and "build" the barcode incrementally,  then you need to use a DIFFERENT mode (such as RS232) on your scanner..."

Or another suggestion from Idle_Mind, "If you hook up the scanner using the RS232 option then you can query it directly...but you have to write lower level software to do so.  You can also buy third party software to handle this for you:
http://www.taltech.com/products/interface.htm
http://www.taltech.com/products/bcwedge.html"
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
To change the mode on your scanner, you need to change the settings on the scanner itself.

My gut feeling, based on your programming experience level, is that you would be best off using some third party software to accomplish this task...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial