Solved

Get Value of USB Barcode

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When I recently replaced my image transfer kit on my office HP color laserjet 5550dn printer, I had a slight problem.  The left bracket that holds the transfer kit got stuck in the upright locked position instead of being at a 45 degree angle facing…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

730 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