如何通过 zebra DS3608 - C# 将条码扫描到文本框中

How to scan barcode into textbox by zebra DS3608 - C#

我正在编写的应用程序不仅可以从组合框生成条码,还可以将条码扫描到文本框中。生成条码运行良好,但通过 Device Zebra DS3608 扫描条码时效果稍差。在这个项目中我添加了一个库:

using USB_Barcode_Scanner;

然后我在public方法中添加修改:

public PrintLabel()
        {
        InitializeComponent();
        textBox1.Focus();
        BarcodeScanner barcodeScanner = new BarcodeScanner(textBox1);
        barcodeScanner.BarcodeScanned += BarcodeScanner_BarcodeScanned;
        Fillcombox();
        }

并自动生成新方法。

  private void BarcodeScanner_BarcodeScanned(object sender, BarcodeScannerEventArgs e)
        {
            textBox1.Text = e.Barcode;
        }

此修改后它编译正确但在启动它而不是专注于文本框后我尝试扫描但它从按钮开始并扫描到组合框,下面是应用程序的图像:

我该如何更改它?它属于此条形码扫描器设置还是此代码?也许下面的代码有帮助:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Zen.Barcode;
using USB_Barcode_Scanner;

namespace IT_equipment_program
{
    public partial class PrintLabel : Form
    {
        MySqlConnection connection;
        MySqlCommand command;
        MySqlDataReader dr;
        public PrintLabel()
        {
            InitializeComponent();
            textBox1.Focus();
            BarcodeScanner barcodeScanner = new BarcodeScanner(textBox1);
            barcodeScanner.BarcodeScanned += BarcodeScanner_BarcodeScanned;
            Fillcombox();
        }

        private void BarcodeScanner_BarcodeScanned(object sender, BarcodeScannerEventArgs e)
        {
            textBox1.Text = e.Barcode;
        }

        void Fillcombox()
        {
            try
            {
                string con = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
                MySqlConnection SelectConnection = new MySqlConnection(con);

                string Selectquery = "SELECT DISTINCT Equipment FROM equipments";
                MySqlCommand com = new MySqlCommand(Selectquery, SelectConnection);
                SelectConnection.Open();

                MySqlDataReader dr = com.ExecuteReader();

                while (dr.Read())
                {
                    cmb_Equip.Items.Add(dr.GetString("Equipment"));
                }

                SelectConnection.Close();
            }

            catch
            {
                MessageBox.Show("please check internet connection.");
            }
        }

        void Fillcombox2()
        {
          try
          {
            string conn2 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
            string query2 = "SELECT IT_Equipments_No FROM equipments WHERE Equipment = @EQUIP";

            using (connection = new MySqlConnection(conn2))
            {
                using (command = new MySqlCommand(query2, connection))
                {
                    command.Parameters.AddWithValue("@EQUIP", cmb_Equip.Text);
                    connection.Open();

                    command.ExecuteNonQuery();
                    dr = command.ExecuteReader();
                    while (dr.Read())
                    {
                        cmb_IT_Equip_NO.Items.Add(dr.GetString("IT_Equipments_No"));
                    }
                    connection.Close();
                }
            }
          }

          catch
          {
              MessageBox.Show("Please check internet connection.");
          }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void cmb_Equip_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string conn3 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
                string query3 = "SELECT Equipment FROM equipments WHERE Equipment = @EQUIP";

                using (connection = new MySqlConnection(conn3))
                {
                    using (command = new MySqlCommand(query3, connection))
                    {
                        command.Parameters.AddWithValue("@EQUIP", cmb_Equip.Text);
                        connection.Open();

                        command.ExecuteNonQuery();
                        dr = command.ExecuteReader();
                        while (dr.Read())
                        {
                            string name = (string)dr["Equipment"].ToString();
                            txt_equip.Text = name;
                            cmb_IT_Equip_NO.SelectedIndex = -1;
                        }
                        cmb_IT_Equip_NO.Enabled = true;
                        cmb_IT_Equip_NO.Items.Clear();
                        Fillcombox2();
                        connection.Close();
                    }
                }
            }

            catch
            {
                MessageBox.Show("Please check internet connection.");
            }
        }

