2009-03-05 32 views
11

有没有办法从USB条形码读取器读取而忽略键盘并且不知道PID或VID USB扫描仪?我知道有一种方法可以通过使用USB扫描仪的VID和/或PID来区分USB扫描仪输入和键盘输入;这是使用http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/ 代码完成的。但是,如果不将扫描仪的VID或PID放入配置文件(或源代码)中,是否还有另一种解决方案来区分键盘和USB扫描仪?不希望将各种VID或PID放入配置文件的原因是,正在开发的应用程序将部署在众多笔记本电脑上,并且随机附带有任意类型的扫描仪。使用USB条形码扫描器读取条形码以及忽略键盘数据输入,而扫描仪产品ID和供应商ID未知

此外,我不想配置扫描仪的输出开始和结束顺序,因为扫描仪正在被同一台机器上的其他软件使用,我也不想更改其他软件上的代码。我不希望将条形码阅读器编程为串行模式,原因可能与之前提到的相同。

回答

13

有键盘和USB条形码阅读器来区分的方式

你可以依靠这些事实:

  1. 通过条码阅读器在minmum 4个字符扫描码
  2. 通过扫描QR码条码阅读器以RETURN“ENTER”结尾
  3. 扫描孔条码需要不到50毫秒的时间

这是使用VS2005 VB一个简单的形式包含:

  1. TextBox1中
  2. TextBox2中
  3. textbox3
  4. Button1的
  5. 定时器1 “的时间间隔设置为50” MS”

Public Class Form1 

Dim BarcodeStr As String = "" 
Dim IsBarcodeTaken As Boolean = False 
Dim Str As String = "" 
Dim str3 As String = "" 


Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown 

    If Timer1.Enabled = False Then 
     Str = TextBox1.Text 
     str3 = TextBox3.Text 
    End If 

End Sub 

Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress 
    If Timer1.Enabled = False Then 
     Timer1.Enabled = True 
    End If 


    BarcodeStr = BarcodeStr & e.KeyChar 
    If Asc(e.KeyChar) = 13 And Len(BarcodeStr) >= 4 Then 
     IsBarcodeTaken = True 
     TextBox2.Text = BarcodeStr 


    End If 

End Sub 
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp 
    If IsBarcodeTaken = True Then 
     TextBox1.Text = Str 
     TextBox1.Select(Len(TextBox1.Text), 0) 
     Str = "" 

     TextBox3.Text = str3 
     TextBox3.Select(Len(TextBox3.Text), 0) 
     str3 = "" 
    End If 

End Sub 


Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
    BarcodeStr = "" 
    IsBarcodeTaken = False 
    Timer1.Enabled = False 
End Sub 


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    TextBox2.Text = "" 

End Sub 

End Class 
+0

由于我没有足够的代表来编辑其他人的帖子,请让我为你翻译:“假设扫描仪至少输入4个字符,以ENTER键结束,并采用50ms以下的操作。”这听起来对我来说是个好主意,但也许你应该让时间为100ms。 – MiffTheFox 2009-06-18 12:03:16

+0

恐怕,这是不正确的。您可以**通过使用[原始输入](https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536.aspx)告诉哪个设备生成输入。您不需要知道VID和PID,因为USB扫描仪具有指定的* UsagePage *和* Usage *条目。 – IInspectable 2017-01-09 16:21:00

0

还有一个关于条码here的问题,该链接会通过串口向您发送一个使用条形码的答案。也许这对你来说是一个解决方案?

恕我直言:最简单的解决方案将接受来自键盘的输入。

+0

此外,我不想将扫描器配置为串行模式,因为扫描仪正在被同一台机器上的其他软件使用,我不希望更改其他软件上的代码。 – 2009-03-05 14:52:09

0

也许这是一个过于简单的解决方案,但是你能捕捉到按键事件并简单地通过键盘禁止输入吗?

3

那么,我正在使用一个很像Ehab的解决方案 - 我只是为我的应用程序清理了一些代码。我正在使用我的编辑控件的自定义类(它也在做其他一些操作) - 但这些是这个的重要部分:#

public class ScannerTextBox : TextBox 
    { 
     public bool BarcodeOnly { get; set; } 

     Timer timer; 

     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 

      this.ResumeLayout(false); 
     } 

     void timer_Tick(object sender, EventArgs e) 
     { 
      if (BarcodeOnly == true) 
      { 
       Text = ""; 
      } 

      timer.Enabled = false; 
     } 


     protected override void OnKeyPress(KeyPressEventArgs e) 
     { 
      base.OnKeyPress(e); 

      if (BarcodeOnly == true) 
      { 
       if (timer == null) 
       { 
        timer = new Timer(); 
        timer.Interval = 200; 
        timer.Tick += new EventHandler(timer_Tick); 
        timer.Enabled = false; 
       } 
       timer.Enabled = true; 
      } 

      if (e.KeyChar == '\r') 
      { 
       if (BarcodeOnly == true && timer != null) 
       { 
        timer.Enabled = false; 
       } 
      } 
     } 
    } 
相关问题