Solved

Get Value of USB Barcode

Posted on 2016-11-01
4
24 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 14

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 14

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 14

Expert Comment

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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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 …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

914 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