distinguish between the scanner and the keyboard

2020-05-06 10:46发布

hey all. I have a barcode scanner connected to a PC that working with a c# program.now i want to distinguish between the scanner and the keyboard which one is sending data to my program. can everyone help me with a code or advise in c#?

somebody said this to me in another topic(but i can't do this yet): basically you can configure the scanner to send some characters that basically tell the computer "hi, it's me". When you see those characters in your input stream, you know the information is coming from the barcode scanner, not from something the user typed on the keyboard. Did you check the manual that came with your barcode scanner? It should have more information about this.

2条回答
Explosion°爆炸
2楼-- · 2020-05-06 11:03

The scan data will typically end with a carriage return or line feed character, called the suffix. You may be able to configure the scanner to include a prefix as well. That is what your friend was trying to tell you.

查看更多
淡お忘
3楼-- · 2020-05-06 11:08

See update, as at March 2019: https://stackoverflow.com/a/55411255/495455


If your application works with particular barcodes (eg all same char length or ones that can be matched with a RegEx) then you could possibly work it out by coding up a Robot Typing test. eg:

VB.Net:

Private sw As Stopwatch
Private Sub FirstCharacterEntered()
    sw.Start()
End Sub
Private Sub txt_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt.TextChanged
    If txt.length = 0 Then FirstCharacterEntered()
    If txt.Length = BarCodeSerialLength Or New RegularExpressions.Regex("your pattern").IsMatch(txt.Text) Then
        sw.Stop()
        If sw.ElapsedMilliseconds < TimeAHumanWouldTakeToType Then
            'Input is from the BarCode Scanner
        End If    
    End If
End Sub

C#:

private Stopwatch sw;
private void FirstCharacterEntered()
{
    sw.Start();
}
private void txt_TextChanged(System.Object sender, System.EventArgs e)
{
    if (txt.length == 0)
        FirstCharacterEntered();
    if (txt.Length == BarCodeSerialLength | new RegularExpressions.Regex("your pattern").IsMatch(txt.Text)) {
        sw.Stop();
        if (sw.ElapsedMilliseconds < TimeAHumanWouldTakeToType) {
            //Input is from the BarCode Scanner
        }
    }
}
查看更多
登录 后发表回答