如何在C#中使用条形码阅读器 [英] How do I use a barcode Reader in C#

查看:65
本文介绍了如何在C#中使用条形码阅读器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用c#从条形码读取器设备读取值?

How to read a value from a barcode reader device using c#?

推荐答案

对于大多数条形码读取器而言,就像按键盘一样.因此,创建一个文本框,将光标置于该文本框内,然后使用条形码阅读器进行扫描.它应该发送键和一个换行符.
For most barcode readers, it''s just like pressing the keyboard. Hence, create a textbox, position the cursor inside the textbox and scan with the barcode reader. It should send keys and one newline character.


另一种解决方案是在窗体的构造函数中添加MessageFilter来拦截所有键盘消息.这样,您不必输入文本字段,扫描仪就可以独立使用.

Application.AddMessageFilter(new BarcodeMessageFilter());

BarcodeMessageFilter中,您需要实现IMessageFilterbool PreFilterMessage(ref Message m)来捕获所有char消息(m.Msg == 0x0102).

如果将扫描仪设置为具有特殊的起始字节(可以是不可打印的字符),则可以使用此字节来查找是否以下字节/字符是从条形码扫描仪发送的.可以通过在扫描仪中设置的停止字节来检测结尾.

您还应该使用超时来从扫描器读取字节(例如50毫秒),以便您知道何时以某种方式中止了传输.如果没有超时,则消息过滤器将捕获所有后续的char消息,如果停止字节以某种方式丢失.

现在,您可以将以下所有字节读取到缓冲区中,并触发一个事件,该事件无需使用文本字段即可为您提供完整的字符串.
Another solution would be adding a MessageFilter in the contructor of your form to intercept all keyboard messages. This way you dont have to enter a textfield and the scanner can be used independently.

Application.AddMessageFilter(new BarcodeMessageFilter());

In BarcodeMessageFilter you need to implement bool PreFilterMessage(ref Message m) of IMessageFilter to catch all char messages (m.Msg == 0x0102).

If you set your scanner to have a special start byte (could be a non printable char), you can use this byte to find out if the following bytes/chars were sent from a barcode scanner. The end can be detected by the stop byte that is set in the scanner.

You should also use a timeout for reading bytes from the scanner (e.g. 50ms), so that you know when the transmission was somehow aborted. Without a timeout the message filter would catch all following char messages, if the stop byte gets somehow lost.

Now you can read all following bytes into a buffer and trigger an event that gives you the complete string without using a textfield.


我同意答案2. .这就是我的工作区域中与读者合作的方式. ..当设备成功识别条形码时,它将通过键盘内嵌电缆将字符发送到PC,从而模仿输入条形码内容的人.

您还可以使用的条形码通常需要包裹在控制字符(例如星号)中的内容,以指示设备也发送回车键. . ...但这不是必需的.
I concur with Answer #2. . .that''s how things work with the readers in my work area. ..when the device successfully recognizes a barcode it sends the characters to the PC via an inline-with-the-keyboard cable, thereby mimicking a person typing the contents of the barcode.

What you also have at play is the barcode typically needing the contents wrapped in a control character (asterisk for instance) that instructs the device to send the enter key too. . .. .but this is not required.


这篇关于如何在C#中使用条形码阅读器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