Click here to Skip to main content
15,888,044 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when read barcode scanner by c# and recieve value of reading in variable
msg it reading scanned item as 3300002266/r on debug
but when reading in notepad or textbox it reading correct 30026
why this extra digits read and how to make it read correct as 30026
my code as below
my scanner reader data as below
Barcode read 2d for Items 

Barcode Device DataLogic Lite QW2100

MODEL QY2100

CLASS QY2120-BK

S/N G17079019


What I have tried:

C#
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        {
            TimeSpan elapsed = (DateTime.Now - _lastKeystroke);
            if (elapsed.TotalMilliseconds > 100)
                _barcode.Clear();

            // record keystroke & timestamp
            _barcode.Add(e.KeyChar);
            _lastKeystroke = DateTime.Now;
            //		e.KeyChar	52 '4'	char

            // process barcode
            if (e.KeyChar == 13 && _barcode.Count > 0)
            {


                string msg = new String(_barcode.ToArray());
                //  MessageBox.Show(msg);
                label1.Text = msg;
                _barcode.Clear();
            }
        }
Posted
Updated 10-Sep-18 16:07pm

1 solution

Well, I can tell you that your TimeSpan idea to determine if it's the barcode scanner or a human at the keyboard idea is complete crap. Windows is a shared system and your code may not be given control of the CPU in the time frame that your code is looking for, essentially killing your barcode even though the scanner is still "typing" it.

Most barcode scanners will let you configure them to prepend and append character codes to the barcode, essentially giving you a tag to look for to know that the next keystrokes are coming from the barcode scanner, up until the tag at the end of the barcode is sent.

As for the double characters, your not setting the Handled property of the event args to True to tell the control to NOT handle the keypress event. Essentially, your letting event key event happen twice, once in your own code and once by the control that has the focus.
 
Share this answer
 
Comments
ahmed_sa 10-Sep-18 23:00pm    
Thank you for reply
but how to prevent duplicate on digit from code can you tell me by details please help me
and can you give me link or more explain to your idea nay be if applicable i will implement it
Richard Deeming 11-Sep-18 12:25pm    
As Dave said, set the Handled property[^] to true to prevent the keys from being added twice.
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    ...
    e.Handled = true;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900