是否有带有 .net api 的条形码扫描仪(硬件)可供我用来集成到我的应用程序中?

发布于 2024-08-23 14:24:11 字数 237 浏览 3 评论 0原文

据我了解,条形码扫描仪就像键盘一样,其作用也是如此。我需要的是一个具有某种 api(最好是 .net/c#)的扫描仪,我可以将其插入到我的应用程序中。是否有特定的扫描仪硬件供应商开发人员已经使用或当前正在使用,具有 .net api?或者我可以使用任意扫描仪并围绕它构建 api 或使用开源扫描仪之一?

所以我想做的一件事是能够从 USB 扫描仪获取值,而无需将光标放在特定的文本字段上。

谢谢

I understand the barcode scanner is like a keyboard and acts as such. What I need is a scanner that has some sort of api (.net/c# preferably) that i can plug into my app. Is there a particular scanner hardware vendor devs have used or are currently using, that has a .net api? or can I use any arbitrary scanner and build an api around it or use one of the open source ones?

So 1 thing I would like to do, is be able to get the value off the usb scanner without placing the cursor on the particular text field.

Thanks

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

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

发布评论

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

评论(3

卷耳 2024-08-30 14:24:11

嗯,这取决于。如果您有 USB 条形码扫描仪,它的作用就像键盘一样,将扫描的文本直接读取到聚焦控件中。如果您有串行条形码扫描仪,则需要使用 .NET 中的 SerialPort 类。

如果您需要在填充文本框等之前解析数据,那么最好的选择是使用串行扫描仪。

这是一个可供使用的代码示例

public partial class Form1 : Form
{
     SerialPort _serialPort;

     // delegate is used to write to a UI control from a non-UI thread
     private delegate void SetTextDeleg(string text);

     private void Form1_Load(object sender, EventArgs e)
     {
           // all of the options for a serial device
           // can be sent through the constructor of the SerialPort class
           // PortName = "COM1", Baud Rate = 19200, Parity = None, 
           // Data Bits = 8, Stop Bits = One, Handshake = None
           _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
           _serialPort.Handshake = Handshake.None;
           _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
           _serialPort.ReadTimeout = 500;
           _serialPort.WriteTimeout = 500;
           _serialPort.Open();
     }

     private void btnStart_Click(object sender, EventArgs e)
     {
           // Makes sure serial port is open before trying to write
           try
           {
                if(!_serialPort.IsOpen)
                     _serialPort.Open();

                _serialPort.Write("SI\r\n");
           }
           catch (Exception ex)
           {
                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
           }
     }

     void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
     {
          Thread.Sleep(500);
          string data = _serialPort.ReadLine();
          this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
     }

     private void si_DataReceived(string data)
     {
           textBox1.Text = data.Trim();
     }
}

Well, it depends. If you have a USB barcode scanner it acts exactly like a keyboard, reading the scanned text directly into a focused control. If you have a serial barcode scanner, you will need to use the SerialPort class in .NET.

If you are going to need to parse the data before filling a textbox, etc. then best bet would be to use a Serial Scanner.

Here's a code sample for use:

public partial class Form1 : Form
{
     SerialPort _serialPort;

     // delegate is used to write to a UI control from a non-UI thread
     private delegate void SetTextDeleg(string text);

     private void Form1_Load(object sender, EventArgs e)
     {
           // all of the options for a serial device
           // can be sent through the constructor of the SerialPort class
           // PortName = "COM1", Baud Rate = 19200, Parity = None, 
           // Data Bits = 8, Stop Bits = One, Handshake = None
           _serialPort = new SerialPort("COM1", 19200, Parity.None, 8, StopBits.One);
           _serialPort.Handshake = Handshake.None;
           _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
           _serialPort.ReadTimeout = 500;
           _serialPort.WriteTimeout = 500;
           _serialPort.Open();
     }

     private void btnStart_Click(object sender, EventArgs e)
     {
           // Makes sure serial port is open before trying to write
           try
           {
                if(!_serialPort.IsOpen)
                     _serialPort.Open();

                _serialPort.Write("SI\r\n");
           }
           catch (Exception ex)
           {
                MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
           }
     }

     void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
     {
          Thread.Sleep(500);
          string data = _serialPort.ReadLine();
          this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
     }

     private void si_DataReceived(string data)
     {
           textBox1.Text = data.Trim();
     }
}
雨的味道风的声音 2024-08-30 14:24:11

要留在像键盘一样的扫描仪上,您还可以尝试检测整个应用程序中按下的所有按键。要在应用程序中获取 KeyPress 事件,您应该将 Form.KeyPreview 设置为 true 并注册到表单的上述事件。

现在,您将在将每个密钥发送到当前活动控件之前收到它,并且您可以随心所欲地使用它。为了防止它在完成工作后发送到当前活动控件,请将 e.Handled 设置为 true

但这个解决方案肯定有两个缺点:

  1. 它仅在您的应用程序是活动窗口时才有效。因此,如果用户将焦点更改到另一个应用程序,您将不再按下按键。
  2. 也许确定传入的密钥是从扫描仪发送的还是用户刚刚按下键盘上的某个键可能会变得非常棘手。一种可能性可能是缓存传入的按键并测量它们之间的时间差,因为人类通常不会像扫描仪那样快速输入(除非您可能是一名秘书,一分钟内敲击 300 次或更多键盘;-) )。

To stay on a scanner that acts like a keyboard you could also try, to detect all key pressed within your whole application. To get a KeyPress event within your application you should set Form.KeyPreview to true and register to the above mentioned event of your form.

Now you'll receive every key before it is send to the current active control and you can do with it, whatever you like. To prevent that it will send to the current active control after you have done your work, set e.Handled to true.

But this solution has definitely two drawbacks:

  1. It only works if your application is the active window. So if the user changes the focus to another application you won't get the key presses anymore.
  2. Maybe it can become very tricky to decide if the incoming key was sent from a scanner or a user just pressed a key on his keyboard. One possibility could maybe to cache the incoming keys and measure the delta times between them, cause a human normally doesn't enter as fast as a scanner (unless you're maybe a secretary with 300 or more keyboard hits in a minute ;-)).
浴红衣 2024-08-30 14:24:11

我已经使用条形码扫描仪实现了类似的系统。我非常确定(不是100%,这是很久以前的事了)文本作为一个大字符串传入,并且只引发了一个按键事件。因此,您可以做的是将输入字符串与典型的条形码字符串进行匹配,例如 KeyPressEvent,如果匹配则查找该项目。或者甚至只是检查字符串中字符的长度(就好像它的键盘输入一次只能是 1 个字符一样)。

您是否在基本表单上测试过它?

I have implemented a similar system using a barcode scanner. I am pretty sure (not 100% it was a long time ago) that the text came in as the one large string and only raised the one keypress event. So what you could do is match the input string against a typical barcode string e.g. KeyPressEvent, and if it matches then lookup the item. Or even just check the length of chars in the string (as if its keyboard input it should only be 1 char at a time).

Have you tested it out on a basic form?

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