使用 USB 条码扫描仪读取条码并忽略键盘数据输入,同时扫描仪产品 ID 和供应商 ID 未知

发布于 2024-07-14 18:15:40 字数 614 浏览 8 评论 0原文

有没有一种方法可以在忽略键盘且不知道 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 放入配置文件中的原因是,正在开发的应用程序将部署在许多笔记本电脑上,并附加任意类型的扫描仪。

另外,我不想用要输出的开始和/或结束序列来配置扫描仪,因为扫描仪也被同一台机器上的其他软件使用,并且我不想更改代码在其他软件上。 出于与前面提到的相同原因,我不想将条形码读取器编程为串行模式。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

空城之時有危險 2024-07-21 18:15:40

有一种方法可以区分键盘和 USB 条形码阅读器


从以下假设开始:

  1. 条形码阅读器扫描的代码至少有 4 个字符长
  2. 条形码阅读器扫描的代码以“ENTER”按键结束
  3. 所需时间少于 50 毫秒传输整个条形码

这个使用VS2005 VB的简单表格包含:

  1. TextBox1
  2. TextBox2
  3. TextBox3
  4. Button1
  5. Timer1“时间间隔设置为50ms”

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

There is a way to differentiate between keyboard and USB barcode reader


Starting with the following assumptions:

  1. the code scanned by barcode reader will be at least 4 characters long
  2. the code scanned by barcode reader ends with an "ENTER" keypress
  3. it take less than 50 ms to transmit the entire barcode

This simple form using VS2005 VB contains:

  1. TextBox1
  2. TextBox2
  3. TextBox3
  4. Button1
  5. Timer1 "the time interval set to 50ms"

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
初雪 2024-07-21 18:15:40

好吧,我使用的解决方案非常类似于 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;
            }
        }
    }
}

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:

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;
            }
        }
    }
}
白云不回头 2024-07-21 18:15:40

也许这是一个过于简单的解决方案,但是您可以捕获按键事件并简单地阻止通过键盘输入吗?

Perhaps this is an oversimplified solution, but could you capture the key press event and simply prevent entry via keyboard?

め七分饶幸 2024-07-21 18:15:40

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

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

There is 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文