views:

2947

answers:

5

Is there a way to read from a USB barcode reader while ignoring the keyboard and not knowing the PID or VID of the USB scanner? I know that there is a way of differentiating between USB scanner input and keyboard input by using the VID and or PID of the USB scanner; this was done using code from http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/ But is there another solution to differentiate between keyboard and USB scanner without putting the scanner's VID or PID in a configuration file (or source code)? The reason for not wanting to put various VIDs or PIDs in a configuration file is that, the application being developed will be deployed on numerous laptops and have arbitrary types of scanners attached to them.

Also, I don't want to configure the scanner's with a starting and or ending sequence that would be outputted, since the scanner is being used by other software on the same machine as well and I don't want to have to change the code on the other software. I don't want to program the barcode reader to be in serial mode either for the same reason mentioned previously.

A: 

There is a another question about barcodes here, the link will send you to an answer that uses the barcode via a serial port. Maybe that's a solution for you?

IMHO: The most easy solution will be accepting the input from the keyboard.

GvS
Also, I don't want to configure the scanner to be in serial mode, since the scanner is being used by other software on the same machine as well and I don't want to have to change the code on the other software.
Amar Patel
+3  A: 

Hello guys there is a way how to differenciate between keboard and USB barcode reader u can depend on these facts

  1. the code scanned by barcode reader in minmum 4 characters
  2. the code scanned by barcode reader ends with RETURN "ENTER"
  3. it take less than 50 mseconds to scan the hole barcode

so this is a simple form using VS2005 VB contains

  1. textbox1
  2. textbox2
  3. textbox3
  4. Button1
  5. Timer1 "the time interval set to 50"ms" "

Public Class Form1

Dim BarcodeStr As String = ""
Dim IsBarcodeTaken As Boolean = False
Dim Str As String = ""
Dim str3 As String = ""


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

    If Timer1.Enabled = False Then
        Str = TextBox1.Text
        str3 = TextBox3.Text
    End If

End Sub

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
    If Timer1.Enabled = False Then
        Timer1.Enabled = True
    End If


    BarcodeStr = BarcodeStr & e.KeyChar
    If Asc(e.KeyChar) = 13 And Len(BarcodeStr) >= 4 Then
        IsBarcodeTaken = True
        TextBox2.Text = BarcodeStr


    End If

End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    If IsBarcodeTaken = True Then
        TextBox1.Text = Str
        TextBox1.Select(Len(TextBox1.Text), 0)
        Str = ""

        TextBox3.Text = str3
        TextBox3.Select(Len(TextBox3.Text), 0)
        str3 = ""
    End If

End Sub


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    BarcodeStr = ""
    IsBarcodeTaken = False
    Timer1.Enabled = False
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox2.Text = ""

End Sub

End Class

hope it helps Ehab IT Supervisor LIBYA-ZWARA [email protected]

Since I don't have enough rep to edit other people's posts yet, just let me translate that for you: "Assume the scanner will enter at least four characters, end with a ENTER keypress, and take under 50ms to do this." It sounds like a good idea to me, but maybe you should make the time 100ms instead.
MiffTheFox
A: 

Perhaps this is an oversimplified solution, but could you capture the key presss event and simply prevent entry via keyboard?

James Conigliaro
A: 

tanks , that was very nice man.... if U made this source code more optimal , PLZ make me aware of that.

amir
if you are talking about Ehab's version - I've added my solution (inspired by his)
bernhardrusch
A: 

Well, I am using a solution pretty like the one from Ehab - I just cleaned up the code a little bit for my application. I am using a custom class for my edit controls (it is doing some other things too) - but these are the important parts for this:#

    public class ScannerTextBox : TextBox
    {
        public bool BarcodeOnly { get; set; }

        Timer timer;

        private void InitializeComponent()
        {
            this.SuspendLayout();

            this.ResumeLayout(false);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (BarcodeOnly == true)
            {
                Text = "";
            }

            timer.Enabled = false;
        }


        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            if (BarcodeOnly == true)
            {
                if (timer == null)
                {
                    timer = new Timer();
                    timer.Interval = 200;
                    timer.Tick += new EventHandler(timer_Tick);
                    timer.Enabled = false;
                }
                timer.Enabled = true;
            }

            if (e.KeyChar == '\r')
            {
                if (BarcodeOnly == true && timer != null)
                {
                    timer.Enabled = false;
                }
            }
        }
    }
bernhardrusch