Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Get Value of USB Barcode

Posted on 2016-11-01
4
Medium Priority
?
43 Views
Last Modified: 2016-11-21
I am using USB barcode Scanner but I have one problem.
 Form1_Keypress event runs twice.
 So sanner reading value or my pressed key values are also display twice.
for example : I press "23456" then the result display  "2233445566".

What is problem?

 the source code is below.

 Please help me~~~~~
 =========================================================================
 namespace usb_scanner2
 {


     public partial class Form1 : Form
     {
         public delegate void BarcodeReceivedEventHandler(object sender, BarcodeReceivedEventArgs e);

         public class BarcodeReceivedEventArgs : EventArgs
         {

             private string _barcode;

             public BarcodeReceivedEventArgs(string Barcode)
             {

                 _barcode = Barcode;

             }

             public string Barcode { get { return _barcode; } }

         }

         public event BarcodeReceivedEventHandler BarcodeReceived;

         const char ScannerEndCharacter = '\r';

         private StringBuilder scannedCharacters;

         public Form1()
         {
             InitializeComponent();

             scannedCharacters = new StringBuilder();

             this.KeyPreview = true;
             this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);

             this.BarcodeReceived += new BarcodeReceivedEventHandler(Form1_BarcodeReceived);
         }

         void Form1_BarcodeReceived(object sender, BarcodeReceivedEventArgs e)
         {

             MessageBox.Show("Barcode scanned: " + e.Barcode);

         }

         protected virtual void OnBarcodeRecieved(BarcodeReceivedEventArgs e)
         {

             if (BarcodeReceived != null)

                 BarcodeReceived(this, e);

         }

         private void Form1_KeyPress(object sender, KeyPressEventArgs e)
         {
                 if (e.KeyChar == 13)    //ScannerEndCharacter)
                 {
                     e.Handled = true;
                     
                     OnBarcodeRecieved(new BarcodeReceivedEventArgs(scannedCharacters.ToString()));
                     scannedCharacters.Remove(0, scannedCharacters.Length);
                 }

                 else
                 {
                     Debug.Print(e.KeyChar.ToString());
                     scannedCharacters.Append(e.KeyChar);
                     e.Handled = false;
                 }
         }
     }
 }
0
Comment
Question by:Antonio kim
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 14

Assisted Solution

by:frankhelk
frankhelk earned 2000 total points (awarded by participants)
ID: 41869036
In private void Form1_KeyPress(object sender, KeyPressEventArgs e), when you get a CR, you set e.Handled true, but on any other char you set it false, which leaves the event open for other handlers ... the latter might be the problem. Each character is another keypress, so each incoming character is to be handled separately.

Try to set the e.Handled to true behind the if block (and remove the other two instances of setting e.Handled, i.e.
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
         {
                 if (e.KeyChar == 13)    //ScannerEndCharacter)
                 {
                     OnBarcodeRecieved(new BarcodeReceivedEventArgs(scannedCharacters.ToString()));
                     scannedCharacters.Remove(0, scannedCharacters.Length);
                 }
                 else
                 {
                     Debug.Print(e.KeyChar.ToString());
                     scannedCharacters.Append(e.KeyChar);
                 }
                 e.Handled = true;
         }

Open in new window


BTW: Try the code block feature, it makes code much more readable ...
0
 

Author Comment

by:Antonio kim
ID: 41869113
I changed source code as your advice. But the result is same. could you check again?
==========================================================================
        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
                if (e.KeyChar == 13)    //ScannerEndCharacter
                {
                    e.Handled = true;
                   
                    OnBarcodeRecieved(new BarcodeReceivedEventArgs(scannedCharacters.ToString()));
                    scannedCharacters.Remove(0, scannedCharacters.Length);
                }
                else
                {
                    Debug.Print(e.KeyChar.ToString());
                    scannedCharacters.Append(e.KeyChar);
                    e.Handled = true;
                }
        }
0
 
LVL 14

Accepted Solution

by:
frankhelk earned 2000 total points (awarded by participants)
ID: 41870002
Hmmm - I'm a bit puzzled ... since your scanner simulates keystrokes, have you tried to use it with i.e. notepad ? what's dropping in then ?

I suspect the scanner driver (or sth similar on the way of the data) to cause the event fired twice on each character. In that case I'd just let it be and on finishing the string after the CR, I'd just strip each second character out of it. But be aware of the second CR in that case ;-)
0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41895642
Comments are only from me, and I deem the solution sufficient.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question