C# 使用 USB 条码扫描器读取条码,同时忽略键盘数据输入,而扫描器产品 ID 和供应商 ID 未知
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/615036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading a barcode using a USB barcode scanner along with ignoring keyboard data input while scanner product id and vendor id are not known
提问by Amar Patel
Is there a way to read from a USB barcode reader while ignoring the keyboard and not knowing the PID or VID of the USB scanner? I know that there is a way of differentiating between USB scanner input and keyboard input by using the VID and or PID of the USB scanner; this was done using code from http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/But is there another solution to differentiate between keyboard and USB scanner without putting the scanner's VID or PID in a configuration file (or source code)? The reason for not wanting to put various VIDs or PIDs in a configuration file is that, the application being developed will be deployed on numerous laptops and have arbitrary types of scanners attached to them.
有没有办法在忽略键盘并且不知道 USB 扫描仪的 PID 或 VID 的情况下从 USB 条码阅读器读取?我知道有一种方法可以通过使用 USB 扫描仪的 VID 和/或 PID 来区分 USB 扫描仪输入和键盘输入;这是使用来自http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/ 的代码完成的 但是是否有另一种解决方案来区分键盘和 USB 扫描仪不将扫描仪的 VID 或 PID 放在配置文件(或源代码)中?不想将各种 VID 或 PID 放在配置文件中的原因是,正在开发的应用程序将部署在众多笔记本电脑上,并附有任意类型的扫描仪。
Also, I don't want to configure the scanner's with a starting and or ending sequence that would be outputted, since the scanner is being used by other software on the same machine as well and I don't want to have to change the code on the other software. I don't want to program the barcode reader to be in serial mode either for the same reason mentioned previously.
此外,我不想使用将输出的开始和/或结束序列配置扫描仪,因为同一台机器上的其他软件也在使用扫描仪,我不想更改代码在其他软件上。出于前面提到的相同原因,我不想将条形码阅读器编程为串行模式。
采纳答案by GvS
There is a way to differentiate between keyboard and USB barcode reader
有一种方法可以区分键盘和 USB 条码阅读器
You can depend on these facts:
您可以依赖以下事实:
- the code scanned by barcode reader in minmum 4 characters
- the code scanned by barcode reader ends with RETURN "ENTER"
- it take less than 50 mseconds to scan the hole barcode
- 条码阅读器扫描的代码,最少 4 个字符
- 条码阅读器扫描的代码以 RETURN "ENTER" 结尾
- 扫描孔条码不到50毫秒
This is a simple form using VS2005 VB contains:
这是一个使用VS2005 VB的简单表格,包含:
- textbox1
- textbox2
- textbox3
- Button1
- Timer1 "the time interval set to 50"ms"
- 文本框1
- 文本框2
- 文本框3
- 按钮 1
- Timer1 "将时间间隔设置为 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
回答by GvS
There is a another question about barcodes here, the link will send you to an answer that uses the barcode via a serial port. Maybe that's a solution for you?
有一个关于一个条形码另外一个问题在这里,链接会送你到通过串口使用条形码的答案。也许这对你来说是一个解决方案?
IMHO: The most easy solution will be accepting the input from the keyboard.
恕我直言:最简单的解决方案是接受来自键盘的输入。
回答by James Conigliaro
Perhaps this is an oversimplified solution, but could you capture the key presss event and simply prevent entry via keyboard?
也许这是一个过于简单的解决方案,但是您能否捕获按键事件并简单地防止通过键盘输入?
回答by bernhardrusch
Well, I am using a solution pretty like the one from Ehab - I just cleaned up the code a little bit for my application. I am using a custom class for my edit controls (it is doing some other things too) - but these are the important parts for this:#
好吧,我使用的解决方案与 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;
}
}
}
}