Solved

Get Value of USB Barcode

Posted on 2016-11-01
4
21 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
  • 3
4 Comments
 
LVL 13

Assisted Solution

by:frankhelk
frankhelk earned 500 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 13

Accepted Solution

by:
frankhelk earned 500 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 13

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now