        private void cmb_IT_Equip_NO_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string conn4 = "server=127.0.0.1;port=3306;Database=et_system_pl;uid=root;pwd=;CharSet=utf8mb4;";
                string query4 = "SELECT IT_Equipments_No FROM equipments WHERE IT_Equipments_No = @EQUIP_NO";

                using (connection = new MySqlConnection(conn4))
                {
                    using (command = new MySqlCommand(query4, connection))
                    {
                        command.Parameters.AddWithValue("@EQUIP_NO", cmb_IT_Equip_NO.Text);
                        connection.Open();

                        command.ExecuteNonQuery();
                        dr = command.ExecuteReader();
                        while (dr.Read())
                        {
                            string number = (string)dr["IT_Equipments_No"].ToString();
                            txt_IT_NO.Text = number;
                        }
                        btn_generate.Enabled = true;
                        connection.Close();
                    }
                }
            }

            catch
            {
                MessageBox.Show("Please check internet connection.");
            }
        }

        private void btn_init_Click(object sender, EventArgs e)
        {
            cmb_IT_Equip_NO.SelectedIndex = -1;
            cmb_Equip.SelectedIndex = -1;
            cmb_IT_Equip_NO.Items.Clear();
            cmb_Equip.Items.Clear();

            btn_Print.Enabled = false;
            btn_generate.Enabled = false;
            cmb_IT_Equip_NO.Enabled = false;

            txt_equip.Text = string.Empty;
            txt_IT_NO.Text = string.Empty;
            Fillcombox();
        }

        private void btn_generate_Click(object sender, EventArgs e)
        {
            Code128BarcodeDraw Barcode = BarcodeDrawFactory.Code128WithChecksum;
            pictureBox1.Image = Barcode.Draw(txt_IT_NO.Text, 50);
            btn_Print.Enabled = true;
        }

        private void btn_Print_Click(object sender, EventArgs e)
        {
            PrintDialog pd = new PrintDialog();
            PrintDocument pDoc = new PrintDocument();
            pDoc.PrintPage += PrintPicture;
            pd.Document = pDoc;
            if (pd.ShowDialog() == DialogResult.OK)
            {
                pDoc.Print();
            }
        }

        private void PrintPicture(object sender, PrintPageEventArgs e)
        {
            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            pictureBox1.DrawToBitmap(bmp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
            e.Graphics.DrawImage(bmp, 0, 0);
            bmp.Dispose();
        }
    }
}

提前致谢。

根据问题和评论,这里是一个可行解决方案的基本概要:

  • 将扫描器配置为在条码数据传输之前和之后发送 ENTER
  • 删除 USB_Barcode_Scanner 命名空间以及对它公开的事件的所有引用
  • 在窗体上设置一个默认按钮,这样每当按下 ENTER 时就会执行按钮的点击事件
  • 在默认按钮的事件处理程序中,检查条码输入文本框是否为空
  • 如果文本框为空,将焦点设置到文本框
  • 如果文本框有文字,处理文本框中的文字,替换为空字符串

如果您进行这些调整,当条形码扫描器输入第一个 ENTER 时,焦点将更改为输入文本框,该文本框将接收条形码数据,第二个 ENTER 将允许您处理数据.

另外,如果您可以确定如何配置扫描器在条码数据之前发送ESC,您可以在表单上设置一个取消按钮并将焦点设置到取消按钮上的输入框,这可能会使过程更健壮,但您仍然需要确保应用程序具有键盘焦点。


此处的另一个选项是为虚拟 COM 端口重新配置扫描仪,并使用串行端口读取条码输入。在SerialPort data received事件中,可以capture/buffer监听stop sentinel的数据,收到stop sentinel后再进行处理。这具有额外的便利性,即使应用程序没有键盘焦点,它也能正常工作。

扫描仪可以[重新]配置为在实际条码之前发送前缀和后缀。

然后您可以将这些特殊字符用于有关字段更改的内部逻辑。

默认情况下扫描仪发送号码后跟“输入”,如果焦点在特定文本框上,则该文本框将收到号码。

如果您不想重新配置扫描仪,则可以在文本字段中键入“enter”时添加逻辑以将焦点移动到下一个控件。