Solved

Get Value of USB Barcode

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Printers have changed substantially in the last 30 or so years, not just in technical capabilities but in cost and usage as well.  Printers were originally used for interfacing with the operator, not necessarily for printing copy or pictures. In …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
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…

837 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